Definition of synthetic reuse principles

Composite Reuse Principle (CRP)

Also called

Composition/Aggregate Reuse Principle (CARP)

. It requires that in software reuse, we should first use association relation such as combination or aggregation, and then consider using inheritance relation.




If inheritance is to be used, the Richter substitution principle must be strictly followed.

The principle of composite reuse and the Principle of Richter’s substitution complement each other.

The importance of synthetic reuse principles

The usual class reuse is divided into

Inheritance reuse

and

Synthesis of reuse

Although inheritance reuse has the advantages of being simple and easy to implement, it also has the following disadvantages.

  • Inheritance reuse breaks class encapsulation. Because inheritance exposes the implementation details of the parent class to the child class, the parent class is transparent to the child class, so this reuse is also known as “white box” reuse.
  • A subclass has a high degree of coupling with its parent. Any change in the implementation of the parent class results in a change in the implementation of the subclass, which is not conducive to the extension and maintenance of the class.
  • It limits the flexibility of reuse. An implementation inherited from a parent class is static and defined at compile time, so it cannot be changed at run time.


When using composite or aggregate reuse, existing objects can be incorporated into the new object, making it a part of the new object. The new object can invoke the functions of the existing object, which has the following advantages.

  • It maintains class encapsulation. Because the internal details of component objects are invisible to the new object, this reuse is also known as “black box” reuse.
  • Poor coupling between old and new classes. This reuse requires fewer dependencies, and the only way a new object can access a component object is through its interface.
  • High flexibility of reuse. This reuse can occur dynamically at run time, with new objects dynamically referencing objects of the same type as component objects.

The realization method of synthetic reuse principle

The principle of composite reuse is to integrate the existing object into the new object as a member object of the new object, the new object can call the function of the existing object, so as to achieve reuse.




The application of the principle of composite multiplexing is illustrated by the example of the vehicle classification management program.




The automobile classification management program.




Analysis: according to the “power source” can be divided into gasoline vehicles, electric vehicles, etc. According to the “color” can be divided into white cars, black cars and red cars. If you consider both categories, the combinations are numerous. Figure 1 shows a class diagram of vehicle classification implemented with the stepnet: relation.







FIG. 1 Class diagram of automobile classification with inheritance relation




As you can see from Figure 1, the implementation of inheritance generates many subclasses, and adding a new “power source” or a new “color” requires modifying the source code, which violates the open closed principle and is clearly not desirable. However, the above problems can be well solved by using combinatorial relations, as shown in Figure 2.







FIG. 2 Class diagram of automobile classification realized by combinatorial relation




Combined with the content of the previous sections, we have introduced a total of 7 design principles, which are respectively

The open closed principle

,

Richter’s substitution principle

,

Dependency inversion principle

,

Single responsibility principle

,

Interface Isolation Principle

,

Demeter’s rule

And the principles of composite reuse described in this section.




These seven design principles are software

Design patterns

Principles that must be followed as far as possible, and different principles require different emphases. Among them, the open and closed principle is the general principle, it tells us to be open to expansion, to modify closed; Richter’s substitution tells us not to break the inheritance system; The dependency inversion principle tells us to program for interfaces; The single responsibility principle tells us to implement a class with a single responsibility; The principle of interface isolation tells us to keep interfaces simple and simple when designing them. Demeter’s rule tells us to reduce coupling; The principle of composite reuse tells us to use composite or aggregate relation reuse in preference to inheritance relation reuse.