Six principles of design model-single Responsibility Principle

Source: Internet
Author: User

Background

With the advancement of science and technology, mobile phones have more and more functions and the effect is getting better and better. However, Photography enthusiasts still use slrs and reporters use recording pens during interviews, in business office, computers are used a lot... Maybe you can say that one day mobile phones may replace them, but you cannot deny that most of the time a product is simple, and a single responsibility may be a better choice.

Overview

Single Responsibility Principle (SRP) is the first of the six design principles in the design model. The so-called responsibility refers to the reason for class changes. What software design really needs to do is to discover responsibilities and separate those responsibilities from each other. If more than one motivation of a class is changed, this class has more than one responsibility. The single responsibility principle refers to the fact that, for a class, there should be only one reason for its change.

Problem cause

If a class has too many responsibilities, it is equivalent to coupling these responsibilities. A change in responsibilities may weaken or suppress the class's ability to fulfill other responsibilities. This coupling will lead to a fragile design. When a change occurs, the design will suffer unexpected damages.

Class P has two different responsibilities: T1 and T2. When class P needs to be modified due to a change in responsibility T1, it may be that the function of the originally normal role T2 fails.

In this case, we should follow the single responsibility principle and establish two different classes responsible for T1 and T1 respectively, so that when one of them is modified, the other can avoid functional faults.

Example

The following uses user management as an example to demonstrate the single responsibility principle. Define class UserManager at the business logic layer, define class UserDao at the data access layer, and define class User at the object Layer. Each class has different responsibilities and functions.

Public class User {private String username; private String userpass; private int role ;//............. Get and set methods for each attribute} public class UserDao extends BaseDao {// return the public List of all users
 
  
GetAllUser () {List
  
   
UserList = new ArrayList
   
    
();...... // Access Database return userList;} // search for the User's public User getUserByName (String name) {user User = null; String SQL = "SELECT * FROM userdetail WHERE username =? ";... // Find the user return User with the corresponding user name;} // Add the new user public boolean addUser (User user) {// return true to indicate success} public class UserManager {private UserDao userDao = new UserDao (); // verify the username and password public boolean CheckUser (String name, String pass) {boolean flag = false; User user = userDao. getUserByName (name); if (user! = Null & user. getUsername (). equals (pass) {flag = true;} return flag;} // register a new User public void registUser (user User user) {if (userDao. getUserByName (user. getUsername ())! = Null) {System. out. println ("the user name already exists"); return;} if (UserDao. addUser (user) {// registration successful} else {// registration Failed }}}
   
  
 
(Example from: http://wenku.baidu.com/view/d9459cabb0717fd5360cdcc3.html)

Advantages

Reduce the complexity of the class, improve the readability of the class, and improve the maintainability of the system. Make the code really easy to maintain, expand, reuse, and flexible and diverse.


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.