Design Mode 14th Job - Responsibility Chain Mode

Iterator mode - Provides a way to access elements in a collection object sequentially without revealing the object's int...
Responsibility Chain Model - Pay Code Implementation
Command mode
Command mode - code implementation of grill and Grill
Realize One
Realization Two
Realize Three - One-time Notification After Order
Basic introduction of the mode of municipal decree
Command mode code implementation
Command mode advantages

Iterator mode - Provides a way to access elements in a collection object sequentially without revealing the object's internal representation
Responsibility chain pattern: pattern definition
Make it possible for multiple objects to process the request, thereby avoiding the sender and sender of the request
Coupling between receivers.Connect these objects into a chain and follow
This chain passes requests until it has an object.

Responsibility Chain Model - Pay Code Implementation

//Code One public class main { public static void main(String[] args) { Manager m1=new Manager("Floret"); Manager m2=new Manager("Ponyo"); Manager m3=new Manager("Introduction"); Request r1=new Request(); r1.setRequestType("Pay raise"); r1.setRequestContent("Tiger requests a raise"); r1.setNumber(1000); m1.GetResult("manager", r1); m2.GetResult("Chief inspector", r1); m3.GetResult("General manager", r1); Request r2=new Request(); r2.setRequestType("leave"); r2.setRequestContent("Xiaolan is on leave"); r2.setNumber(3); m1.GetResult("manager", r2); m2.GetResult("Chief inspector", r2); m3.GetResult("General manager", r2); } } class Request{ private String requestType; private String RequestContent; private int number; public String getRequestType() { return requestType; } public void setRequestType(String requestType) { this.requestType = requestType; } public String getRequestContent() { return RequestContent; } public void setRequestContent(String requestContent) { RequestContent = requestContent; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } } //Management Class class Manager{ protected String name; public Manager(String name) { this.name=name; } public void GetResult(String managerLevel,Request request) { System.out.println(request.getRequestType()+":"+request.getNumber()); if(managerLevel=="manager") { if(request.getRequestType()=="leave"&&request.getNumber()<=2) { System.out.println("Number<=2,Approved"); }else { System.out.println("Number>2,Not authorized to approve"); } }else if(managerLevel=="Chief inspector"){ if(request.getRequestType()=="leave"&&request.getNumber()<=5) { System.out.println("Number<=5,Approved"); }else { System.out.println("Number>2,Not authorized to approve"); } }else if(managerLevel=="General manager") { if(request.getRequestType()=="leave") { System.out.println(""); }else if(request.getRequestType()=="Pay raise"&&request.getNumber()<=500) { System.out.println("Pay raise<=500,Approved"); }else if(request.getRequestType()=="Pay raise"&&request.getNumber()>500) { System.out.println("Pay raise>500,Not authorized to approve"); } } } }
//Code 2 public class main { public static void main(String[] args) { CommonManager m1=new CommonManager("Floret"); Majordomo m2=new Majordomo("Ponyo"); GeneralManager m3=new GeneralManager("Introduction"); m1.SetSuperior(m2); m2.SetSuperior(m3); Request r1=new Request(); r1.setRequestType("leave"); r1.setRequestContent("Xiaolan is on leave"); r1.setNumber(1); m1.RequestApplications(r1); Request r2=new Request(); r2.setRequestType("leave"); r2.setRequestContent("Flowers Take Leave"); r2.setNumber(4); m1.RequestApplications(r2); Request r3=new Request(); r3.setRequestType("Pay raise"); r3.setRequestContent("Small Basket Increase"); r3.setNumber(1000); m1.RequestApplications(r3); } } class Request{ private String requestType; private String RequestContent; private int number; public String getRequestType() { return requestType; } public void setRequestType(String requestType) { this.requestType = requestType; } public String getRequestContent() { return RequestContent; } public void setRequestContent(String requestContent) { RequestContent = requestContent; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } } //Manager class abstract class Manager{ protected String name; protected Manager superior; public Manager(String name) { this.name=name; } protected void SetSuperior(Manager superior) { this.superior=superior; } abstract public void RequestApplications(Request request); } //Operator class class CommonManager extends Manager{ public CommonManager(String name) { super(name); } public void RequestApplications(Request request) { if(request.getRequestType()=="leave"&&request.getNumber()<=2) { System.out.println(name+":"+request.getRequestContent()+"Number"+request.getNumber()+"Approved"); }else { if(superior!=null) { superior.RequestApplications(request); } } } } class Majordomo extends Manager{ public Majordomo(String name) { super(name); } public void RequestApplications(Request request) { if(request.getRequestType()=="leave"&&request.getNumber()<=5) { System.out.println(name+":"+request.getRequestContent()+"Number"+request.getNumber()+"Approved"); }else { if(superior!=null) { superior.RequestApplications(request); } } } } class GeneralManager extends Manager{ public GeneralManager(String name) { super(name); } public void RequestApplications(Request request) { if(request.getRequestType()=="leave") { System.out.println(name+":"+request.getRequestContent()+"Number"+request.getNumber()+"Approved"); }else if(request.getRequestType()=="Pay raise"&&request.getNumber()<=500){ System.out.println(name+":"+request.getRequestContent()+"Number"+request.getNumber()+"Approved"); }else { System.out.println(name+":"+request.getRequestContent()+"Number"+request.getNumber()+"We'll see"); } } }

Chain of Responsibility: Enables multiple objects to process requests without coupling between the sender and receiver of the request.
Connect this object into a chain and pass the request along the chain until one object processes it.
The client making this request here does not know which of these objects will ultimately process the request, so that changes to the system can dynamically reorganize and assign responsibility without affecting the client.

//Responsibility chain schematic code public class main { public static void main(String[] args) { Handler h1=new ConcreteHandler1(); h1.setName("Approver 1"); Handler h2=new ConcreteHandler2(); h2.setName("Approver 2"); Handler h3=new ConcreteHandler3(); h3.setName("Approver 3"); h1.SetSuccessor(h2); h2.SetSuccessor(h3); int []rs= {2,5,14,22,15,3,27,23}; for(int i=0;i<rs.length;i++) { h1.HandRequest(rs[i]); } } } abstract class Handler{ protected String name; protected Handler successor;//Set up successors public void SetSuccessor(Handler successor) { this.successor=successor; } public abstract void HandRequest(int request); public void setName(String name) { this.name=name; } public String getName(){ return name; } } //Specific Processing Class class ConcreteHandler1 extends Handler{ public void HandRequest(int request) { if(request>=0&&request<10) { System.out.println(this.getName()+"Processing Request"+request); }else if(successor!=null) { successor.HandRequest(request); } } } class ConcreteHandler2 extends Handler{ public void HandRequest(int request) { if(request>=10&&request<20) { System.out.println(this.getName()+"Processing Request"+request); }else if(successor!=null) { successor.HandRequest(request); } } } class ConcreteHandler3 extends Handler{ public void HandRequest(int request) { if(request>=20&&request<30) { System.out.println(this.getName()+"Processing Request"+request); }else if(successor!=null) { successor.HandRequest(request); } } }

Command mode

Command mode is also known as Action mode or Transaction mode.
Command mode encapsulates a request or operation into an object.Command mode allows the system to parameterize the client using different requests; queuing requests or logging requests provides command revocation and recovery capabilities.
Command mode is the encapsulation of commands: the responsibility to issue and execute commands is separated and delegated to different objects.
Command mode allows the requesting party and the receiving party to be independent, so that the requesting party does not have to know the interface of the receiving party, how the request was received, whether the operation was executed, when it was executed, and how it was executed.

Command mode - code implementation of grill and Grill

Realize One

public class main { public static void main(String[] args) { Barbercucer b=new Barbercucer(); b.BakeMutton(); b.BakeMutton(); b.BakeMutton(); b.BakeChickenWing(); b.BakeMutton(); b.BakeMutton(); b.BakeChickenWing(); } } class Barbercucer{ public void BakeMutton() { System.out.println("Shish kebab"); } public void BakeChickenWing() { System.out.println("Roast chicken wings"); } }

Realization Two

public class main { public static void main(String[] args) { Barbercucer b=new Barbercucer(); Command c1=new BakeChickenCommand(b); Command c2=new BakeMuttonCommand(b); Command c3=new BakeMuttonCommand(b); Waiter w=new Waiter(); w.SetOrder(c1); w.Notify(); w.SetOrder(c2); w.Notify(); w.SetOrder(c3); w.Notify(); } } class Barbercucer{ public void BakeMutton() { System.out.println("Shish kebab"); } public void BakeChickenWing() { System.out.println("Roast chicken wings"); } } //Abstract command class abstract class Command{ protected Barbercucer receiver; public Command(Barbercucer receiver) { this.receiver=receiver; } public abstract void ExcuteCommand(); } //Shish kebab class BakeMuttonCommand extends Command{ public BakeMuttonCommand(Barbercucer receiver) { super(receiver); } public void ExcuteCommand() { receiver.BakeMutton(); } } //Roast chicken wings class BakeChickenCommand extends Command{ public BakeChickenCommand(Barbercucer receiver) { super(receiver); } public void ExcuteCommand() { receiver.BakeChickenWing(); } } //Server class class Waiter{ private Command command; public void SetOrder(Command command) { this.command=command; } public void Notify() { command.ExcuteCommand(); } }

Realize Three - One-time Notification After Order

public class main { public static void main(String[] args) { Barbercucer b=new Barbercucer(); Command c1=new BakeChickenCommand(b); Command c2=new BakeMuttonCommand(b); Command c3=new BakeMuttonCommand(b); waiterlst w=new waiterlst(); w.SetOrder(c1); w.SetOrder(c2); w.SetOrder(c3); w.SetOrder(c1); w.CancelOrder(c1); w.Notify(); } } class Barbercucer{ public void BakeMutton() { System.out.println("Shish kebab"); } public void BakeChickenWing() { System.out.println("Roast chicken wings"); } } //Abstract command class abstract class Command{ protected Barbercucer receiver; public Command(Barbercucer receiver) { this.receiver=receiver; } public abstract void ExcuteCommand(); } //Shish kebab class BakeMuttonCommand extends Command{ public BakeMuttonCommand(Barbercucer receiver) { super(receiver); } public void ExcuteCommand() { receiver.BakeMutton(); } } //Roast chicken wings class BakeChickenCommand extends Command{ public BakeChickenCommand(Barbercucer receiver) { super(receiver); } public void ExcuteCommand() { receiver.BakeChickenWing(); } } //Server class class waiterlst{ private ArrayList<Command>orders=new ArrayList<Command>(); public void SetOrder(Command command) { if(command.getClass().getName()=="Barbercucer.BakeChickenWingCommand") { System.out.println("Attendant: There are no chicken wings left"); }else { orders.add(command); SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Add orders:"+command.getClass().getName()+"Time:"+df.format(new Date())); } } public void CancelOrder(Command command) { orders.remove(command); SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Cancellation of order:"+command.getClass().getName()+"Time:"+df.format(new Date())); } public void Notify() { Iterator<Command>cmd=orders.iterator(); while(cmd.hasNext()) { cmd.next().ExcuteCommand(); } } }

Basic introduction of the mode of municipal decree

1) Command Pattern: In software design, it is often necessary to send requests to certain objects, but you do not know who the recipient of the request is or which operation is requested. You only need to specify the specific recipient of the request when the program is running. In this case, you can use command mode to design.
2) Command mode makes the request sender and the request receiver decouple the peer-to-peer coupling, and makes the call relationship between objects more flexible and decoupled.
3) In command mode, a request is encapsulated as an object so that different parameters are used to represent different requests (that is, commands), and command mode also supports revocable operations.
4)Invoker is the caller (general), Receiver is the callee (soldier), MyCommand is the command, implements the Command interface, and holds the receiving object.

Command mode code implementation

public class main { public static void main(String[] args) { Receiver r=new Receiver(); Command c=new ConcreteCommand(r); Invoker i=new Invoker(); i.SetCommand(c); i.ExecuteCmommand(); } } abstract class Command{ protected Receiver receiver; public Command(Receiver receiver) { this.receiver=receiver; } abstract public void Execute(); } class ConcreteCommand extends Command{ public ConcreteCommand(Receiver receiver) { super(receiver); } public void Execute() { receiver.Action(); } } //Invoker class that requires the command to execute the request class Invoker{ private Command command; public void SetCommand(Command commannd) { this.command=commannd; } public void ExecuteCmommand() { command.Execute(); } } //Receiver class, which knows how to perform an operation related to the execution of a request, any class can act as a receiver class Receiver{ public void Action() { System.out.println("Execute Request!"); } }

Command mode advantages

Command mode allows the requesting party and the receiving party to evolve independently, with the following advantages:
Command mode makes it easy to incorporate new commands into your system.
Allows the receiving party to decide whether to Veto the request.
It is easier to design a command queue.
Undo and Redo for requests can be easily implemented.
Commands can be easily logged when needed.
Command mode separates the object requesting an operation from the object performing the operation.
Command classes, like any other class, can be modified and extended.

9 June 2020, 21:37 | Views: 5927

Add new comment

For adding a comment, please log in
or create account

0 comments