1. 程式人生 > >三國設計模式——觀察者模式

三國設計模式——觀察者模式

efault bre ati fault println bst imp default mman

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 }
諸葛軍師下令 進攻!!!
關羽將軍進攻曹仁!!!
張飛將軍火燒夏侯惇糧草!!!
趙雲將軍偷襲曹軍營地!!!

三國設計模式——觀察者模式