Design Patterns-Single responsibility principle

Source: Internet
Author: User

1. Single Responsibility Principle

Single Duty principle: change only because of one factor

"Zen of Design Patterns", the author mentions that someone has written an interface like this

void changeuser (userob userob,changeoptions option);

It's better to write separately

void changeusername (String userName); void changeuseraddress (String address); void Changeusertel (String Tel);

Although, as the author mentioned, the following substitution above, in the end should not be replaced? See the code is not enough, do not know ...

In my view of the HGE engine code, the author is the combination of the two, provide the first interface, the internal implementation of the process will be divided into a second form of the function, called internally.

Well, like this.

Changeuser (user_name,"Lily"); Changeuser (user_address,"Heave  "); Changeuser (User_tel,"138xxxxxx110");

Still like this one.

Changeusername ("Tom"); Changeuseraddress ("hell" ) ); Changeusertel ("189xxxxxx911");

    • For extended functionality, the obvious above is not good enough, because the Changeuser function is to be modified
    • If you want to modify the corresponding change function, the one above to modify the Changeuser and corresponding change function

--151120

Design Patterns-Single responsibility 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.