design_model(12)chain

1.Responsibility chain mode

Avoid the coupling relationship between the sender and the receiver of the request.

2.Example

public interface Leader {
	public  abstract void  leave(Apply  apl);
}

public class Manager  implements Leader  {
	 private  Leader  leader;
     public void  leave(Apply  apply) {
    	   if(apply.getI()>5) {
    		   System.out.println("The manager agrees.Leader.leave (apply);}else {System.out.println ("manager agrees");}}Public Leader getLeader () {Return leader;}Public void setLeader (Leader leader) {This.leader = leader;}}Public class Major implements Leader {Public void leave (Apply apply) {System.out.println ("director agrees");}}Public class Chain {Public statIC void aplly (Apply apply) {Manager Manager = new Manager ();Major major = new Major ();Manager.setLeader (Major);Manager.leave (apply);}}Public class Apply {Private inT I;Public int getI () {Return I;}Public void setI (int i) {This.i = I;}}Public class Client {Public static void main (String[] args) {Apply apply = new AppLy ();Apply.setI (10);Chain chain = new Chain ();Chain.aplly (apply);}}

 

Leave a Reply

Your email address will not be published. Required fields are marked *