Design mode Chain of Responsibility (duty chain)

zhaozj2021-02-16  62

Chain of Responsibility Definition CHAIN ​​of Responsibility (COR) is a series of categories (Classes) attempts to process a request request, which is a loose coupling, and the only common is to pass the request between them. That is, a request is to process, and the A category is processed. If you do not process, it is passed to B category processing. If you don't have it, it is passed to C-category processing, which is transmitted like a chain (chain).

How to use? Although this section uses COR, but also demonstrates what is COR.

There is a Handler interface:

Public interface handler {public void handlerequest ();

This is an example of processing the request, if there are multiple requests, such as requesting help request printing or request formatting: The most thoughtful solution is: In the interface, add multiple requests: public interface handler {public void handlehelp (); Public void handleprint (); public void handlex ();

}

Specifically, a realization interface Handler code: public class concretehandler imports handler {private handler success

Public Concretehandler (Handler Success "{this.successor = SUCCESSOR;

Public void handlehelp () {// Confident processing request Help code ...}

Public void handleprint () {// If it is a print snoring processes print success,} public void handleFormat ()} If it is format rotation process ();

} There are three such specific implementation categories, which are processed to handle HELP, and process Print processing Format is probably our most common programming idea.

Although the idea is simple, there is an extension problem. If we need to add a request request type, you need to modify the interface and each of its implementations.

Second Solution: Both each request becomes a interface, so we have the following code:

Public interface helphandler {public void handlehelp ();

Public interface printhhandler {public void handleprint ();

Public interface formathandler {public void handlex ();

Public Class Concretehandler Implements Helphandler, PrintHandler, FormatHandlet {Private Helphandler Helpsuccessor; Private Printhandler PrintSuccessor; Private Formathandler FormatSuccessor;

public ConcreteHandler (HelpHandler helpSuccessor, PrintHandler printSuccessor, FormatHandler formatSuccessor) {this.helpSuccessor = helpSuccessor; this.printSuccessor = printSuccessor; this.formatSuccessor = formatSuccessor;} public void handleHelp () {.......}

Public void handleprint () {this.printsuccessor = printsuccessor;

Public void handleformat () {this.formatSuccessor = formatsuccessor;

}

This approach is increasing in the case of a new request request, but saves the modification of the interface, and the interface implementation ConcreteHandler needs to be modified. And the code is obviously not simple and beautiful.

Solution 3: Only one parameterization method is used in the Handler interface: public interface handler {public void handlerequest (String request);} The Handler implementation code is as follows: Public class concretehandler implements handler {private handler selfcess

Public Concretehandler (Handler Success "{this.successor = SUCCESSOR;

Public void handlerequest (String Request) {if (Request.Equals ("Help")) {// Here is the specific code of HELP} else // is passed to the next sucpsor.handle (request);

}

}

Here first, it is assumed to be a string type, if not what is wrong? Of course we can create a specialized category Request

Last Solution: Interface Handler code is as follows: public interface Handler {public void handleRequest (Request request);} Request defined categories: public class Request {private String type; public Request (String type) {this.type = type;}

Public string gettype () {return type;}

Public void execute () {// request real specific behavior code}} The Handler implementation code is as follows: Public class concretehandler implements handler {private handler success

Public Concretehandler (Handler Success "{this.successor = SUCCESSOR;

public void handleRequest (Request request) {if (request instanceof HelpRequest) {// This is the specific code} else if (request instanceof PrintRequst) {request.execute () Help the process;} else // passed to the next successor.handle (REQUEST);}}

}

This solution is COR, in a chain, there is a category of corresponding responsibilities, so it is called Chain of Responsibility.

The advantage of COR: because the request from the outside is not presented, if each category can be given, if you can't handle it, you can give up. Undoubted this reduces the coupling between categories.

The disadvantage is that the efficiency is low because the completion of a request may be traversed to finally complete, and of course it can also be optimized with the concept of trees. In Java AWT1.0, the processing of the mouse button is using COR, after java.1.1, use Observer instead of COR

Due to the spread, because in COR, there must be a unified interface handler. Limitations are here.

转载请注明原文地址:https://www.9cbs.com/read-20600.html

New Post(0)