Definition of synthetic reuse principles

Composite Reuse Principle (CRP) is 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

Generally, class reuse is divided into inheritance reuse and composite reuse. Although inheritance reuse has the advantages of simplicity and easy implementation, it also has the following disadvantages:

  1. 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.
  2. 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.
  3. 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.


In combination or aggregate reuse, existing objects can be incorporated into the new object and become a part of the new object. The new object can invoke the functions of the existing object, which has the following advantages:

  1. 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.
  2. Poor coupling between old and new classes. This reuse requires fewer dependencies, and the only way for a new object to access a member object is through the component object’s interface.
  3. High flexibility of reuse. This reuse can occur dynamically at run time, with new objects dynamically referencing objects of the same type as member objects.


The realization method of synthetic reuse principle


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.




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.


Combined with the content of the previous sections, we introduce seven design principles, which are open closed principle, Richter’s substitution principle, dependency inversion principle, single responsibility principle, interface isolation principle, Demeter’s rule and the composite reuse principle introduced in this section.


These seven design principles are the principles that software design patterns must follow, and they require different emphases. Among them:

  • The open and closed principle is the general principle. It tells us to be open to expansion and closed to modification.
  • 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 that we want to have 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 for advantage and inheritance relation reuse less.


Principles of Composite Reuse – Principles of object-oriented design