In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the advantages and disadvantages of the responsibility chain pattern of the web design pattern". In the daily operation, I believe that many people have doubts about the advantages and disadvantages of the responsibility chain pattern of the web design pattern. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the advantages and disadvantages of the responsibility chain pattern of the web design pattern?" Next, please follow the editor to study!
What is the chain of responsibility model?
The client issues a request, and the objects on the chain have the opportunity to process the request, and the client does not need to know who the specific object is. In this way, the decoupling between the requester and the receiver is realized, and the dynamic combination of responsibility chain can be realized on the client side. Make programming more flexible.
Keystone
There are multiple objects working together on a task.
These objects use a chained storage structure to form a chain, and each object knows its next object.
An object handles the task, and you can add some operations and pass the object to the next task. You can also end the processing of the task and end the task on this object.
The client is responsible for assembling the chain structure, but the client does not need to care who handles the task in the end.
Responsibility chain pattern structure diagram
Abstract processor (Handler) role: defines an interface for handling requests. If necessary, the interface can define a method to set and return a reference to the next home. This role is usually implemented by a Java abstract class or Java interface.
Specific handler (ConcreteHandler) role: after receiving the request, the specific processor can choose to process the request or pass the request to the next home.
Advantages and disadvantages: the main function of the responsibility chain model is: dynamic combination, decoupling of requester and receiver. The requester and receiver are loosely coupled: the requestor does not need to know the recipient or how to deal with it. Each responsibility object is only responsible for its own scope of responsibility, and the rest is left to the successor. The components are completely decoupled. Dynamic combination of responsibilities: the responsibility chain model will disperse the functions into separate responsibility objects, and then dynamically combine to form a chain when in use, so that the responsibility objects can be assigned flexibly, and the responsibility of changing objects can also be added flexibly. Disadvantages: produce a lot of fine-grained objects: because the functional processing is scattered into separate responsibility objects, each object has a single function, and a lot of responsibility objects are needed to deal with the whole process, which will produce a large number of fine-grained responsibility objects. May not be able to handle: each responsibility object is only responsible for its own part, so that a request can appear, and even if the entire chain is completed, there is no responsibility object to deal with it. This requires providing default processing and paying attention to the effectiveness of the construction chain. Application scenario
Multi-condition process judgment authority control
ERP system process approval General Manager, personnel Manager, Project Manager
The underlying implementation of Java filter Filter, for example: in the Java filter, the client sends the request to the server, and the filter goes through parameter filtering, session filtering, form filtering, hidden filtering, and detecting request header filtering.
Gateway privilege control responsibility chain mode
As the entrance to the micro-service program, the gateway intercepts all requests from the client to achieve permission control, such as judging the current limit of Api interface, blacklist, user session and parameter filtering. Api API current limit → blacklist intercepts → user session → parameter filtering
The handler processing scheme of the GatewayHandler abstract role public abstract class GatewayHandler {/ * the next handler * / protected GatewayHandler nextGatewayHandler; / * implementation must implement * / public abstract void service (); public void setNextGatewayHandler (GatewayHandler nextGatewayHandler) {this.nextGatewayHandler = nextGatewayHandler } / * execute the next handler * / protected void nextService () {if (nextGatewayHandler! = null) {nextGatewayHandler.service (); / / point to the next blacklist} specific handler implementation @ Componentpublic class CurrentLimitHandler extends GatewayHandler {@ Override public void service () {System.out.println ("the first gateway limit judgment....") NextService ();}} @ Componentpublic class BlacklistHandler extends GatewayHandler {@ Override public void service () {System.out.println ("second blacklist intercept judgment"); nextService ();} @ Componentpublic class ConversationHandler extends GatewayHandler {@ Override public void service () {System.out.println ("third user session interception judgment."); nextService () }} implement related sqlCREATE TABLE `prev_handler_ handler` (`ID`int (11) NOT NULL AUTO_INCREMENT COMMENT 'primary key ID', `handler_ name` varchar (32) DEFAULT NULL COMMENT' handler name', `handler_ id` varchar (32) DEFAULT NULL COMMENT 'handler primary key id', `prev_handler_ id`handler` based on database, `next_handler_ id`handler` (32) DEFAULT NULL COMMENT' next handler', PRIMARY KEY (`ID`) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COMMENT=' permission table' -Records of gateway_handler-- INSERT INTO `gateway_ handler` VALUES ('1600,' Api API current limit', 'currentLimitHandler', null,' blacklistHandler') INSERT INTO `gateway_ handler` VALUES ('17 blocks, 'blacklist blocking', 'blacklistHandler',' currentLimitHandler', 'conversationHandler'); INSERT INTO `gateway_ handler` VALUES (' 18 minutes, 'session authentication', 'conversationHandler',' blacklistHandler', null); GatewayHandlerService@Servicepublic class GatewayHandlerService {@ Autowired private GatewayHandlerMapper gatewayHandlerMapper; private GatewayHandler firstGatewayHandler / * get the first handeler encapsulation of the database * * @ return * / public GatewayHandler getFirstGatewayHandler () {if (firstGatewayHandler! = null) {return firstGatewayHandler;} / / 1. Query the database for the address hanlder GatewayHandlerEntity firstGatewayHandlerEntity = gatewayHandlerMapper.getFirstGatewayHandler (); if (firstGatewayHandlerEntity = = null) {return null;} / / 2. Get springboot injection container id String handlerId = firstGatewayHandlerEntity.getHandlerId (); GatewayHandler firstGatewayHandler = SpringUtils.getBean (handlerId, GatewayHandler.class); / / 3. Get the next handler container beanid String nextHandlerId = firstGatewayHandlerEntity.getNextHandlerId (); / / 4. Record the current loop hanlder object GatewayHandler tempGatewayHandler = firstGatewayHandler; while (! StringUtils.isEmpty (nextHandlerId)) {/ / 5. Get the next handerl object from the springboot container GatewayHandler nextGatewayHandler = SpringUtils.getBean (nextHandlerId, GatewayHandler.class); tempGatewayHandler.setNextGatewayHandler (nextGatewayHandler); / / 6. Set the next nextHandlerId GatewayHandlerEntity nextGatewayHandlerEntity = gatewayHandlerMapper.getByHandler (nextHandlerId); if (nextGatewayHandlerEntity = = null) {break;} nextHandlerId = nextGatewayHandlerEntity.getNextHandlerId (); tempGatewayHandler = nextGatewayHandler;} this.firstGatewayHandler = firstGatewayHandler; return firstGatewayHandler }} Database access layer GatewayHandlerMapperpublic interface GatewayHandlerMapper {/ * get the first GatewayHandler * * @ return * / @ Select ("SELECT handler_name AS handlerName,handler_id AS handlerid, prev_handler_id AS prevhandlerid, next_handler_id AS nexthandlerid FROM gateway_handler WHERE prev_handler_id is null;;") public GatewayHandlerEntity getFirstGatewayHandler () @ Select ("SELECT handler_name AS handlerName,handler_id AS handlerid, prev_handler_id AS prevhandlerid, next_handler_id AS nexthandlerid FROM gateway_handler WHERE handler_id=# {handlerId}") public GatewayHandlerEntity getByHandler (String handlerId);} @ Datapublic class GatewayHandlerEntity implements Serializable, Cloneable {/ * * primary key ID * / private Integer id; / * * handler name * / private String handlerName; / * * handler primary key id * / private String handlerId / * * next handler * / private String nextHandlerId;} controller@RestControllerpublic class HandlerController {@ Autowired private GatewayHandlerService gatewayHandlerService; @ RequestMapping ("/ client") public String client () {GatewayHandler firstGatewayHandler = gatewayHandlerService.getFirstGatewayHandler (); firstGatewayHandler.service (); return "success";}} execution effect
This article reference: ant classroom: http://www.mayikt.com
At this point, the study of "what are the advantages and disadvantages of the chain of responsibility pattern of web design pattern" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.