Talking about the single principle of c # design patterns,

Source: Internet
Author: User

Talking about the single principle of c # design patterns,

Single principle: the functional modules are independent during programming. A single function is more conducive to maintenance and reuse.

For example, a personal computer has many functions. It is difficult to create a new one if you want to use only one function. At the same time, if your computer cannot be turned on, and your calculator function cannot be used.

In programming, if a class encapsulates too many functions, the above results are similar.

Single Responsibility Principle

Example 1:


You can see that the interface design in this class diagram is problematic, and the user attributes are not separated from the user behavior. We split this interface based on user attributes and behaviors.



The API is split into two interfaces. IUserBo is responsible for user attributes and IUserBiz is responsible for user behavior. After instantiating the object UserINfo, we can use UserInfo as the IUserBo implementation class or IUserBiz implementation class, which depends on where we are used. To obtain user information, use UserInfo as the implementation class of IUserBOSS. To maintain user information, use it as the implementation class of IUserBiz. In practical applications, we tend to split an interface into two parts: IUserBO and IUserBIz. The class diagram is as follows.


After doing so, we split an interface into two, which conforms to the single responsibility principle. What is the single responsibility principle?

The core idea of a single responsibility principle is:One class is better to do only one thing. There is only one reason for its change.

The single responsibility principle can be seen as the extension of low coupling and high cohesion in the object-oriented principle. The responsibility is defined as the cause of change to improve cohesion to reduce the cause of change. There are too many responsibilities, and there may be more reasons for changes. This will lead to the dependency of duties, which will affect each other and greatly damage their cohesion and coupling. A single responsibility usually means a single function. Therefore, do not implement too many function points for the class to ensure that the entity has only one reason for its change.Example 2:

Rectangle has two methods. One is draw, which is used to draw a graph, and the other is area, which is used to calculate the area. Rectangle violates the single responsibility principle because it has two responsibilities: Calculating Area and drawing Rectangle. Drawing a graphic is related to the user interface, but the calculation of the graphic area is not necessarily related to the interface. If you write these two responsibilities into a class, if you only need to use area () to calculate the area, you have to compile the draw () method together, but it may not be used. If one of the roles needs to be modified, you have to re-compile and deploy the other. If the class has more than one responsibility, these responsibilities are coupled. Changing a role may affect and impede the functionality of a class for other services. It is better to separate the two roles.


The two duties are separated, so the coupling degree will be reduced. The core of the SRP principle is to require only one change to the class. classes that violate this principle should be reconstructed, for example, separating duties in the Fa-ade mode or Proxy mode, use Basic methods such as Extract Interface, Extract Class, and Extract Method.

Example 3:Look at the interface below the detail

Public interface Phone
{
// Call
Public void dial (string phoneNumber );
// Call
Public void chat (object o );
// Response
Public void answer (object o );
// Call completed
Public void huangup ();
}


Is there a problem with this interface? There is a problem. A single responsibility requires that an interface or class has only one reason for change, that is, an interface or class has only one responsibility, and it is responsible for one thing. Phone is not a responsibility. It has two responsibilities: Protocol management and data transmission. The diag () and huangup () methods implement Protocol management, dialing, and hanging up. Chat () and answer () are data transmission. Protocol changes and data transmission will cause class changes, so we cannot say that it complies with the single responsibility principle. Since these two responsibilities do not affect each other, we should consider splitting them into two interfaces.

This class diagram already conforms to the single responsibility principle, but is much more complicated. combination is a strong coupling relationship, and both have a common life cycle. This kind of strong coupling increases the complexity of the class, let's modify it.



This design is perfect. A mobile phone implements two interfaces and integrates two responsibilities into one class. Although you may think this phone class has two reasons for change, we are interface-oriented programming, APIS rather than implementation classes are published. If you want to make the class conform to the single responsibility principle, you must use the previous class diagram. However, the coupling of the class increases.

Benefits of a single responsibility principle:Class complexity reduced readability improved maintainability improved risks caused by changes reduced

Related Article

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.