Six principles of design patterns (1)-single Responsibility Principle

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.