7. Principles of composite reuse

Article Directory

1. Definition

The composite multiplexing principle is also called the combination\aggregation multiplexing principle, which is defined as follows:

Try to use object composition instead of inheritance to achieve the purpose of reuse .

2. Analysis

1. Overview of composite reuse principles

The principle of composite reuse is to pass in a new object Association relationship (including combination relationship and aggregation relationship) To use some existing objects and make them part of the new objects;

New objectTo achieve the purpose of reusing its existing functions by delegating the method of calling an existing object. In short:
Try to use composition/aggregation relationships and use less inheritance.

2. Two basic methods of reuse

In object-oriented design, existing designs and implementations can be reused in different environments through two basic methods, namely throughCombination/aggregation relationship or through inheritance.

2.1 Inheritance and reuse

Inheritance and reuse:

It is simple to implement and easy to extend. Destroy the encapsulation of the system; the implementation inherited from the base class is static and cannot be changed at runtime, and there is not enough flexibility; it can only be used in a limited environment. ("White Box" reuse)

Why the encapsulation is broken, B is a subclass of A, and B inherits A, then it is equivalent to knowing the attributes and behaviors of A (internal implementation); when A changes, B also changes, and the parent class changes. Subcategories are implicated.

2.2 Combination\aggregation multiplexing

Combination/aggregation multiplexing:

The degree of coupling is relatively low, and the operation of the member object is selectively called; it can be performed dynamically at runtime. ("Black box" reuse)

2.3 Matters needing attention

Combination/aggregation canMake the system more flexible, Between class and classReduced coupling, The change of each category has relatively little impact on other categories.

So generallyUse composition/aggregation first to achieve reuse, And then consider inheritance,

When using inheritance,Need to strictly follow the Richter substitution principle, Effective use of inheritance will help to understand the problem and reduce complexity, and abuse of inheritance will increase the difficulty of system construction and maintenance and the complexity of the system, so it is necessaryUse inheritance reuse carefully.

Three, examples

Insert picture description here

1. Explain the name

(1) getConnection(): connect to the database
(2) StudentDAO: add, delete, check and modify students
(3) TeacherDAO: add, delete, check and modify teachers

2. Lead the problem

(1) Before accessing the information of students or teachers, you must connect to the database and perform operations. Therefore, in order to reuse getConnection(), the two DAO classes are reused through inheritance.

(2) Such a design does not seem to be a problem, but what happens if the demand changes and whether it meets the opening and closing principle. Assuming the consequences of changes in requirements can be used as a reference for judging the quality of the design.

If you need to change the database connection method, such as the original JDBC connection to the database, now the database connection pool connection is used, you need to modify the DBUtil class source code.

If StudentDAO uses JDBC connection, but TeacherDAO uses connection pool connection, you need to add a new DBUtil class, and modify the source code of StudentDAO or TeacherDAO to inherit the new database connection class, which will violate the principle of opening and closing, and the system scalability Poor.

3. Reconstruction of the principle of composite reuse

Insert picture description here