Design Model-six principles-one Responsibility Principle

Source: Internet
Author: User

Single Responsibility Principle (SRP.

Definition:

There shoshould never be more than one reason for a class to change.

There should be only one reason for the change.

 

Sometimes, developers may have some problems when designing interfaces. For example, user attributes and user behaviors are declared in an interface. This causes the Business Objects and business logic to be put together. As a result, this interface has two responsibilities and the interface responsibilities are unclear, according to the definition of SRP, it violates the single responsibility principle of the interface.

The following is an example:

Package COM. loulijun. chapter1; public interface itutu {// height void setshengao (double height); double getshengao (); // weight void settizhong (double weight); double gettizhong (); // Boolean chifan (Boolean hungry); // Boolean shangwang (Boolean silly );}

In the above example, the height and weight are business objects, and the corresponding method is mainly responsible for user attributes. While eating and surfing the Internet are the corresponding business logic, mainly responsible for user behavior. However, this will give people a feeling that they do not know what the interface is, their responsibilities are unclear, and various problems will occur during later maintenance.

Solution: the single responsibility principle is to break this interface into two interfaces with different responsibilities.

Itutubo. Java: Responsible for Tutu (painted, for example, Personal Name) attributes

Package COM. loulijun. chapter1;/*** Bo: bussiness object, Business Object * responsible for user attributes * @ author administrator **/public interface itutubo {// height void setshengao (double height ); double getshengao (); // weight void settizhong (double weight); double gettizhong ();}

Itutubl. Java: Responsible for painting

Package COM. loulijun. chapter1;/*** Bl: business logic, business logic * is responsible for user behavior * @ author administrator **/public interface itutubl {// Boolean chifan (Boolean hungry ); // Boolean shangwang (Boolean silly );}

In this way, the single responsibility of the interface is realized. When implementing interfaces, two different classes are required.

Tutubo. Java

package com.loulijun.chapter1;public class TutuBO implements ItutuBO {private double height;private double weight;@Overridepublic double getShengao() {return height;}@Overridepublic double getTizhong() {return weight;}@Overridepublic void setShengao(double height) {this.height = height;}@Overridepublic void setTizhong(double weight) {this.weight = weight;}}

Tutubl. Java

Package COM. loulijun. chapter1; public class tutubl implements itutubl {@ overridepublic Boolean chifan (Boolean hungry) {If (hungry) {system. out. println ("Go eat hot pot... "); Return true;} return false;} @ overridepublic Boolean shangwang (Boolean silly) {If (silly) {system. out. println ("it's boring, it's going online... "); Return true;} return false ;}}

This makes it clear that when you need to modify user attributes, you only need to modify the itutubo interface, which only affects the tutubo class and does not affect other classes.

So what is the significance of the single responsibility principle?

1. Reduce the complexity of the class and clearly define what responsibilities are implemented.

2. improve readability

3. improve maintainability

4. reduces the risks caused by changes and is very helpful for system scalability and maintainability.

 

However, there is a problem with the use of a single responsibility principle. There is no clear division standard for "responsibilities". If the division of duties is too detailed, it will lead to a sharp increase in the number of interfaces and implementation classes, it increases complexity and reduces the maintainability of the Code. Therefore, we need to analyze the specific situation when using this responsibility. The suggestion is that the interface must adopt the single responsibility principle, and the single responsibility principle should be implemented as much as possible in class design. It is best to cause a class change for one reason.

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.