
单例模式

//.h#include <iostream>using namespace std;class Singleton {public:static const Singleton* getInstance();static void doSomething(){cout << "doSomething" <<endl;}private:Singleton();~Singleton();static Singleton* This; //使用静态变量解决资源的分配和释放};//.c#include "Singleton.h"//Singleton* Singleton::This = NULL; //懒汉Singleton* Singleton::This = new Singleton(); //饿汉const Singleton* Singleton::getInstance(){if(!This){This = new Singleton();}return This;}Singleton::Singleton() {}Singleton::~Singleton() {}int main(){Singleton::getInstance() -> doSomething();}
//https://www.cnblogs.com/kohlrabi/p/9179630.html//https://blog.csdn.net/dove1202ly/article/details/81477511#include <iostream>using namespace std;class Singleton{public:static Singleton * getInstance(){return instance;}static void destroy(){if(instance != nullptr){delete instance;instance = NULL;}}private:Singleton(){}static Singleton* instance;};Singleton* Singleton::instance = new Singleton();int main(){Singleton* singleton = Singleton::getInstance();Singleton* singleton2 = Singleton::getInstance();if(singleton == singleton2){cout << "singleton1 = singleton2" << endl;}Singleton::destroy();if(Singleton::getInstance() == nullptr){cout << "singleton1 is destroy" << endl;}return 0;}
观察者模式


观察者
//observer//.h#include <iostream>using namespace std;class Observer {public:Observer() { ; }virtual ~Observer() { ; }//当被观察者发生变化时,通知virtual void Update(void* pArg) = 0;};
被观察者
//.h#include <list>#include <string>#include "Observer.h"class Observer;class Observerable {public:Observerable();virtual ~Observerable();void Attach(Observer* pOb);void Detach(Observer* pOb);int GetObserverCount() const {return _obs.size();}void DetachAll(){_obs.clear();}virtual void GetSomeNews(string str){SetChange(str);}protected:void SetChange(string news);private:bool _bChange;list<Observer*> _obs;void Notify(void* pArg);};//.c#include "Observerable.h"Observerable::Observerable():_bChange(false){}Observerable::~Observerable() {}void Observerable::Attach(Observer* pOb){if(pOb == NULL){return;}//auto c++11auto it = _obs.begin();for(; it != _obs.end(); it++){if(*it == pOb){return;}}_obs.push_back(pOb);}void Observerable::Detach(Observer* pOb){if((pOb == NULL) || (_obs.empty() == true)){return;}_obs.remove(pOb);}void Observerable::SetChange(string news){_bChange = true;Notify((void *) news.c_str());}void Observerable::Notify(void* pArg){if(_bChange == false){return;} else{auto it = _obs.begin();for(; it != _obs.end(); it++){(*it) -> Update(pArg);}_bChange = false;}}
使用
#include "Observerable.h"class User1: public Observer{void Update(void *pArg) {cout << "User1:" << (char *)pArg << endl;}};class User2: public Observer{void Update(void *pArg) {cout << "User2:" << (char *)pArg << endl;}};class News: public Observerable{public:void GetSomeNews(string str){SetChange("News:" + str);}};int main(){//帮忙把职责关系分清了User1 u1;User2 u2;News n1;n1.GetSomeNews("T0");cout << n1.GetObserverCount() << endl;n1.Attach(&u1);n1.Attach(&u2);n1.GetSomeNews("T1");cout << n1.GetObserverCount() << endl;n1.Detach(&u2);n1.GetSomeNews("T2");cout << n1.GetObserverCount() << endl;n1.DetachAll();n1.GetSomeNews("T3");cout << n1.GetObserverCount() << endl;return 1;}
适配器(Adapter)模式

#include <iostream>#include <string>using namespace std;class LegacyRectangle{public:LegacyRectangle(double x1, double y1, double x2, double y2){_x1 = x1;_y1 = y1;_x2 = y2;_y2 = y2;}void LegacyDraw(){cout << "LegacyRectangle:LegacyDraw:" << _x1 << " " << _y1 << " " << _x2 << " " << _y2 << " " <<endl;}private:double _x1;double _y1;double _x2;double _y2;};class Rectangle{public:virtual void draw(string str) = 0;};//第一种适配方式 多继承class RectangleAdapter: public Rectangle, public LegacyRectangle{public:RectangleAdapter(int x, int y, int w, int h):LegacyRectangle(x, y, x + w, y + h){cout << "RectangleAdapter(int x, int y, int w, int h)" << endl;}virtual void draw(string str){cout << "RectangleAdapter:draw" << endl;LegacyDraw();}};//第二种适配方式 组合class RectangleAdapter2: public Rectangle{public:RectangleAdapter2(int x, int y, int w, int h): _lRect(x, y, x + w, y + h){cout << "RectangleAdapter(int x, int y, int w, int h)" << endl;}virtual void draw(string str){cout << "RectangleAdapter:draw" << endl;_lRect.LegacyDraw();}private:LegacyRectangle _lRect;};int main(){int x = 20, y = 50, w = 200, h = 200;RectangleAdapter ra(x, y, w, h);Rectangle *pR = &ra;pR->draw("Rectangle1");cout << endl;RectangleAdapter2 ra2(x, y, w, h);Rectangle *pR2 = &ra2;pR2->draw("Rectangle2");return 0;}

