Design Patterns Getting Started - CHAIN ​​of Responsibility [Essence]

xiaoxiao2021-03-06  76

Design Patterns Getting Started - CHAIN ​​of Responsibility [Essence]

The concept of Chain of Responsibility, even a newcomer, a newcomer, a simple if ... Else if ... Else process control has the concept of responsible chain: IF (/ * Compliance Conditions One * /) // Execute An Else if (/ * Compliance Request Conditions DIS * /) // Performing Request 2 ELSE // Performing Preset Request or Display Message This is from the viewpoint of structured programming If the concept of consignment, if you use the object's point of view, there is a better example is the Java's exception handling mechanism. When an exception occurred in the route, it is more complied with the exception captured by Catch, if conforms In order to perform the set processed, if it is not aligned to an appropriate exception, the exception is thrown out of the try ... catch block. In the GOF book, the purpose of the duties will be: allowing multiple objects to handle requests to avoid coupling relationships between requesting senders and recipients, combining these objects into a chain, along this The chain passes the request until there is an object to process it.

We first use an example to explain the request: public interface handler {public void handle ();} public class handleSymbol imports handler {public void handle () {system.out.println ("SYMBOL has been handled ");}} public class HandleCharacter implements Handler {public void handle () {System.out.println (" Character has been handled ");}} public class HandleNumber implements Handler {public void handle () {System. Out.println ("Number Has Been Handled");}} @} import java.io. *; public class main {public void chains () throws Exception {system.out.print ("Press any key twurn:"); char INPUT = (char) system.in.read (); handler request = null; if (character.isletter (input)) Request = new handlecharacter (); else if (Character.Indigit (Input)) request = new handlenumber () Else Request = New HandLesymbol (); request.handle ();} public static void main (string [] args) throws oews oException {new TES T (). CHAINS ();}} Execute: Press Any Key Then Return: XCharacter Has Been Handled This is a very simple program, you can determine that you entered the number, character or symbol, if we will objects organized in a manner of responsibilities between objects, we can rewrite the program as follows: public class Handler {private Handler successor; public void setSuccessor (Handler successor) {this.successor = successor;} public Handler getSuccessor () {return successor ;

} Public void handleRequest (char input) {if (successor = null!) Successor.handleRequest (input);}} public class HandleNumber extends Handler {public void handleRequest (char input) {if (Character.isDigit (input)) {System .out.println ( "Number has been handled");} else getSuccessor () handleRequest (input);.}} public class HandleCharacter extends Handler {public void handleRequest (char input) {if (Character.isLetter (input)) { System.out.println ( "Character has been handled");} else getSuccessor () handleRequest (input);.}} public class HandleSymbol extends Handler {public void handleRequest (char input) {System.out.println ( "Symbol has Been handled ");}} import java.io. *; public class main {public stat ic void main (String [] args) throws IOException {Handler numberHandler = new HandleNumber (); Handler characterHandler = new HandleCharacter (); Handler symbolHandler = new HandleSymbol (); numberHandler.setSuccessor (characterHandler); characterHandler.setSuccessor (symbolHandler); System.out.print ("Press any key kiln:"); char input = (char) system.in.read (); numerhandler.HandleRequest (}}} When the responsibility between the organizational object, usually Organize from fine-grained until coarse particle size, from special to abstraction,

Just regarding the numbers as the specialization of the character, the character is the specialization of the symbol. The UML structure diagram of the duties is as follows: From the time of execution of the item, its operation is very simple responsibility, as follows: The example we have lifted is very simple in the request, just compared to the input In addition, in more general, the request can be packaged as an object and provide a method between GetType () to compare the objects in the responsibility chain, for example: public class request {private string type; public request {private string type; public request {private () {This.type = type;} public string gettype () {RETURN TYPE;} PUBLIC VOID EXECUTE () {// Examples of execution request}} The example raised in the book of GOF is a secondary instruction system, in one In the interface, it is desirable that the user can obtain the relevant description topics. If the sub-element is described, the relevant description is displayed, otherwise, forward to the container element or parent element including it, to ensure that the user's auxiliary instructions can be obtained Respond. WHERE WOULD you want to go? Http://www.javaworld.com.tw/confluence/homepage.Arttp://www.caterpillar.onlyfun.net/phpbb2/

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

New Post(0)