Design Patterns - seven design principles (four) - Synthesis of multiplexing principles and design principles summary

Outline

A brief seven design principles:

  1. Opening and closing principle : that all core object-oriented design, open for extension, closed for modification
  2. Dependency Inversion Principle : For programming interfaces, depending on the abstract rather than relying on specific
  3. Single Responsibility Principle : An interface is only responsible for one thing, only one cause of class changes
  4. Interface segregation principle : the use of multiple specialized interface, instead of using a total Interface
  5. Demeter (the least known principle) : only communicate with friends (member variables, methods, input and output parameters), do not talk to strangers, control access modifier
  6. Richter Substitution Principle : subclass can extend the functionality of the parent class, but can not change the original function of the parent class
  7. Synthesis multiplexing principle object of the possible object composition (has-a) / polymerized (contanis-a), but not reached the inheritance of software reuse:

Synthesis of multiplexing principles

definition

Synthesis of multiplexing principles (Composite / Aggregate Reuse Principle, CARP) refers to the possible object composition (has-a) / polymerized (contanis-a), instead of inheritance purposes of software reuse. You can make the system more flexible, to reduce the coupling between the class and class, a relatively small change in the influence of other classes class caused.

Our inheritance is called white-box reuse, which is equivalent to all of the implementation details are exposed to the subclass. Composition / polymerization also known as black box multiplexed objects can not be accessed outside the class implementation details. To do code design based on specific business scenarios, in fact, we need to follow OOP model.

Examples

Or in database operations, for example, to create first DBConnectionclass:

/**
 * @author eamon.zhang
 * @date 2019-09-26 上午10:42
 */
public class DBConnection {
    public String getConnection() {
        return "MySQL 数据库连接";
    }
}

Creating ProductDaocategories:

/**
 * @author eamon.zhang
 * @date 2019-09-26 上午10:43
 */
public class ProductDao {
    private DBConnection dbConnection;

    public void setDbConnection(DBConnection dbConnection) {
        this.dbConnection = dbConnection;
    }

    public void addProduct() {
        String conn = dbConnection.getConnection();
        System.out.println("使用" + conn + "增加产品");
    }
}

This is a very typical synthesis multiplexing principle scenarios. However, the current design, it DBConnectionis not an abstraction, not easy system expansion. The system supports the current MySQLdatabase connection, assuming the business changes, the database layer to support operation of Oraclethe database. Of course, we can DBConnectionincrease the Oracledatabase supported methods. But contrary to the principle of opening and closing. In fact, we do not have to modify Daothe code will be DBConnectionrevised to abstract, look at the code:

/**
 * @author eamon.zhang
 * @date 2019-09-26 上午10:42
 */
public abstract class DBConnection {
    public abstract String getConnection();
}

Then, the MySQLlogic pulled:

/**
 * @author eamon.zhang
 * @date 2019-09-26 上午10:46
 */
public class MySQLConnection extends DBConnection {
    @Override
    public String getConnection() {
        return "MySQL 数据库连接";
    }
}

Then create Oraclea logical support of:

/**
 * @author eamon.zhang
 * @date 2019-09-26 上午10:47
 */
public class OracleConnection extends DBConnection {
    @Override
    public String getConnection() {
        return "Oracle 数据库连接";
    }
}

The particular choice to the application layer, look class diagram:


Summary of Design Principles

Learning design principles, learning the basic design patterns. In the actual development process, not necessarily require that all the code follows the design principle, we have to consider manpower, time, cost, quality, not deliberately pursuit of perfection, to follow the design principles in appropriate scene, embodies a kind of trade-offs, help us design a more elegant code structure.

Guess you like

Origin www.cnblogs.com/eamonzzz/p/11595602.html