软件设计21|观察者模式
一、Java版本
(1)效果如下:
(2)类图如下:
(3)工程目录如下:
(4)代码如下:
Client.java
1 package observer; 2 3 public class Client { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 8 ConcreteSubject subject=new ConcreteSubject(); 9 ConcreteObserver investor1=new ConcreteObserver("股民1"); 10 ConcreteObserver investor2=new ConcreteObserver("股民2"); 11 ConcreteObserver investor3=new ConcreteObserver("股民3"); 12 subject.add(investor1); 13 subject.add(investor2); 14 subject.add(investor3); 15 16 subject.Rise(0.1); 17 subject.Rise(0.001); 18 19 subject.remove(investor3); 20 System.out.println(); 21 22 subject.Down(0.1); 23 subject.Down(0.001); 24 } 25 26 }
ConcreteObserver.java
1 package observer; 2 3 //具体观察者 4 public class ConcreteObserver implements Observer { 5 6 private String name; 7 public ConcreteObserver(String name){ 8 this.setName(name); 9 } 10 11 @Override 12 public void Rise() { 13 // TODO Auto-generated method stub 14 System.out.println("价格上涨,"+name+"买股票!"); 15 } 16 17 @Override 18 public void Down() { 19 // TODO Auto-generated method stub 20 System.out.println("价格下降,"+name+"大哭一场!"); 21 } 22 23 public String getName() { 24 return name; 25 } 26 27 public void setName(String name) { 28 this.name = name; 29 } 30 31 }
ConcreteSubject.java
1 package observer; 2 3 import java.util.ArrayList; 4 5 //具体目标类 6 public class ConcreteSubject extends Subject { 7 8 private ArrayList<Observer> observer; 9 10 public ConcreteSubject() { 11 observer=new ArrayList<>(); 12 } 13 14 @Override 15 public void add(Observer o) { 16 // TODO Auto-generated method stub 17 observer.add(o); 18 } 19 20 @Override 21 public void remove(Observer o) { 22 // TODO Auto-generated method stub 23 observer.remove(o); 24 } 25 26 @Override 27 public void Rise(double rise) { 28 // TODO Auto-generated method stub 29 if(rise>0.05) { 30 for(Observer obs:observer) { 31 obs.Rise(); 32 } 33 } 34 } 35 36 @Override 37 public void Down(double down) { 38 // TODO Auto-generated method stub 39 if(down<0.05) { 40 for(Observer obs:observer) { 41 obs.Down(); 42 } 43 } 44 } 45 46 }
Observer.java
1 package observer; 2 3 //抽象观察者 4 public interface Observer { 5 public void Rise();//股票升 6 public void Down();//股票降 7 }
Subject.java
1 package observer; 2 3 //抽象目标类 4 public abstract class Subject { 5 public abstract void add(Observer observer); 6 public abstract void remove(Observer observer); 7 public abstract void Rise(double rise); 8 public abstract void Down(double down); 9 }
(5)参考链接:设计模式——观察者模式
二、C++版本
(1)效果如下:
(2)类图如下:
(3)代码如下:
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 class Subject; 6 class Observer 7 { 8 public: 9 Observer() {}; 10 virtual ~Observer() {}; 11 virtual void Rise() = 0;//股票上升 12 virtual void Down() = 0;//股票下降 13 }; 14 15 class ConcreteObserver :public Observer 16 { 17 public: 18 ConcreteObserver(string name, Subject* subject) :m_observerName(name), m_subject(subject) {}; 19 ~ConcreteObserver() {}; 20 void Rise() { 21 cout << "价格上涨," << m_observerName << "买股票!" << endl; 22 } 23 void Down() { 24 cout << "价格下降," << m_observerName << "大哭一场!" << endl; 25 } 26 private: 27 string m_observerName; 28 Subject* m_subject; 29 }; 30 31 class Subject 32 { 33 public: 34 Subject() {}; 35 virtual ~Subject() {}; 36 void addObserver(Observer* observer) { 37 m_observers.push_back(observer); 38 } 39 void deleteObserver(Observer* observer) { 40 for (vector<Observer*>::iterator iter = m_observers.begin();iter != m_observers.end(); iter++){ 41 if (*iter == observer){ 42 m_observers.erase(iter); 43 return; 44 } 45 } 46 } 47 void Rise(double rise) { 48 if(rise>0.05){ 49 for (vector<Observer*>::iterator iter = m_observers.begin();iter != m_observers.end();iter++){ 50 (*iter)->Rise(); 51 } 52 } 53 } 54 void Down(double down) { 55 if (down < 0.05) { 56 for (vector<Observer*>::iterator iter = m_observers.begin(); iter != m_observers.end(); iter++) { 57 (*iter)->Down(); 58 } 59 } 60 } 61 private: 62 vector<Observer*> m_observers; 63 }; 64 65 class ConcreteSubject :public Subject 66 { 67 public: 68 ConcreteSubject(string name) :m_subjectName(name) {}; 69 ~ConcreteSubject() {}; 70 private: 71 string m_subjectName; 72 }; 73 74 int main() 75 { 76 Subject* subject = new ConcreteSubject("subject"); 77 78 Observer* observerA = new ConcreteObserver("observerA", subject); 79 Observer* observerB = new ConcreteObserver("observerB", subject); 80 81 subject->addObserver(observerA); 82 subject->addObserver(observerB); 83 84 subject->Rise(0.1); 85 cout << "***" << endl; 86 subject->Down(0.01); 87 88 return 0; 89 }
(4)参考链接:设计模式-观察者模式 C++实现