Outline
A brief seven design principles:
- Opening and closing principle : that all core object-oriented design, open for extension, closed for modification
- Dependency Inversion Principle : For programming interfaces, depending on the abstract rather than relying on specific
- Single Responsibility Principle : An interface is only responsible for one thing, only one cause of class changes
- Interface segregation principle : the use of multiple specialized interface, instead of using a total Interface
- Demeter (the least known principle) : only communicate with friends (member variables, methods, input and output parameters), do not talk to strangers, control access modifier
- Richter Substitution Principle : subclass can extend the functionality of the parent class, but can not change the original function of the parent class
- 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 DBConnection
class:
/**
* @author eamon.zhang
* @date 2019-09-26 上午10:42
*/
public class DBConnection {
public String getConnection() {
return "MySQL 数据库连接";
}
}
Creating ProductDao
categories:
/**
* @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 DBConnection
is not an abstraction, not easy system expansion. The system supports the current MySQL
database connection, assuming the business changes, the database layer to support operation of Oracle
the database. Of course, we can DBConnection
increase the Oracle
database supported methods. But contrary to the principle of opening and closing. In fact, we do not have to modify Dao
the code will be DBConnection
revised 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 MySQL
logic pulled:
/**
* @author eamon.zhang
* @date 2019-09-26 上午10:46
*/
public class MySQLConnection extends DBConnection {
@Override
public String getConnection() {
return "MySQL 数据库连接";
}
}
Then create Oracle
a 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.