First what is Decoupling ??
Encapsulating the behavior that have several values.Decoupling encourage you to make another layer.
For Example
Suppose that u have animal class and another subclass class ( Dog , Bride ) this class inherit from animal as will and you need to add the capability of flying for this subclass , the best solution to do that is using Decoupling as following :-
public class animal {
public Flys FlyingType ;
}
public interface Flys{
string fly();
}
Encapsulating the behavior that have several values.Decoupling encourage you to make another layer.
For Example
Suppose that u have animal class and another subclass class ( Dog , Bride ) this class inherit from animal as will and you need to add the capability of flying for this subclass , the best solution to do that is using Decoupling as following :-
public class animal {
public Flys FlyingType ;
}
public interface Flys{
string fly();
}
class ItFly : Flys{
public string fly(){
return "It Can Fly";
}
}
class CantFly : Flys{
public string fly(){
return "It Can not Fly";
}
}
Now you can set and change the behavior in the fly.
You can also extend this behavior by add new class (FlySuperFast or FlyWithWings) without effect super-class or any subclass.
The Negative for using Decoupling is increase number of object and class.
Look following UML for more information
No comments:
Post a Comment