目录
一设计模式
## 1.1 设计原则
例1:
例2:
二 单例模式
2.1懒汉式
## 2.2饿汉式
三工厂模式
## 3.1抽象工厂模式
四 建造者模式
4.1简单版
4.2建造者模式
五原型模式
六组合模式
七代理模式
八装饰模式
九适配器模式
# 一 享元模式
# 二 桥接模式
# 三 外观模式
# 四 观察者模式
```
创建型模式:
工厂模式,抽象工厂模式,单例模式,建造者模式等
结构型模式:
关注类和对象的组合,继承被用来组合接口和定义组合对象
适配器模式,桥接模式 组合模式,外观模式 等
行为型模式:
关注对象之间的通信
```
```
1.开放封闭原则:所有新增的功能不是通过类的改动来实现,而是通过新增代码来实现。
2.依赖倒置原则:依赖与抽象,不依赖于具体的实现
```
```c++
#include
#include
#include
#include
using namespace std;
class BankWoker
{
public:
virtual void Worker() = 0;
};
class GetMoney :public BankWoker
{
public:
void Worker()
{
cout << "取款业务" << endl;
}
};
class SaveMoney :public BankWoker
{
public:
void Worker()
{
cout << "存款业务" << endl;
}
};
int main(void)
{
BankWoker *b = new GetMoney;
b->Worker();
delete b;
b = new SaveMoney;
b->Worker();
return 0;
}
```
```c++
#include
#include
#include
#include
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 << "hard disk work..." << endl;
}
};
class ACPU:public CPU
{
public:
virtual 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(void)
{
CPU *c = new ACPU;
HardDisk *h = new AHardDisk;
Computer com(h,c);
com.work();
return 0;
}
```
```c++
保证一个类只能生成唯一的实例对象,也就是说,在整个程序中,只存在一个实例对象。
```
```c++
#include
#include
#include
#include
#include
#include
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!"<
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< 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;
}
```
```c++
#include
#include
#include
#include
#include
#include
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!"<
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;
}
```
```c++
#include
#include
#include
#include
#include
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;
}
```
```c++
#include
#include
#include
#include
#include
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;
}
```
#include
#include
#include
#include
//#include
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()
{
House *h=new House;
Builder *b=new Builder(h);
b->Constructor();
return 0;
}
#include
#include
#include
#include
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()
{
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;
}
#include
#include
using namespace std;
class Person
{
protected:
int age;
string name;
public:
Person(){}
Person(string n,int a)
{
name=n;
age=a;
}
virtual Person *clone()=0;
virtual void show()=0;
};
class Student:public Person
{
private:
int id;
public:
Student(){}
Student(string n,int a,int i):Person(n, a)
{
id=i;
}
void show()
{
cout<clone();
p2->show();
return 0;
}
#include
#include
#include
using namespace std;
class iFile//虚函数
{
public:
virtual int Add(iFile *file)=0;
virtual string GetName()=0;
virtual int remove(iFile *fie)=0;
virtual list *GetChild()=0;
};
class File:public iFile
{
private:
string name;
public:
File(string n)
{
name=n;
}
virtual int Add(iFile *file)
{
return -1;
}
virtual string GetName()
{
return name;
}
virtual int remove(iFile *file)
{
return -1;
}
virtual list *GetChild()
{
return nullptr;
}
};
class Dir:public iFile
{
private:
string name;
list *l;
public:
Dir(string n)
{
name =n;
l=new list;
}
virtual int Add(iFile *file)
{
l->emplace_back(file);
return -1;
}
virtual string GetName()
{
return name;
}
virtual int remove(iFile *file)
{
l->remove(file);
return -1;
}
virtual list *GetChild()
{
return l;
}
};
void show(iFile *root,int gap)
{
for(int i=0;iGetName()<GetChild();
if(l!=nullptr)
{
for(auto it=l->begin();it!=l->end();it++)
{
show((*it),gap+1);
}
}
};
int main()
{
iFile *dir1=new Dir("day1");
iFile *dir2=new Dir("day2");
iFile *dir3=new Dir("day3");
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("9.c");
iFile *file7=new File("7.c");
dir2->Add(file1);
dir2->Add(file2);
dir2->Add(file3);
dir2->Add(file4);
dir2->Add(file5);
dir2->Add(file6);
dir3->Add(file1);
dir3->Add(file3);
dir3->Add(file5);
dir3->Add(file7);
dir3->Add(file2);
dir1->Add(dir2);
dir1->Add(dir3);
show(dir1,0);
return 0;
}
#include
#include
#include
using namespace std;
class Sale
{
public:
virtual void SaleBook()=0;
};
class BookStore:public Sale
{
private:
static int count;
public:
void SaleBook()
{
cout<<"书店卖书。。。"<SaleBook();
}
};
int BookStore::count=0;
int main()
{
Sale *store=new BookStore();
Sale *taobao=new TaoBao(store);
taobao->SaleBook();
store->SaleBook();
cout<
#include
#include
#include
using namespace std;
class Phone
{
public:
virtual void function()=0;
};
class CallPhone:public Phone
{
public:
void function()
{
cout<<" telephone"<function();
cout<<"take photoes phone"<function();
cout<<"lisen music phone"<function();
cout<<"go online phone"<function();
cout<<"**********take picture**********"<function();
cout<<"**********liste nmusic**********"<function();
cout<<"**********go online**********"<function();
return 0;
}
#include
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....."<start();
return 0;
}
主要通过与其它类对象共享数据来减少内存的使用
适用情况:有大量对象需要重复创建的时候,或者以共享内存的方式
```c++
#include
#include
};
int main(void)
{
FwFactory *f = new FwFactory;
f->GetPerson(1);
f->GetPerson(2);
f->GetPerson(3);
cout << "**************" << endl;
f->GetPerson(2);
return 0;
}
```
将抽象和实现分离,使他们可以独立变化。
基于类的最小设计原则
```c++
#include
#include
class Phone
{
public:
virtual void func() = 0;
};
class Iphone :public Phone
{
public:
void func()
{
cout << "this is IPHONE...." << endl;
}
};
class HUAWEI :public Phone
{
public:
void func()
{
cout << "this is HUAWEI" << 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(void)
{
Phone *p = new Iphone;
Soft *a = new QQ(p);
a->func();
return 0;
}
```
提供一个简单一致的界面,为了系统中统一的一套接口
```c++
#include
#include
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(void)
{
Facade *f = new Facade;
f->func();
return 0;
}
```
当对象存在一对多的关系的时候,使用观察者模式,当一个对象被修改的时候,则会自动通知
依赖他的所有对象。
```c++
#include
#include
i->Notify("WARNING");
return 0;
}
```