Talking about the JAVA design pattern-the responsibility chain pattern (COR), the design pattern cor
Reprinted please indicate the source: http://blog.csdn.net/l1028386804/article/details/45569099
I. Overview
So that multiple objects have the opportunity to process the request, so as to avoid coupling between the request sender and the receiver. Connect these objects into a chain and pass the request along the chain until an object processes it. The idea of this mode is to give multiple objects the opportunity to process a request, thus decoupling the sender and receiver.
Ii. Applicability
1. There are multiple objects that can process a request, and the object that processes the request is automatically determined at the runtime.
2. You want to submit a request to one of multiple objects without specifying the receiver explicitly.
3. The object set that can process a request should be dynamically specified.
3. Participants
1. Handler defines an interface for processing requests. (Optional) Implement the successor chain.
2. ConcreteHandler processes the requests it is responsible. Can access its successor. If the request can be processed, it will be processed; otherwise, the request will be forwarded to its successor.
3. The Client submits a request to the ConcreteHandler object on the chain.
Iv. Category chart
V. Example
Handler
package com.lyz.design.cor;import org.omg.CORBA.Request;/** * Handler * @author liuyazhuang * */public interface RequestHandle { void handleRequest(Request request);}
ConcreteHandler
Package com. lyz. design. cor; import org. omg. CORBA. request;/*** ConcreteHandler * @ author liuyazhuang **/public class HRRequestHandle implements RequestHandle {public void handleRequest (Request request) {if (Request instanceof DimissionRequest) {System. out. println ("to resign, personnel approval! ") ;}System. out. println (" request completed ");}}
Package com. lyz. design. cor;/*** ConcreteHandler * @ author liuyazhuang **/public class PMRequestHandle implements RequestHandle {RequestHandle rh; public PMRequestHandle (RequestHandle rh) {this. rh = rh;} public void handleRequest (Request request) {if (request instanceof AddMoneyRequest) {System. out. println ("to raise the salary, approved by the Project Manager! ") ;}Else {rh. handleRequest (request );}}}
Package com. lyz. design. cor;/*** ConcreteHandler * @ author liuyazhuang **/public class TLRequestHandle implements RequestHandle {RequestHandle rh; public TLRequestHandle (RequestHandle rh) {this. rh = rh;} public void handleRequest (Request request) {if (request instanceof LeaveRequest) {System. out. println ("to ask for leave, approved by the project leader! ") ;}Else {rh. handleRequest (request );}}}
Test
Package com. lyz. design. cor; import org. omg. CORBA. request;/*** Test * @ author liuyazhuang **/public class Test {public static void main (String [] args) {RequestHandle hr = new HRRequestHandle (); requestHandle pm = new PMRequestHandle (hr); RequestHandle tl = new TLRequestHandle (pm); // The team leader processes the resignation Request request Request = new Request (); tl. handleRequest (request); System. out. println ("============"); // team leader handles the salary increase request = new AddMoneyRequest (); tl. handleRequest (request); System. out. println ("========"); // The project manager handles the resignation request = new DimissionRequest (); pm. handleRequest (request );}}
Result
To resign, personnel approval! Request completed ============== ask for a raise, approved by the Project Manager! ========== To resign, personnel approval! Request completed