Talking about the design mode: Chain Of Responsibility)
What is the responsibility chain model?
Official explanation: avoid coupling the sender of a request to its own er by giving multiple objects a chance to handle the request.
So that multiple objects have the opportunity to process requests, so as to avoid coupling between request senders and recipients.
In general, an execution command may be processed by multiple objects. To avoid a complicated association between the sender and the receiver, treat these processed objects as one node in a chain of responsibilities and send request commands to these nodes. All these nodes have the opportunity to process requests.
Why is the responsibility chain model used?
The sender and the receiver are decoupled. If not packaged, a command may correspond to an object that processes the command. In this way, the association between a sender and the receiver becomes complex, formed a strong coupling relationship. This does not benefit from system expansion. Concatenates the command processing object into a chain of responsibility for processing the command, which makes the sender and receiver weakly correlated, reduces coupling, and facilitates expansion.
The commands process links between objects through the successor (successor), so that the order of nodes can be changed flexibly without a fixed order.
When assigning responsibilities to an object, the responsibility chain gives you more flexibility. You can dynamically add or modify the chain at runtime to add or change the responsibilities for processing a request. You can combine this mechanism with the static special handling Object Inheritance Mechanism.
How to Use the responsibility chain model?
The UML diagram of the responsibility chain mode is as follows:
Explanation of each component:
Client: the sender of the command. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/examples/MHutcSz6c/zvdO/2 qOsxNq + example + 38 zltcTD/examples/Wwcu0psDtw/examples + examples/examples + vH8yDD/example + u908rc1d/example + release/zB7rbUz/release/Ox2NDrtq/release + tL/T67K7tL/release + tKbA7bjDw/release + oaO8tMP8we6/release + release/ jxNy21LjDw/tests + tests/OjrM7Sz + vKtc/tests + RmlsZbPktbHD/tests + DQo8cHJlIGNsYXNzPQ = "brush: java; "> Public abstract class Parser {private Parser successor; protected String fomat; public void transfer (File file) {if (getSuccessor ()! = Null) {getSuccessor (). parse (file);} else {System. out. println (the processor for parsing the text cannot be found);} public abstract void parse (File file); protected boolean canParseFile (File file) {return (file! = Null) & (file. getName (). endsWith (this. fomat);} public void setFomat (String fomat) {this. fomat = fomat;} public Parser getSuccessor () {return successor;} public void setSuccessor (Parser successor) {this. successor = successor ;}}
2. define a specific parser. When the file belongs to the scope of its own parsing, it will be parsed; otherwise, it will be passed to the successor.
Public class TextParser extends Parser {public TextParser (Parser successor) {setFomat (txt); setSuccessor (successor);} @ Override public void parse (File file) {if (canParseFile (file) {System. out. println (file. getName () + Text File parsed successfully);} else {super. transfer (file) ;}} public class XmlParser extends Parser {public XmlParser () {setFomat (xml) ;}@ Override public void parse (File file) {if (canParseFile (file) {System. out. println (file. getName () + XML file parsed successfully);} else {super. transfer (file );}}}
3. Define the sequence of the parser in the responsibility chain in the client. For the sake of conciseness, Text-> Xml is used.
public class client { public static void main(String[] args) { XmlParser xmlParser = new XmlParser(); TextParser textParser = new TextParser(xmlParser); textParser.parse(new File(test.xml)); }}
The following areas can be expanded:
1. The parser can be added flexibly. 2. The successor's initialization can not be performed in the constructor method, but the setter method is used. 3. You can change the structure to a file to Parse Multiple text formats as needed.That is, the "unpure" responsibility chain mentioned above. You can study the details by yourself.