三国设计模式——观察者模式
Posted 昱歌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三国设计模式——观察者模式相关的知识,希望对你有一定的参考价值。
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 }
诸葛军师下令 进攻!!!
关羽将军进攻曹仁!!!
张飞将军火烧夏侯惇粮草!!!
赵云将军偷袭曹军营地!!!
以上是关于三国设计模式——观察者模式的主要内容,如果未能解决你的问题,请参考以下文章
请问为啥玩QQ三国是选择OpenGL模式或D3D模式游戏画面会出现白屏呢?