In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge of this article "how to achieve the single responsibility principle of java design pattern" is not understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve the single responsibility principle of java design pattern" article.
The principle of single responsibility (Single Responsibility Principle), referred to as SRP.
Definition:
There should never be more than one reason for a class to change.
There should be one and only one reason for the class to change.
Sometimes, developers have some problems when designing an interface, such as the user's properties and user behavior are declared in an interface. As a result, business objects and business logic are put together, which results in two responsibilities of the interface, which is not clear, and violates the principle of single responsibility of the interface according to the definition of SRP.
Here is an example:
Package com.loulijun.chapter1; public interface Itutu {/ / height void setShengao (double height); double getShengao (); / weight void setTizhong (double weight); double getTizhong (); / / eating boolean chiFan (boolean hungry); / / online boolean shangWang (boolean silly);}
This problem exists in the above example. Height and weight belong to the business object, and the corresponding methods are mainly responsible for the attributes of the user. Eating and surfing the Internet are the corresponding business logic, which are mainly responsible for the behavior of users. But this will give people the impression that they don't know what the interface is really doing, the responsibility is not clear, and it will cause all kinds of problems in the later maintenance.
Solution: single responsibility principle, decompose this interface into two interfaces with different responsibilities
ItutuBO.java: responsible for the attributes of tutu (smear, if it is a person's name)
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: the act of being responsible for smearing
Package com.loulijun.chapter1; / * BL:Business Logic, business logic * responsible for user behavior * @ author Administrator * * / public interface ItutuBL {/ / eating boolean chiFan (boolean hungry); / / online boolean shangWang (boolean silly);}
This fulfills the single responsibility of the interface. Then when implementing the interface, you need to have two different classes
TutuBO.java
Package com.loulijun.chapter1; public class TutuBO implements ItutuBO {private double height; private double weight; @ Override public double getShengao () {return height;} @ Override public double getTizhong () {return weight;} @ Override public void setShengao (double height) {this.height = height } @ Override public void setTizhong (double weight) {this.weight = weight;}}
TutuBL.java
Package com.loulijun.chapter1; public class TutuBL implements ItutuBL {@ Override public boolean chiFan (boolean hungry) {if (hungry) {System.out.println ("go to eat hot pot..."); return true;} return false } @ Override public boolean shangWang (boolean silly) {if (silly) {System.out.println ("so boring, online"); return true;} return false;}}
This makes it clear that when you need to modify user properties, you only need to modify the ItutuBO interface, which will only affect the TutuBO class, not other classes.
So what is the significance of the principle of single responsibility?
Reduce the complexity of the class and have a clear definition of what kind of responsibilities to implement
Improve readability
Improve maintainability
Reducing the risk caused by change is very helpful to the expansibility and maintainability of the system.
However, there is a problem with using the single responsibility principle. There is no clear division standard for "responsibility". If the division of responsibilities is too detailed, it will lead to a sharp increase in the number of interfaces and implementation classes, but increase the complexity and reduce the maintainability of the code. Therefore, it is necessary to analyze the specific situation when using this responsibility. It is suggested that the interface must adopt the principle of single responsibility, and the design of the class should achieve the principle of single responsibility as much as possible. * is a reason for the change of a class.
The above is about the content of this article on "how to achieve the single responsibility principle of java design pattern". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.