1、設計原則
(1)開放封閉原則:類的改動是通過增加代碼進行的,而不是修改代碼
(2)依賴倒置原則:依賴于抽象,不要依賴于具體的實作(類),也就是針對接口程式設計
#include <iostream>
using namespace std;
class BankWorker //抽象類
{
public:
virtual void Worker() = 0;
};
class GetMoney:public BankWorker
{
public:
void Worker() //虛函數重寫
{
cout<<"取款業務"<<endl;
}
};
class SaveMoney:public BankWorker
{
public:
void Worker()
{
cout<<"存款業務"<<endl;
}
};
int main(int argc, char const *argv[])
{
BankWorker *b = new GetMoney;
b->Worker();
delete b;
b = new SaveMoney;
b->Worker();
delete b;
return 0;
}
2、依賴倒置原則
實列:電腦:硬碟、CPU
#include <iostream>
using namespace std;
class HardDisk
{
public:
virtual void Work() = 0;
};
class CPU
{
public:
virtual void Work() = 0;
};
class AHardDisk:public HardDisk
{
public:
void Work()
{
cout<<"AHard Disk work"<<endl;
}
};
class ACPU:public CPU
{
public:
void Work()
{
cout<<"ACPU work"<<endl;
}
};
class Computer
{
private:
HardDisk *m_h;
CPU *m_c;
public:
Computer(HardDisk *h, CPU *c)
{
m_h = h;
m_c = c;
}
void work()
{
m_h->Work();
m_c->Work();
}
};
int main(int argc, char const *argv[])
{
CPU *c = new ACPU; //c指向 ACPU,相當于可以操作裡面的public
HardDisk *h = new AHardDisk; //h指向 AHardDisk,相當于可以操作裡面的public
Computer *com = new Computer(h,c); //将兩個指針傳給 m_h, m_c, 他們自然可以操作work();
com->work();
return 0;
}
3、單例模式-懶漢式
單例模式:保證一個類隻生成唯一的執行個體對象,也就是說,整個程式空間中,隻存在一個執行個體對象。
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
class Singleton
{
private:
static Singleton *m_instance;
static int count;
Singleton()
{
}
public:
static Singleton *GetInstance()
{
if (NULL == m_instance)
{
printf("配置設定空間\n");
usleep(1000000);
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;
int main(int argc, char const *argv[])
{
Singleton *s1 = Singleton::GetInstance();
Singleton *s2 = Singleton::GetInstance();
Singleton *s3 = Singleton::GetInstance();
Singleton *s4 = Singleton::GetInstance();
cout<<Singleton::GetCount()<<endl;
if (s1 == s2)
{
cout<<"equal"<<endl;
}
s1->Release();
return 0;
}
加入線程!
#include <iostream>
#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)
{
printf("配置設定空間\n");
usleep(1000000);
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;
}
}
};
void *CreateInstance(void *arg)
{
/*
int ret = pthread_mutex_trylock(&mutex);
if (ret == 0)
{
cout<<"鎖沒有被使用"<<endl;
Singleton *s = Singleton::GetInstance();
cout<<s<<endl;
pthread_mutex_unlock(&mutex);
}
else if (EBUSY == ret)
{
cout<<"鎖正在被使用"<<endl;
}*/
pthread_mutex_lock(&mutex);
Singleton *s = Singleton::GetInstance();
cout<<s<<endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
Singleton * Singleton::m_instance = NULL;
int Singleton::count = 0;
int main(int argc, char const *argv[])
{
/* Singleton *s1 = Singleton::GetInstance();
Singleton *s2 = Singleton::GetInstance();
Singleton *s3 = Singleton::GetInstance();
Singleton *s4 = Singleton::GetInstance();
cout<<Singleton::GetCount()<<endl;
if (s1 == s2)
{
cout<<"equal"<<endl;
}
s1->Release();*/
pthread_mutex_init(&mutex, NULL);
int ret;
pthread_t tid[10];
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;
}
4、單例模式-餓漢式
先配置設定空間,在按照單例模式,隻配置設定一次空間。
#include <iostream>
#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)
{
printf("配置設定空間\n");
usleep(1000000);
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(int argc, char const *argv[])
{
Singleton *s1 = Singleton::GetInstance();
Singleton *s2 = Singleton::GetInstance();
Singleton *s3 = Singleton::GetInstance();
Singleton *s4 = Singleton::GetInstance();
cout<<Singleton::GetCount()<<endl;
if (s1 == s2)
{
cout<<"equal"<<endl;
}
s1->Release();
return 0;
}
5、工廠模式
(1)簡單工廠模式
#include <iostream>
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:
Fruit* CreateApple()
{
return new Apple;
}
Fruit* CreateBanana()
{
return new Banana;
}
Fruit* CreatePear()
{
return new Pear;
}
};
int main(int argc, char const *argv[])
{
Factory *f = new Factory;
Fruit *Fruit;
Fruit = f->CreateApple();
Fruit->show();
delete Fruit;
Fruit = f->CreateBanana();
Fruit->show();
delete Fruit;
return 0;
}
(2)工廠模式
#include <iostream>
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(int argc, char const *argv[])
{
Factory *f = new AppleFactory;
Fruit *Fruit;
Fruit = f->Create();
Fruit->show();
delete f;
delete Fruit;
f = new BananaFactory;
Fruit = f->Create();
Fruit->show();
delete f;
delete Fruit;
return 0;
}
(3)抽象工廠模式
#include <iostream>
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:
Fruit* CreateApple()
{
return new NorthApple;
}
Fruit* CreateBanana()
{
return new NorthBanana;
}
Fruit* CreatePear()
{
return new NorthPear;
}
};
class SouthFactory:public Factory
{
public:
Fruit* CreateApple()
{
return new SouthApple;
}
Fruit* CreateBanana()
{
return new SouthBanana;
}
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(int argc, char const *argv[])
{
Factory *f = new SouthFactory;
Create(f);
delete f;
f = new NorthFactory;
Create(f);
delete f;
return 0;
}
6、建造者模式
(1)簡單版
#include <iostream>
using namespace std;
class House
{
private:
string wall;
string window;
string door;
public:
void SetWall()
{
wall = "WALL";
}
void Setwindow()
{
window = "WINDOW";
}
void Setdoor()
{
door = "DOOR";
}
};
class Builder
{
private:
House *house;
public:
Builder(House *h)
{
house = h;
}
void Constructor()
{
house->Setdoor();
house->SetWall();
house->Setwindow();
}
};
int main(int argc, char const *argv[])
{
House *h = new House;
Builder *b = new Builder(h);
b->Constructor();
return 0;
}
(2)建造者模式
#include <iostream>
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 VillaBuilder:public Builder
{
public:
VillaBuilder(House *h)
{
house = h;
}
void Constructor()
{
house->Setdoor("VILLADOOR");
house->SetWall("VILLAWALL");
house->Setwindow("VILLAWINDOW");
}
};
class Designer
{
private:
Builder *b;
public:
void SetBuilder(Builder *builder)
{
b = builder;
}
void Constructor()
{
b->Constructor();
}
};
int main(int argc, char const *argv[])
{
Designer *d = new Designer;
House *h = new House;
Builder *b = new CommonBuilder(h);
d->SetBuilder(b);
d->Constructor();
delete b;
return 0;
}
7、原型模式
#include <iostream>
#include <cstring>
using namespace std;
class Person
{
protected:
char name[32];
int age;
public:
Person()
{
}
Person(char *n, int a)
{
strcpy(name,n);
age = a;
}
virtual void show() = 0;
virtual Person* clone() = 0;
};
class Student:public Person
{
private:
int id;
public:
Student()
{
}
Student(char *n, int a, int i):Person(n,a)
{
id = i;
}
void show()
{
cout<<"name = "<<name<<" age = "<<age<<" id = "<<id<<endl;
}
Person* clone()
{
Student *tmp = new Student;
*tmp = *this;
return tmp;
}
};
int main(int argc, char const *argv[])
{
Person *p1 = new Student("aa",18,100);
Person *p2 = p1->clone();
p2->show();
return 0;
}
8、組合模式
#include <iostream>
#include <list>
using namespace std;
class iFile
{
public:
virtual string GetName() = 0;
virtual int Add(iFile *file) = 0;
virtual int Remove(iFile *file) = 0;
virtual list<iFile*> *GetChild() = 0;
};
class File:public iFile
{
private:
string name;
public:
File(string n)
{
name = n;
}
string GetName()
{
return name;
}
int Add(iFile *file)
{
return -1;
}
int Remove(iFile *file)
{
return -1;
}
list<iFile *>*GetChild()
{
return NULL;
}
};
class Dir : public iFile
{
private:
string name;
list<iFile *> *l;
public:
Dir(string n)
{
name = n;
l = new list<iFile *>;
}
string GetName()
{
return name;
}
int Add(iFile *file)
{
l->push_back(file);
return 1;
}
int Remove(iFile *file)
{
l->remove(file);
return 1;
}
list<iFile*> *GetChild()
{
return l;
}
};
void show(iFile *root, int gap)
{
for (int i = 0; i < gap; i++)
{
cout<<"---";
}
if (root != NULL)
{
cout<<root->GetName()<<endl;
}
else
{
return;
}
list<iFile *> *l = root->GetChild();
if (l != NULL)
{
for (auto it = l->begin(); it != l->end(); it++)
{
show(*it, gap+1);
}
}
}
int main(int argc, char const *argv[])
{
iFile *dir1 = new Dir("home");
iFile *dir2 = new Dir("day1");
iFile *dir3 = new Dir("day2");
iFile *file1 = new File("1.c");
iFile *file2 = new File("2.c");
iFile *file3 = new File("3.c");
iFile *file4 = new File("4.c");
iFile *file5 = new File("5.c");
iFile *file6 = new File("6.c");
dir1->Add(file1);
dir2->Add(file2);
dir2->Add(file3);
dir3->Add(file4);
dir3->Add(file5);
dir3->Add(file6);
dir3->Add(file6);
dir1->Add(dir2);
dir1->Add(dir3);
show(dir1,0);
return 0;
}
9、代理模式
#include <iostream>
using namespace std;
class Sale
{
public:
virtual void SaleBook() = 0;
};
class BookStore:public Sale
{
private:
static int count;
public:
void SaleBook()
{
cout<<"書店賣書"<<endl;
count++;
}
static int GetCount()
{
return count;
}
};
int BookStore::count = 0;
class TaoBao:public Sale
{
private:
Sale *sale;
public:
TaoBao(Sale *s)
{
sale = s;
}
void SaleBook()
{
cout<<"網上賣書"<<endl;
sale->SaleBook();
}
};
int main(int argc, char const *argv[])
{
Sale *store = new BookStore;
Sale *taoBao = new TaoBao(store);
store->SaleBook();
taoBao->SaleBook();
cout<<BookStore::GetCount()<<endl;
return 0;
}
10、裝飾模式
//把要添加的附加功能分别放在單獨的類中,讓這個類包含有裝飾的對象
#include <iostream>
using namespace std;
class Phone
{
public:
virtual void Function() = 0;
};
class CallPhone:public Phone
{
public:
void Function()
{
cout<<"能打電話的手機"<<endl;
}
};
class PhotoPhone:public Phone
{
private:
Phone *phone;
public:
PhotoPhone(Phone *p)
{
phone = p;
}
void Function()
{
phone->Function();
cout<<"能拍照的手機"<<endl;
}
};
class MusicPhone:public Phone
{
private:
Phone *phone;
public:
MusicPhone(Phone *p)
{
phone = p;
}
void Function()
{
phone->Function();
cout<<"能聽音樂的手機.."<<endl;
}
};
class IPhone:public Phone
{
private:
Phone *phone;
public:
IPhone(Phone *p)
{
phone = p;
}
void Function()
{
phone->Function();
cout<<"能上網的手機..."<<endl;
}
};
int main(int argc, char const *argv[])
{
cout<<"***************打電話******************"<<endl;
Phone *callPhone = new CallPhone();
callPhone->Function();
cout<<"***************拍照******************"<<endl;
Phone *photoPhone = new PhotoPhone(callPhone);
photoPhone->Function();
cout<<"***************聽音樂******************"<<endl;
Phone *musicPhone = new MusicPhone(photoPhone);
musicPhone->Function();
cout<<"***************上網******************"<<endl;
Phone *iPhone = new IPhone(musicPhone);
iPhone->Function();
return 0;
}
11、擴充卡模式
//擴充卡模式:改變已有類或外部類的接口形式
//适用情況:将一個類的接口轉成客戶所希望的另一個接口
#include <iostream>
using namespace std;
class Current
{
public:
virtual int GetCurrent() = 0;
};
class Current_220:public Current
{
public:
int GetCurrent()
{
return 220;
}
};
class Adapter:public Current
{
private:
Current *current;
public:
Adapter(Current *c)
{
current = c;
}
int GetCurrent()
{
if (current->GetCurrent() == 220)
{
return 12;
}
else
{
return 5;
}
}
};
class Phone
{
private:
Current *current;
public:
Phone(Current *c)
{
current = c;
}
void Start()
{
if (current->GetCurrent() == 220)
{
cout<<"boom..."<<endl;
}
else if(current->GetCurrent() == 12)
{
cout<<"手機充電中......"<<endl;
}
}
};
int main(int argc, char const *argv[])
{
Current *c = new Current_220;
Current *a = new Adapter(c);
Phone *p = new Phone(a);
p->Start();
return 0;
}
12、享元模式
#include <iostream>
#include <map>
using namespace std;
class Student
{
private:
string name;
int age;
int id;
public:
Student()
{
}
Student(string n, int a, int i)
{
name = n;
age = a;
id = i;
}
string GetName()
{
return name;
}
int GetAge()
{
return age;
}
int GetId()
{
return id;
}
};
class FwFactory
{
private:
multimap<int, Student *> *m;
public:
FwFactory()
{
m = new multimap<int, Student*>;
}
~FwFactory()
{
while (!m->empty())
{
Student *tmp = m->begin()->second;
delete tmp;
m->erase(m->begin());
}
delete m;
}
void GetPerson(int id)
{
string name;
int age;
multimap<int,Student *>::iterator it;
it = m->find(id);
if (it == m->end())
{
cout<<"input the name age...."<<endl;
cin>>name>>age;
Student *s = new Student(name, age, id);
m->insert(make_pair(id,s));
}
else
{
Student *s = it->second;
cout<<s->GetName()<<s->GetAge()<<s->GetId()<<endl;
}
}
};
int main(int argc, char const *argv[])
{
FwFactory *f = new FwFactory;
f->GetPerson(1);
f->GetPerson(2);
f->GetPerson(3);
cout<<"*******************"<<endl;
f->GetPerson(3);
return 0;
}
13、觀察者模式
//當一個對象狀态發生變化時,自動通知其它關聯對象,自動解析對象狀态
#include <iostream>
#include <list>
using namespace std;
class Infom;
class Observer //觀察者
{
public:
virtual void Subscribe(Infom *l) = 0; //訂閱
virtual void Unsubscribe(Infom *l) = 0; //取消訂閱
virtual void Update(string str) = 0; //更新狀态
};
class Infom //通知者 秘書
{
public:
virtual void Add(Observer *o) = 0;
virtual void Remove(Observer *o) = 0;
virtual void Noftify(string str) = 0;
};
class secretary:public Infom
{
private:
list<Observer *> l;
public:
virtual void Add(Observer *o)
{
l.push_back(o);
}
virtual void Remove(Observer *o)
{
l.remove(o);
}
virtual void Noftify(string str)
{
for (auto it = l.begin(); it != l.end(); it++)
{
(*it)->Update(str);
}
}
};
class Staff:public Observer
{
public:
virtual void Subscribe(Infom *l) //訂閱
{
l->Add(this);
}
virtual void Unsubscribe(Infom *l) //取消訂閱
{
l->Remove(this);
}
virtual void Update(string str) //更新狀态
{
if (str == "WARNING")
{
cout<<"boss is coming"<<endl;
}
else
{
cout<<"continue to play games..."<<endl;
}
}
};
int main(int argc, char const *argv[])
{
Infom *i = new secretary;
Observer *o1 = new Staff;
Observer *o2 = new Staff;
Observer *o3 = new Staff;
Observer *o4 = new Staff;
o1->Subscribe(i);
o2->Subscribe(i);
o3->Subscribe(i);
o4->Subscribe(i);
i->Noftify("WARNING");
return 0;
}
14、橋接模式
#include <iostream>
using namespace std;
class Phone
{
public:
virtual void func() = 0;
};
class HUAWEL:public Phone
{
public:
void func()
{
cout<<"this is HUAWEI..."<<endl;
}
};
class Iphone:public Phone
{
public:
void func()
{
cout<<"this is Iphone..."<<endl;
}
};
class Soft
{
protected:
Phone *phone;
public:
virtual void func() = 0;
};
class QQ:public Soft
{
public:
QQ(Phone *p)
{
phone = p;
}
void func()
{
phone->func();
cout<<"this is QQ...."<<endl;
}
};
class Vchat:public Soft
{
public:
Vchat(Phone *p)
{
phone = p;
}
void func()
{
phone->func();
cout<<"this is Vchat...."<<endl;
}
};
int main(int argc, char const *argv[])
{
Phone *p = new HUAWEL;
Soft *s = new QQ(p);
s->func();
return 0;
}
15、外觀模式
#include <iostream>
using namespace std;
class SystemA
{
public:
void func()
{
cout<<"this is A..."<<endl;
}
};
class SystemB
{
public:
void func()
{
cout<<"this is B..."<<endl;
}
};
class SystemC
{
public:
void func()
{
cout<<"this is C..."<<endl;
}
};
class Facade
{
private:
SystemA *a;
SystemB *b;
SystemC *c;
public:
Facade()
{
a = new SystemA;
b = new SystemB;
c = new SystemC;
}
~Facade()
{
delete a;
delete b;
delete c;
}
void func()
{
a->func();
b->func();
c->func();
}
};
int main(int argc, char const *argv[])
{
Facade *f = new Facade;
f->func();
return 0;
}