Talking about the object composition and inheritance

Lead:


In our careers, we often hear colleagues Daniel said: "To an interface for programming, rather than programming"; "instead of using a combination of priority inheritance." Well, today I will talk about objects in combination with inheritance.

  1. What is inheritance and composition
  2. Inherited advantages and disadvantages
  3. The combination of the advantages and disadvantages

What is inheritance and composition


Class or subclass inheritance so that we can realize other classes use to define the class (using superClass to define the implementation of subClass). Subclass are often called white box multiplexing (white-box reuse), as described with details of the internal superClass generally visible to a subclass.
Object composition required object having good interface combination, and by reference to other objects resulting from operation of fortune defined state. Therefore, objects may be combined into other objects to build more complex functions. Since details of the internal object is not visible to other objects, which appear as a "black box", this type of multiplexing is called black box multiplexing (black-box reuse).
This is what we often say that the black box and white-box reuse reuse.

Inherited advantages and disadvantages


Then use the inherited class or subclass What are some of the advantages and disadvantages of it:
Advantages:
1, class inheritance brutalization simple, intuitive relationship statically defined at compile time.
2, to achieve easy modification multiplexed, Sub can cover the super implemented.
Disadvantages: 1, can not be changed to inherit from the super implementation (not necessarily a disadvantage) at run time
2, sub a partial implementation is usually defined in the super.
3, sub directly to the super implementation details, thus undermining the closure large column  Talking object composition and inheritance installed.
4, any changes will be implemented to force super subclass also be changed, since their implementation linked together.
5, if implemented to inheritance issues in the new scene is obsolete or does not apply, it is necessary to rewrite super inherited or achieved.
Because of dependencies in the class hierarchy, implementation, subclasses are multiplexed may be a problem. There is a solution, the only inheritance (subtyping) from the protocol or abstract base class, the country is that they only rarely achieve, but the agreement did not materialize.

The combination of the advantages and disadvantages


Let's combination of objects using multiple objects, each object is assumed that the normal operation of the interface to other objects. Therefore, for the normal operation of the system, their interfaces need to carefully design. Now I will say that he is the advantages and disadvantages
advantages: 1, does not destroy the package, because only access the object through the interface;
2, reducing dependency to achieve, because the real face is defined by the interface;
3, you can run when any object is replaced with other objects of the same type;
4, can keep the wrapper classes to focus on a single task;
5, and his class hierarchy can be kept simple, not overly inflated and can not be managed;
disadvantages: 1, involving objects and more;
2, behavior of the system will depend on the relationships between different objects, rather than defining a single class;
3, ready-made components is not always enough, causing us to constantly define new objects.

Conclusion:


Despite these shortcomings, object composition still have a lot of benefits to the system design. We can in some sections with class inheritance to overcome these shortcomings, making it easier to create new components take advantage of existing components. Of course, with this class inherits or inherited it with class

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11711002.html