变了
最近多了很多东西要面对,有些事情令我的想法更加多了,感觉对各种事情的看法发生变法了。
可能是每段时期的想法可能都不一样,现在我和一年多的女朋友分手了,反而感觉轻松舒服了许多,也不用想究竟。
好了,感情问题的东西不用多想了。
最近我在学习设计模式,发现Head First设计模式 这本书很好,对比学校的清华教材,学习起来效率很高。
一开始先了解了很多种模式,细读了装饰者模式和观察者模式,如响雷惊耳,原来设计模式讲述的就是解决问题的方法,是前辈们积累下来的经验,让程序变得更可靠,灵活,稳定#¥%……&*的设计。
下面是自己用c++实现的代码,练练手,其中有些提醒自己的小注释,不影响阅读。
Observer:
#include <iostream> #include <vector> using namespace std ; class DisplayElement{ public: virtual void display() = 0; }; class Observer{ public: int index; virtual void update(int p,int t,int h)= 0; /////////推方式,可观察者通知观察者 virtual void update() = 0; //拉 virtual void setIndex(int) = 0; virtual int getIndex() = 0; // virtual void display() = 0; }; class Observable{ public: int count; vector<Observer*> observers; //////////用指针<Observer*> Observable(int p =0,int t = 0,int h = 0) { pressure = p; temperature = t; humidity = h; count = -1; } void addObserver(Observer *o) { count++; o->setIndex(count); observers.push_back(o); } void removeObserver(Observer *o) { int Index = o->getIndex(); int i = 0; for(vector<Observer*>::iterator it = observers.begin();it != observers.end();i++) { if(i==Index) observers.erase(it); else it++; } } void notifyObservers() { //// if(observers.begin()==observers.end()) { cout<<"wtf ,begin == end\n"; } for(vector<Observer*>::const_iterator it = observers.begin();it != observers.end();it++) { (*it)->update();//pressure,temperature,humidity); //////////加括号(*iter) } } /* void showObservers() { for(vector<Observer*>::iterator it = observers.begin();it != observers.end();it++) { (*it)->display(); } } */ void MesurementsChanged(int p,int t,int h) { pressure = p; temperature = t; humidity = h; notifyObservers(); } int getPressure(){return pressure;} int getTemperature(){return temperature;} int getHumidity(){return humidity;} private: int pressure; int temperature; int humidity; }; class ObserverOne:public Observer,public DisplayElement{ public: int temperature; int humidity; int index; Observable *observable; ObserverOne(Observable *observable) { this->observable = observable; observable->addObserver(this); temperature = 38; humidity = 70; index = -1; } virtual void setIndex(int i){index = i;} virtual int getIndex(){return index;} ~ObserverOne() { observable->removeObserver(this); } virtual void display() { cout<<"Current Conditions: "<<temperature<<" F degrees and " <<humidity<<" % humidity.\n"; } virtual void update(int p,int t,int h)//在“推”过来的数据中选取 { temperature = t; humidity = h; display(); } virtual void update()//拉 需要的数据过来 { temperature = observable->getTemperature() *10; humidity = observable->getHumidity() *10 ; display(); } }; int main() { Observable theme; ObserverOne CurrentCondition(&theme); CurrentCondition.display(); theme.MesurementsChanged(80,37,71); theme.MesurementsChanged(80,36,73); theme.MesurementsChanged(80,35,65); return 0; }
代码再混乱,只要有图解就很清晰的
Decorator:
#include <iostream> #include <string> using namespace std; class Beverage{ public: string description; public: Beverage(){ description = "Unknown Beverage"; } virtual const double cost() = 0; virtual string getDescription(){ return description; } }; class DarkRoast:public Beverage{ //深焙咖啡 public: //// string description; public: DarkRoast(){ description = "DarkRoast"; } const double cost(){ return 0.99; } }; class CondimentDecorator:public Beverage{ public: virtual string getDescription() = 0; }; class Milk:public CondimentDecorator{ //牛奶 private: Beverage *beverage; public: Milk(Beverage *beverage){ this->beverage = beverage; } string getDescription(){ return beverage->getDescription() + ", Milk"; } const double cost(){ return beverage->cost() + 0.10; } }; class Soy:public CondimentDecorator{ //豆浆 private: Beverage *beverage; public: Soy(Beverage *beverage){ this->beverage = beverage; } string getDescription(){ return beverage->getDescription() + ", Soy"; } const double cost(){ return beverage->cost() + 0.15; } }; class Whip:public CondimentDecorator{ //奶泡 private: Beverage *beverage; public: Whip(Beverage *beverage){ this->beverage = beverage; } string getDescription(){ return beverage->getDescription() + ", Whip"; } const double cost(){ return beverage->cost() + 0.10; } }; class Mocha:public CondimentDecorator{ //摩卡 private: Beverage *beverage; public: Mocha(Beverage *beverage){ this->beverage = beverage; } string getDescription(){ return beverage->getDescription() + ", Mocha"; } const double cost(){ return beverage->cost() + 0.20; } }; void display(Beverage* beverage){ cout<<beverage->getDescription()<<' '<<beverage->cost()<<'$'<<endl; } int main() { Beverage *beverage = new DarkRoast(); display(beverage); beverage= new Mocha(beverage); display(beverage); beverage = new Milk(beverage); display(beverage); delete beverage;//最近c++写得少了,连内存都忘记释放了!
return 0; }