文章目录
- 一 享元模式
- 二 桥接模式
- 三 外观模式
- 四 观察者模式
一 享元模式
主要通过与其它类对象共享数据来减少内存的使用
适用情况:有大量对象需要重复创建的时候,或者以共享内存的方式
#include <iostream>
#include <map>
#include <string>
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->emplace(make_pair(id, s));
}
else
{
Student *s = it->second;
cout << s->GetName() << " " << s->GetAge() << " " << s->GetId() << endl;
}
}
};
int main(void)
{
FwFactory *f = new FwFactory;
f->GetPerson(1);
f->GetPerson(2);
f->GetPerson(3);
cout << "**************" << endl;
f->GetPerson(2);
return 0;
}
二 桥接模式
将抽象和实现分离,使他们可以独立变化。
基于类的最小设计原则
#include <iostream>
#include <map>
#include <string>
using namespace std;
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;
}
三 外观模式
提供一个简单一致的界面,为了系统中统一的一套接口
#include <iostream>
#include <map>
#include <string>
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(void)
{
Facade *f = new Facade;
f->func();
return 0;
}
四 观察者模式
当对象存在一对多的关系的时候,使用观察者模式,当一个对象被修改的时候,则会自动通知
依赖他的所有对象。
#include <iostream>
#include <map>
#include <string>
#include <list>
using namespace std;
class Infom;
class Observer //观察者 员工
{
public:
virtual void Subscribe(Infom *i) = 0; //订阅
virtual void UnSubscribe(Infom *i) = 0; //取消订阅
virtual void Update(string str) = 0; //更新状态
};
class Infom //通知者 秘书
{
public:
virtual void Add(Observer *o) = 0;
virtual void Remove(Observer *o) = 0;
virtual void Notify(string str) = 0;
};
class Secretary :public Infom
{
private:
list <Observer *> l;
public:
void Add(Observer *o)
{
l.emplace_back(o);
}
void Remove(Observer *o)
{
l.remove(o);
}
void Notify(string str)
{
for (auto &it : l)
{
it->Update(str);
}
}
};
class Staff :public Observer
{
public:
virtual void Subscribe(Infom *i) //订阅
{
i->Add(this);
}
virtual void UnSubscribe(Infom *i) //取消订阅
{
i->Remove(this);
}
virtual void Update(string str) //更新状态
{
if (str == "WARNING")
{
cout << "boss is comming" << endl;
}
else
{
cout << "continue to play games" << endl;
}
}
};
int main(void)
{
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->Notify("WARNING");
return 0;
}