【设计模式】13、责任链模式

 

 

 1 package com.shejimoshi.behavioral.ChainOfResponsibility;
 2 
 3 
 4 /**
 5  * 功能:问题类型
 6  * 时间:2016年3月1日上午9:59:18
 7  * 作者:cutter_point
 8  */
 9 public enum QuestionType
10 {
11     HARD, MIDDLE, EASY
12 }

 

 1 package com.shejimoshi.behavioral.ChainOfResponsibility;
 2 
 3 
 4 /**
 5  * 功能:问题类,有问题类型
 6  * 时间:2016年3月1日上午9:54:23
 7  * 作者:cutter_point
 8  */
 9 public class Question
10 {
11     private QuestionType qt;
12     //构造
13     public Question(QuestionType qtt)
14     {
15         this.qt = qtt;
16     }
17     public QuestionType getQt()
18     {
19         return qt;
20     }
21     public void setQt(QuestionType qt)
22     {
23         this.qt = qt;
24     }
25     
26 }

 

 1 package com.shejimoshi.behavioral.ChainOfResponsibility;
 2 
 3 
 4 /**
 5  * 功能:处理问题的人物的抽象类
 6  * 时间:2016年3月1日上午10:02:04
 7  * 作者:cutter_point
 8  */
 9 public abstract class HandleQuestioner
10 {
11     //后继的责任对象
12     protected HandleQuestioner hqr;
13     
14     public void setHandleQurstioner(HandleQuestioner hqr)
15     {
16         this.hqr = hqr;
17     }
18     
19     public HandleQuestioner getHandleQurstioner()
20     {
21         return hqr;
22     }
23     
24     //处理问题
25     public abstract void handleQuestion(Question q);
26     
27 }

 

 1 package com.shejimoshi.behavioral.ChainOfResponsibility;
 2 
 3 
 4 /**
 5  * 功能:同事
 6  * 时间:2016年3月1日上午10:04:50
 7  * 作者:cutter_point
 8  */
 9 public class Colleague extends HandleQuestioner
10 {
11 
12     //处理方法
13     @Override
14     public void handleQuestion(Question q)
15     {
16         if(q.getQt() == QuestionType.EASY)
17         {
18             System.out.println("同事可以完美处理");
19         }
20         else
21         {
22             System.out.print("同事无法处理\t");
23             if(this.getHandleQurstioner() != null)
24                 this.getHandleQurstioner().handleQuestion(q); //交给上级处理
25         }
26     }
27 
28 }

 

 1 package com.shejimoshi.behavioral.ChainOfResponsibility;
 2 
 3 
 4 /**
 5  * 功能:老大
 6  * 时间:2016年3月1日上午10:10:10
 7  * 作者:cutter_point
 8  */
 9 public class Boss extends Colleague
10 {
11     //处理方法
12     @Override
13     public void handleQuestion(Question q)
14     {
15         if(q.getQt() == QuestionType.MIDDLE)
16         {
17             System.out.println("老大可以完美处理");
18         }
19         else
20         {
21             System.out.print("老大无法处理\t");
22             if(this.getHandleQurstioner() != null)
23                 this.getHandleQurstioner().handleQuestion(q); //交给上级处理
24         }
25     }
26 }

 

 1 package com.shejimoshi.behavioral.ChainOfResponsibility;
 2 
 3 
 4 /**
 5  * 功能:技术总监
 6  * 时间:2016年3月1日上午10:11:35
 7  * 作者:cutter_point
 8  */
 9 public class TechnicalDirector extends HandleQuestioner
10 {
11 
12     @Override
13     public void handleQuestion(Question q)
14     {
15         if(q.getQt() == QuestionType.HARD)
16         {
17             System.out.println("技术总监完美处理");
18         }
19         else
20         {
21             System.out.print("技术总监无法处理\t");
22             if(this.getHandleQurstioner() != null)
23                 this.getHandleQurstioner().handleQuestion(q); //上级处理
24         }
25     }
26 
27 }

 

测试代码:

 1 package com.shejimoshi.behavioral.ChainOfResponsibility;
 2 
 3 
 4 /**
 5  * 功能:责任链模式
 6  *         使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止
 7  * 适用:有多个的对象可以处理一个请求,那个对象处理该请求运行时刻自动确定
 8  *         你想在不明确指定接收者的情况下,想多个对象的一个提交一个请求
 9  *         可处理一个请求的对象集合应被动态指定
10  * 时间:2016年3月1日上午9:33:24
11  * 作者:cutter_point
12  */
13 public class Test
14 {
15     public static void main(String[] args)
16     {
17         //简单题
18         Question qq1 = new Question(QuestionType.EASY);
19         //中等问题
20         Question qq2 = new Question(QuestionType.MIDDLE);
21         //难题
22         Question qq3 = new Question(QuestionType.HARD);
23         HandleQuestioner colleague = new Colleague();
24         HandleQuestioner boss = new Boss();
25         HandleQuestioner technicalDirector = new TechnicalDirector();
26         colleague.setHandleQurstioner(boss);
27         boss.setHandleQurstioner(technicalDirector);
28         
29         colleague.handleQuestion(qq1);
30         colleague.handleQuestion(qq2);
31         colleague.handleQuestion(qq3);
32     }
33 }

 

显示结果:

同事可以完美处理
同事无法处理	老大可以完美处理
同事无法处理	老大无法处理	技术总监完美处理

  

 

posted @ 2016-03-01 10:26  cutter_point  阅读(357)  评论(0编辑  收藏  举报