3.2 单例模式-懒汉式
保证一个类只能生成唯一的实例对象,也就是说,在整个程序中,只存在一个实例对象。
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <unistd.h> #include <pthread.h> pthread_mutex_t mutex; using namespace std; class Singleton { private: static Singleton *m_instance; static int count; Singleton() { } public: static Singleton *GetInstance() { if (NULL == m_instance) { cout<<"m_instance = NULL!"<<endl; usleep(1000); m_instance = new Singleton; //全程只分配一次空间(懒汉式) } count++; return m_instance; } static int GetCount() { return count; } void Release() { count--; if (count == 0 && m_instance != NULL) { delete m_instance; } } }; Singleton *Singleton::m_instance = NULL; int Singleton::count = 0; void* CreateInstance(void *arg) { pthread_mutex_lock(&mutex); Singleton *s = Singleton::GetInstance(); cout<<s<<endl; pthread_mutex_unlock(&mutex); } int main(void) { /*Singleton *s1 = Singleton::GetInstance(); Singleton *s2 = Singleton::GetInstance(); Singleton *s3 = Singleton::GetInstance(); Singleton *s4 = Singleton::GetInstance(); Singleton *s5 = Singleton::GetInstance(); Singleton *s6 = Singleton::GetInstance(); cout << Singleton::GetCount() << endl; if (s1 == s2) { cout << "equal" << endl; }*/ pthread_mutex_init(&mutex,NULL); pthread_t tid[10]; int ret; for(int i = 0 ; i < 10;i++) { ret = pthread_create(&tid[i],NULL,CreateInstance,NULL); if(ret != 0) { perror("pthread_create"); } } void *status; for(int i = 0; i < 10;i++) { pthread_join(tid[i],&status); } pthread_mutex_destroy(&mutex); return 0; }
3.3 单例模式-饿汉式
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <unistd.h> #include <pthread.h> pthread_mutex_t mutex; using namespace std; class Singleton { private: static Singleton *m_instance; static int count; Singleton() { } public: static Singleton *GetInstance() { /*if (NULL == m_instance) { cout<<"m_instance = NULL!"<<endl; usleep(1000); m_instance = new Singleton; //全程只分配一次空间(懒汉式) }*/ count++; return m_instance; } static int GetCount() { return count; } void Release() { count--; if (count == 0 && m_instance != NULL) { delete m_instance; } } }; Singleton *Singleton::m_instance = new Singleton; //先分配内存空间(饿汉式) int Singleton::count = 0; int main(void) { Singleton *s1 = Singleton::GetInstance(); Singleton *s2 = Singleton::GetInstance(); Singleton *s3 = Singleton::GetInstance(); Singleton *s4 = Singleton::GetInstance(); Singleton *s5 = Singleton::GetInstance(); Singleton *s6 = Singleton::GetInstance(); cout << Singleton::GetCount() << endl; if (s1 == s2) { cout << "equal" << endl; } return 0; }
3.4 工厂模式
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <windows.h> using namespace std; class Fruit { public: virtual void show() = 0; }; class Apple :public Fruit { public: void show() { cout << "this is apple" << endl; } }; class Banana :public Fruit { public: void show() { cout << "this is Banana" << endl; } }; class Pear :public Fruit { public: void show() { cout << "this is Pear" << endl; } }; class Factory { public: virtual Fruit *Create() = 0; }; class AppleFactory :public Factory { public: Fruit *Create() { return new Apple; } }; class BananaFactory :public Factory { public: Fruit *Create() { return new Banana; } }; class PearFactory :public Factory { public: Fruit *Create() { return new Pear; } }; int main(void) { Factory *f = new AppleFactory; Fruit *fruit; fruit = f->Create(); fruit->show(); delete f; delete fruit; f = new BananaFactory; fruit = f->Create(); fruit->show(); return 0; }
3.5 抽象工厂模式
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <windows.h> using namespace std; class Fruit { public: virtual void show() = 0; }; class NorthApple :public Fruit { public: void show() { cout << "this is NorthApple" << endl; } }; class SouthApple :public Fruit { public: void show() { cout << "this is SouthApple" << endl; } }; class NorthBanana :public Fruit { public: void show() { cout << "this is NorthBanana" << endl; } }; class SouthBanana :public Fruit { public: void show() { cout << "this is SouthBanana" << endl; } }; class NorthPear :public Fruit { public: void show() { cout << "this is NorthPear" << endl; } }; class SouthPear :public Fruit { public: void show() { cout << "this is SouthPear" << endl; } }; class Factory { public: virtual Fruit *CreateApple() = 0; virtual Fruit *CreateBanana() = 0; virtual Fruit *CreatePear() = 0; }; class NorthFactory :public Factory { public: virtual Fruit *CreateApple() { return new NorthApple; } virtual Fruit *CreateBanana() { return new NorthBanana; } virtual Fruit *CreatePear() { return new NorthPear; } }; class SouthFactory :public Factory { public: virtual Fruit *CreateApple() { return new SouthApple; } virtual Fruit *CreateBanana() { return new SouthBanana; } virtual Fruit *CreatePear() { return new SouthPear; } }; void Create(Factory *f) { Fruit *fruit; fruit = f->CreateApple(); fruit->show(); delete fruit; fruit = f->CreateBanana(); fruit->show(); delete fruit; } int main(void) { Factory *f = new SouthFactory; Create(f); delete f; f = new NorthFactory; Create(f); Fruit *f1 = new SouthApple; f1->show(); return 0; }
一、工厂模式
factory是来通过对于的工厂创建apple等水果
在创建好后,用fruit来接,并且调用
二、抽象工厂和工厂的区别
汽车可以分为轿车、SUV、MPV等,也分为奔驰、宝马等。我们可以将奔驰的所有车看作是一个产品族,而将宝马的所有车看作是另一个产品族。分别对应两个工厂,一个是奔驰的工厂,另一个是宝马的工厂。与工厂方法不同,奔驰的工厂不只是生产具体的某一个产品,而是一族产品(奔驰轿车、奔驰SUV、奔驰MPV)。“抽象工厂”的“抽象”指的是就是这个意思。 即相比于工厂方法,抽象工厂定义了一系列的产品,而不是一个产品。
3.6 建造者模式
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; class House { private: string wall; string window; string door; public: void SetWall(string w) { wall = w; } void SetWindow(string w) { window = w; } void SetDoor(string d) { door = d; } }; class Builder { protected: House *house; public: virtual void Constructor() = 0; }; class CommonBuilder :public Builder { public: CommonBuilder(House *h) { house = h; } void Constructor() { house->SetDoor("DOOR"); house->SetWall("WALL"); house->SetWindow("WINDOW"); } }; class VilliaBuilder :public Builder { public: VilliaBuilder(House *h) { house = h; } void Constructor() { house->SetDoor("VILLIA-DOOR"); house->SetWall("VILLIA-WALL"); house->SetWindow("VILLIA-WINDOW"); } }; class Designer { private: Builder *b; public: void SetBuilder(Builder *Builder) { b = Builder; } void Constructor() { b->Constructor(); } }; int main(void) { Designer *d = new Designer; House *h = new House; Builder *b = new CommonBuilder(h); d->SetBuilder(b); d->Constructor(); delete b; b = new VilliaBuilder(h); d->SetBuilder(b); d->Constructor(); return 0; }