I recently encountered a lot of problems in the project process, and the customer constantly raised new requirements. It was really crazy to have a full-body pull. Now, let's look back and summarize our efforts to reduce the coupling between modules and embody the "High Cohesion and low coupling" principle. Here we will introduce the six design principles of the design model, the six principles are derived from the book "Zen of design patterns". I can't understand them when I graduated. Now I seem to be right. Okay, let's get started.
Single Responsibility Principle, Single Responsibility Principle. In a word, "There shoshould never be more than one reason for a class to change ".
Here I am going directly. Our general design is like this:
The single responsibility principle breaks the conventional design. It does this:
The separation of interfaces separates user information maintenance from business logic, reflecting the idea of interface-oriented programming. Each interface only implements its own responsibilities.
IUserBiz userInfo = new UserInfo ();
// If I want to assign a value, I think it is a pure BO.
IUserBO userBO = (IUserBO) userInfo;
UserBO. setPassword ("abc ");
// When I want to execute the action, I think it is a business logic class.
IUserBiz userBiz = (IUserBiz) userInfo;
UserBiz. deleteUser ();
What are the advantages of extracting the behavior of modifying object property information as an interface and extracting business actions as another interface?
1. The complexity of classes is reduced, and all responsibilities are clearly defined;
2. improved readability and reduced complexity. Of course, improved readability;
3. Improved maintainability. Of course, improved readability makes maintenance easier;
4. Reduce risks caused by changes.
The mysteries of the solution are as follows...
I would like to thank the book "Zen of design patterns". Of course, I also received a paper version from the Mechanical Industry Press project. Welcome programmers (yuan) to shoot bricks...
We look forward to the six principles of the next design model (2)-Lee's replacement principle.