码迷,mamicode.com
首页 > 其他好文 > 详细

三国设计模式——观察者模式

时间:2017-10-19 16:59:12      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:rgs   protect   new   and   三国   port   hang   ase   def   

1 package observer;
2 
3 public abstract class ObserverGenerals {
4     
5     protected Subject subject;
6     
7     public abstract void accept(String cmd);
8 }
 1 package observer;
 2 
 3 public class ObserverGuanYu extends ObserverGenerals{
 4 
 5     public ObserverGuanYu(Subject subject) {
 6         // TODO Auto-generated constructor stub
 7         this.subject = subject;
 8         subject.attach(this);
 9     }
10     
11     @Override
12     public void accept(String cmd) {
13         // TODO Auto-generated method stub
14         switch (cmd) {
15         case Commands.Attack:
16             System.out.println("关羽将军进攻曹仁!!!");
17             break;
18         case Commands.Retreat:
19             System.out.println("关羽将军死守博望坡!!!");
20             break;
21         default:
22             System.out.println("关羽将军原地待命!!!");
23             break;
24         }
25     }
26 
27 }
 1 package observer;
 2 
 3 public class ObserverZhangFei extends ObserverGenerals{
 4     
 5     public ObserverZhangFei(Subject subject) {
 6         // TODO Auto-generated constructor stub
 7         this.subject = subject;
 8         subject.attach(this);
 9     }
10     
11     @Override
12     public void accept(String cmd) {
13         // TODO Auto-generated method stub
14         switch (cmd) {
15         case Commands.Attack:
16             System.out.println("张飞将军火烧夏侯惇粮草!!!");
17             break;
18         case Commands.Retreat:
19             System.out.println("张飞将军退回中军帐!!!");
20             break;
21         default:
22             System.out.println("张飞将军原地待命!!!");
23             break;
24         }
25     }
26 
27 }
 1 package observer;
 2 
 3 public class ObserverZhaoYun extends ObserverGenerals{
 4     
 5     public ObserverZhaoYun(Subject subject) {
 6         // TODO Auto-generated constructor stub
 7         this.subject = subject;
 8         subject.attach(this);
 9     }
10     
11     @Override
12     public void accept(String cmd) {
13         // TODO Auto-generated method stub
14         switch (cmd) {
15         case Commands.Attack:
16             System.out.println("赵云将军偷袭曹军营地!!!");
17             break;
18         case Commands.Retreat:
19             System.out.println("赵云将军引夏侯惇进博望坡!!!");
20             break;
21         default:
22             System.out.println("赵云将军原地待命!!!");
23             break;
24         }
25     }
26 
27 }
 1 package observer;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class Subject {
 7     
 8     private List<ObserverGenerals> observerGeneralss;
 9     private String cmd;
10     
11     public Subject() {
12         // TODO Auto-generated constructor stub
13         this.observerGeneralss = new ArrayList<ObserverGenerals>();
14     }
15     
16     
17     public String getCmd() {
18         return cmd;
19     }
20 
21 
22     public void setCmd(String cmd) {
23         this.cmd = cmd;
24         this.command();
25     }
26 
27 
28     public void attach(ObserverGenerals observerGenerals){
29         this.observerGeneralss.add(observerGenerals);
30     }
31     
32     private void command(){
33         for (ObserverGenerals observerGenerals : observerGeneralss) {
34             observerGenerals.accept(cmd);
35         }
36     }
37     
38 }
 1 package observer;
 2 
 3 public class Barracks {
 4     
 5     private static Subject subject;
 6     private static ObserverGuanYu guanyu;
 7     private static ObserverZhangFei zhangfei;
 8     private static ObserverZhaoYun zhaoyun;
 9     
10     public static void main(String[] args) {
11         
12         subject = new Subject();
13         
14         guanyu = new ObserverGuanYu(subject);
15         zhangfei = new ObserverZhangFei(subject);
16         zhaoyun = new ObserverZhaoYun(subject);
17         
18         String cmd = Commands.Attack;
19         
20         System.out.println("诸葛军师下令 " + cmd + "!!!");
21         
22         subject.setCmd(cmd);
23                 
24     }
25     
26 }
诸葛军师下令 进攻!!!
关羽将军进攻曹仁!!!
张飞将军火烧夏侯惇粮草!!!
赵云将军偷袭曹军营地!!!

三国设计模式——观察者模式

标签:rgs   protect   new   and   三国   port   hang   ase   def   

原文地址:http://www.cnblogs.com/yuge790615/p/7693437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!