(1/6) design principles: Single Responsibility Principle

Single Responsibility Principle:

(Single Responsibility Principle, SRP)


There should be one and only one cause of change class:

Meaning that no matter doing, I only do one thing, you told me to go grocery shopping, I only buy food, he told me the way to take out the trash is not done


Applications:

Single responsibility principle applicable scope of interfaces, methods, class. By all accounts, interfaces, and methods must ensure single responsibility, the class does not have to ensure that they meet business on the line.


Suppose we want to be a user name and modify the function to change the password, you can have a variety of implementations, such as the following list two kinds of ways:

Wrong Demonstration:

public enum Operation
{
   [The Description ( " Modify nickname " )]
    UpdateUserName,
   [The Description ( " Change Password " )]
    UpdatePassword
}
public  interface IUserOperation
{
    void UpdateUserInfo(User user, Operation type);
}
public class UserOperation : IUserOperation
{
    public void UpdateUserInfo(User user, Operation type)
    {
         if (type == Operation.UpdateUserName)
         {
              // perform modification operations nickname 
         }
          the else  IF (type == Operation.UpdatePassword)
         {
              // perform an operation to modify the password 
         }
    }
}

The first is achieved by different things to do different types of Operation, to change the password and change the name coupled together, easy to cause problems, as long as a little attention, passed the wrong enumeration value on the tragedy, the code is also not very directly see what is operating, that is, this method is not clear responsibilities.


So we use the second way to achieve:

Correct demonstration:

public  interface IUserOperation
{
    void UpdateUserName(User user);
    void UpdatePassword(User user, string password);
}

public class UserOperation : IUserOperation
{
    public void UpdateUserName(User user)
    {
        // implement modification operations nickname 
    }

    public void UpdatePassword(User user, string password)
    {
       // realize the operation to change the password 
    }
}

The second realization, to change passwords and to change the name separated, that is, to change the password and change the name are treated as duty alone, like this is very clear, what you call the method, it is clearly aware of this method is realize what kind of logic. The conclusion is Shane? The second approach is consistent with the practice single responsibility principle. In reality we see a lot of code like the first implementation, but there is an enumeration of dozen cases, look at the code really strenuous.


And imagine this scenario, suppose we let Bob go out the trash, red go shopping, come back later called red red to wash the dishes. For the following two examples also achieved:

Wrong Demonstration:

public  class Xiaoming: IHousework
{
    public void Shopping()
    {
        // Xiao Ming is not shopping, it does not achieve 
        the throw  new new NotImplementedException ();
    }

    public void PourGarbage()
    {
        Console.WriteLine ( " Bob only trash " );
    }
}
public  class Xiaohong: IHousework
{
    public void Shopping()
    {
        Console.WriteLine ( " red shopping only " );
    }

    public void PourGarbage()
    {
        // red is not empty the garbage, so do not achieve 
        the throw  new new NotImplementedException ();
    }
}

Half-way back to red to wash the dishes, how to achieve? According to the wording, will add the interface washingUp () method Housework, then turn red Xiao Ming and implement dishes this method, but Bob is not specifically implement the code, like this is not felt very awkward, does not comply with the principle of single responsibility, modify a place, does not affect other places do not need to be changed, only make changes to the local need to use. Xiao Ming would not have been washing dishes, washing dishes but this method is going to achieve.


So we use the second way to achieve:

Correct demonstration:

public interface IShopping
{
    void DoShopping();
}

public interface IPourGarbage
{
    void DoPourGarbage();
}

Public  Interface IWashingUp
{
    void DoWashingUp();
}

public class Xiaoming : IPourGarbage
{
    public  void DoPourGarbage ()
    {
        Console.WriteLine ( " only achieve behavior garbage because garbage is only responsible for Xiao Ming " );
    }
}
public class Xiaohong : IShopping, IWashingUp
{
    public void DoShopping()
    {
        Console.WriteLine ( " realize shopping behavior, because red is only responsible for shopping " );
    }

    public void DoWashingUp()
    {
        Console.WriteLine ( " achieve dishwashing behavior, because red is also responsible for washing dishes " );
    }
}

You can see, this implementation of the different chores are as different responsibilities, they separated, this implementation can achieve on-demand type chores, Xiao Ming simply go to the trash, you realize PourGarbage interfaces, red and go shopping washing dishes, Shopping and WashingUp interfaces to achieve, will not affect the other side, this is the perfect preparation of the code out of the principle of single responsibility.


class

But for the implementation class will need to consider the various factors, the single responsibility principle rote cause sharp increase in class, to maintain bring a lot of trouble; and excessive duties but also for sub-categories of the complexity of the man-made manufacturing systems, had a class can implement behavior have to split into two, and then use a combination of the polymerization or re-coupled together, the complexity of a man-made system

Read some information like this can not say certain mandatory requirements by single responsibility principle points, or duties vary in the class said, not quite as clear as points above the interface in accordance with the principle of single responsibility is very clear and also very reasonable. Imagine this scene: We want to achieve a user registration, login, logout, can be like the following two kinds of ways:

The first implementation:

From the user's point of view, these operations are the user's behavior, it can be placed in a single class UserBiz:

public  class UserBiz
{
    public void Register(User user)
    {
        // Register Operation 
    }
     public  void the Login (the User User)
    {
        // login operation 
    }
     public  void Zimbabwe Logout (the User the User)
    {
        // logout 
    }
}

Second implementation:

Someone said, not to say a single responsibility it? From operational considerations, the need to register, login, logout separately:

public class UserRegisterBiz
{
    public void Register()
    {
        // registration operation 
    }
}

public class UserLoginBiz
{
    public void Login()
    {
        // login operation 
    }
}

public class UserLogoutBiz
{
    public void Logout()
    {
        // logout 
    }
}

Feel like bickering, in fact, not good or bad, depending on the specific business analysis, you say you login, registration, cancellation operation code a lot, you need to split, it apart, nothing wrong.


By the example above, we summarize the single responsibility principle What are the benefits:

    ● reduce the complexity of the class, realize what responsibilities are clear and unambiguous definitions;

    ● improve readability, reduce complexity, improve readability of course;

    ● improve maintainability, improve readability, it is certainly easier maintained;

    ● risk reduction due to the change, the change is essential, single responsibility if the interface done well, an interface repair

Just change the corresponding implementation class impact, no impact on other interfaces, this scalability of the system, maintenance has a very big
help.


to sum up:

The single responsibility principle, the purpose is to improve the maintainability, readability, scalability code, if the single responsibility for the destruction of these three characteristics, may outweigh the benefits.

Reference and Acknowledgments: http: //www.liebrother.com/single-responsibility-principle

Guess you like

Origin www.cnblogs.com/gme5/p/11850850.html