design_model(12)chain

1.责任链模式

避免请求的发送者和接受者之间的耦合关系。

2.实例

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("经理同意");
			leader.leave(apply);
    	   }else {
    		   System.out.println("经理同意");
    	   }
     }
	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("总监同意");
    }
}

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);
	}
}

 

posted @ 2018-09-01 14:45  fatale  阅读(85)  评论(0编辑  收藏  举报