The synthetic multiplexing principle, also known as the combinatorial/aggregation multiplexing principle (composition/aggregate reuse Principle, CARP), is defined as follows:
Synthetic multiplexing principles (Composite reuse Principle, CRP): Try to use object combinations instead of inheritance to achieve reuse. |
The principle of synthetic multiplexing is to use some existing objects in a new object to make them part of the new object through association relationships (including combinatorial relationships and aggregation relationships), and new objects to invoke the methods of the existing objects to achieve the purpose of multiplexing. In short: When you reuse, use the combination/aggregation Relationship (association relationship) sparingly and inherit less .
In object-oriented design, two methods can be used to reuse existing design and implementation in different environments, that is, through the combination/aggregation relationship or through inheritance, but first should consider using combination/aggregation, composition/aggregation can make the system more flexible, reduce the coupling between class and class, The change of one class has relatively little effect on other classes, and secondly, when inheriting is used, it is necessary to strictly follow the principle of the Richter substitution, and the effective use of inheritance will help to understand the problem and reduce the complexity, while abusing inheritance will increase the difficulty of building and maintaining the system and the complexity of the system. It is therefore prudent to use inheritance reuse.
The main problem with inheritance is that inheritance reuse destroys the encapsulation of the system, because inheritance exposes the implementation details of the base class to subclasses, because the inner details of the base class are usually visible to the child class, so this reuse is called "white box" reuse, and if the base class changes, then the implementation of the subclass has to change. The implementations inherited from the base class are static, impossible to change at run time, not flexible enough, and inheritance can only be used in a limited environment (such as a class is not declared to be inherited).
|
Extended For an in-depth understanding of inheritance, you can refer to the author of "Software Architecture design", Mr. Wen Yu's article-"See the mountain is only the mountain see water just water-ascension to the understanding of inheritance." |
|
Because a composition or aggregation relationship can incorporate an existing object (also known as a member object) into a new object, making it part of a new object, the new object can invoke the functionality of an existing object, which makes the internal implementation details of the member object invisible to the new object, so this reuse is also known as a "black box" reuse. Relative inheritance relation, its coupling degree is relatively low, the change of member object has little effect on new object, can invoke the operation of member object selectively in the new object according to actual need; composite multiplexing can be done dynamically at run time, and new objects can dynamically reference other objects of the same type as the member object.
In general, if the relationship between the two classes is "Has-a", the combination or aggregation should be used, and if the "is-a" relationship can use inheritance. "Is-a" is a strict taxonomic definition, meaning that a class is "one" of another class, while "Has-a" is different, which indicates that a role has a certain responsibility.
The following is a simple example to deepen the understanding of the principle of synthetic multiplexing:
Sunny software company developers in the initial CRM system design, considering the number of customers, the system uses MySQL as a database, and database operations related classes such as Customerdao class need to connect the database, the method of connecting the database getconnection () Encapsulated in the Dbutil class, because the Getconnection () method of the Dbutil class needs to be reused, the designer will Customerdao as a subclass of the Dbutil class, as shown in the initial design scheme structure 1: Figure 1 Schematic diagram of the initial design scheme As the number of customers increases, the system decides to upgrade to an Oracle database, so you need to add a new Oracledbutil class to connect to the Oracle database, since Customerdao and dbutil are inheritance relationships in the initial design. Therefore, in the replacement of the database connection mode needs to modify the source code of the Customerdao class, the Customerdao as a subclass of Oracledbutil, which will violate the open and closed principle. "Of course, you can also modify the source code of the Dbutil class, also violate the open and close principle. 】 It is now reconstructed using the synthetic multiplexing principle. |
According to the principle of synthetic reuse, we should use more correlation and less inheritance when we implement multiplexing. So in this example we can use associative multiplexing instead of inheritance reuse, as shown in the reconstructed structure 2:
Fig. 2 Structure diagram after reconstruction
In Figure 2, the relationship between Customerdao and Dbutil is changed from an inheritance relationship to an association, and a dependency injection is used to inject the Dbutil object into the Customerdao, either by using construct injection or by using setter injection. If you need to extend the functionality of Dbutil, you can do so through its subclasses, such as by using subclass Oracledbutil to connect to the Oracle database. Since Customerdao is programmed for Dbutil, the object of the Dbutil subclass can override the Dbutil object in accordance with the principle of the Richter substitution, and simply inject the subclass object into the Customerdao to use the method extended by the subclass. For example, by injecting a Oracledbutil object into the Customerdao, the Oracle database connection can be implemented, the original code does not need to be modified, and there is flexibility to add new ways of connecting the database.
The principle of object-oriented design in synthetic multiplexing