设计模式
1.单例模式
class Singleton {
private:
Singleton(){
m_singer = NULL;
}
public:
static Singleton *GetInstance() {
return m_singer;
}
static void FreeInstatce() {
if (m_singer) {
delete m_singer;
m_singer = NULL;
}
}
private:
static Singleton *m_singer;
};
Singleton *Singleton::m_singer = new Singleton;
int main() {
Singleton *s1 = Singleton::GetInstance();
return 0;
}
class Singleton {
private:
Singleton() {
m_singer = NULL;
}
public:
static Singleton *GetInstance() {
if (!m_singer) {
lock();
if (!m_singer) {
m_singer = new Singleton;
}
unlock();
}
return m_singer;
}
private:
static Singleton *m_singer;
};
Singleton *m_signer = NULL;
2.简单工厂模式
创建一个工厂,根据输入条件的不同,产生不同的类,这些类具有相同的抽象类。
class Fruit {
public:
virtual void getFruit() = 0;
};
class Banana:public Fruit {
public:
void getFruit() {
cout << "Banana!" << endl;
}
};
class Apple :public Fruit {
public:
void getFruit() {
cout << "Apple!" << endl;
}
};
class Factory {
public:
static Fruit *create(const char *name) {
Fruit *tmp = NULL;
if (strcmp(name, "banana") == 0)
tmp = new Banana;
else if (strcmp(name, "apple") == 0)
tmp = new Apple;
else
return NULL;
return tmp;
}
};
int main() {
Fruit *b = Factory::create("banana");
if (!b)
cout << "Creat b err!" << endl;
b->getFruit();
Fruit *a = Factory::create("apple");
if (!a)
cout << "Creat a err!" << endl;
a->getFruit();
return 0;
}
3.工厂模式
与简单工厂模式不同,核心工厂类作为一个抽象角色,负责具体工厂子类必须实现的接口。
class Fruit {
public:
virtual void say() {
cout << "fruit" << endl;
}
};
class FruitFactory {
public:
virtual Fruit *getFruit() {
return new Fruit;
}
};
class Banana :public Fruit {
public:
virtual void say() {
cout << "banana" << endl;
}
};
class BananaFactory :public FruitFactory {
public:
virtual Fruit *getFruit() {
return new Banana;
}
};
class Apple :public Fruit {
public:
virtual void say() {
cout << "apple" << endl;
}
};
class AppleFactory :public FruitFactory {
public:
virtual Fruit *getFruit() {
return new Apple;
}
};
int main() {
Fruit *f = NULL;
FruitFactory *ff = NULL;
ff = new BananaFactory;
f = ff->getFruit();
f->say();
ff = new AppleFactory;
f = ff->getFruit();
f->say();
return 0;
}
4.抽象工厂模式
工厂模式只能生产一个产品,而抽象工厂模式可以生产一个产品组。
class Fruit {
public:
virtual void say() {
cout << "fruit" << endl;
}
};
class FruitFactory {
public:
virtual Fruit *getApple() {
return new Fruit;
}
virtual Fruit *getBanana() {
return new Fruit;
}
};
class SouthBanana :public Fruit {
public:
virtual void say() {
cout << "south banana" << endl;
}
};
class SouthApple :public Fruit {
public:
virtual void say() {
cout << "south apple" << endl;
}
};
class NorthBanana :public Fruit {
public:
virtual void say() {
cout << "north banana" << endl;
}
};
class NorthApple :public Fruit {
public:
virtual void say() {
cout << "north apple" << endl;
}
};
class SouthFactory :public FruitFactory {
virtual Fruit *getApple() {
return new SouthApple;
}
virtual Fruit *getBanana() {
return new SouthBanana;
}
};
class NorthFactory :public FruitFactory {
virtual Fruit *getApple() {
return new NorthApple;
}
virtual Fruit *getBanana() {
return new NorthBanana;
}
};
int main() {
Fruit *f = NULL;
FruitFactory *ff = NULL;
ff = new SouthFactory;
f = ff->getApple();
f->say();
f = ff->getBanana();
f->say();
ff = new NorthFactory;
f = ff->getApple();
f->say();
f = ff->getBanana();
f->say();
return 0;
}
5.建造者模式
一个对象的构建比较复杂,将一个对象的构建和对象的表示进行分离。
class House {
public:
void setFloor(string floor)
{
this->m_floor = floor;
}
void setDoor(string door) {
this->m_door = door;
}
void setWall(string wall) {
this->m_wall = wall;
}
string getFloor() {
return this->m_floor;
}
string getDoor() {
return this->m_door;
}
string getWall() {
return this->m_wall;
}
private:
string m_floor;
string m_door;
string m_wall;
};
class Builder {
public:
virtual void makeFloor() = 0;
virtual void makeDoor() = 0;
virtual void makeWall() = 0;
virtual House *getHouse() = 0;
};
class FlatBuild :public Builder {
public:
FlatBuild() {
this->m_house = new House;
}
virtual void makeFloor() {
m_house->setFloor("Flat Floor");
}
virtual void makeDoor() {
m_house->setDoor("Flat Door");
}
virtual void makeWall() {
m_house->setWall("Flat Floor");
}
virtual House *getHouse() {
return m_house;
}
private:
House * m_house;
};
class VillaBuild :public Builder {
public:
VillaBuild() {
this->m_house = new House;
}
virtual void makeFloor() {
m_house->setFloor("Villa Floor");
}
virtual void makeDoor() {
m_house->setDoor("Villa Door");
}
virtual void makeWall() {
m_house->setWall("Villa Floor");
}
virtual House *getHouse() {
return m_house;
}
private:
House * m_house;
};
class Director {
public:
void construct(Builder *b)
{
b->makeFloor();
b->makeDoor();
b->makeWall();
}
};
int main() {
House *h = NULL;
Builder *b = NULL;
Director *d = NULL;
d = new Director;
b = new FlatBuild;
d->construct(b);
h = b->getHouse();
cout << h->getDoor() <<endl ;
cout << h->getFloor() << endl;
cout << h->getWall() << endl;
b = new VillaBuild;
d->construct(b);
h = b->getHouse();
cout << h->getDoor() << endl;
cout << h->getFloor() << endl;
cout << h->getWall() << endl;
return 0;
}
6.原型模式
一个复杂对象具有自我复制功能,统一一套接口。采用复制原型对象的方法来创建对象的实例。
class Person {
public:
virtual Person *clone() = 0;
virtual void say() = 0;
};
class CppProgrammer:public Person {
public:
CppProgrammer() {
this->m_name = "";
this->m_age = 0;
this->m_resume = NULL;
}
CppProgrammer(string name, int age) {
this->m_name = name;
this->m_age = age;
this->m_resume = NULL;
}
~CppProgrammer() {
if (this->m_resume)
{
delete this->m_resume;
this->m_resume = NULL;
}
}
virtual void say() {
cout << this->m_name << " ," << this->m_age << " ," << this->m_resume << "!" << endl;
}
void setResume(const char *s) {
this->m_resume = new char[strlen(s)+1];
memcpy(this->m_resume, s, strlen(s)+1);
}
virtual Person *clone() {
CppProgrammer *p = new CppProgrammer;
*p = *this;
p->setResume(this->m_resume);
return p;
}
private:
string m_name;
int m_age;
char *m_resume;
};
int main() {
CppProgrammer *p1= new CppProgrammer("jack", 18);
p1->setResume("I'm a Cpp programmer!");
p1->say();
Person *p2 = p1->clone();
delete p1;
p2->say();
return 0;
}
7.代理模式
为其他对象,提供一种代理,以控制对对象的访问。
class Interface {
public:
virtual void Request() = 0;
};
class RealSubject : public Interface{
public:
virtual void Request() {
cout << "真实的请求" << endl;
}
};
class ProxySubject :public Interface {
public:
virtual void Request() {
this->m_realSubject = new RealSubject;
this->m_realSubject->Request();
delete this->m_realSubject;
this->m_realSubject = NULL;
}
private:
RealSubject *m_realSubject;
};
int main() {
ProxySubject *ps = new ProxySubject;
ps->Request();
return 0;
}
8.装饰模式
动态的给一个对象添加一些额外的职责。
class Car {
public:
virtual void show() = 0;
};
class RunCar : public Car{
public:
void run() {
cout << "可以跑" << endl;
}
virtual void show() {
run();
}
};
class SwimCar : public Car {
public:
SwimCar(Car *car)
{
this->m_car = car;
}
void swim() {
cout << "可以游" << endl;
}
virtual void show() {
m_car->show();
swim();
}
private:
Car *m_car;
};
class FlyCar : public Car {
public:
FlyCar(Car *car)
{
this->m_car = car;
}
void fly() {
cout << "可以飞" << endl;
}
virtual void show() {
m_car->show();
fly();
}
private:
Car *m_car;
};
int main() {
Car *run_car = new RunCar;
run_car->show();
cout << endl;
SwimCar *swim_car = new SwimCar(run_car);
swim_car->show();
cout << endl;
FlyCar *fly_car = new FlyCar(swim_car);
fly_car->show();
delete fly_car;
delete swim_car;
delete run_car;
return 0;
}
9.适配器模式
将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的那些类,一起工作。
class Current18v {
public:
void use18vCurrent() {
cout<<"使用18v的交流电"<<endl;
}
};
class Current220v {
public:
void use220vCurrent() {
cout << "使用220v的交流电" << endl;
}
};
class Adapater :public Current18v {
public:
Adapater(Current220v *p_220v)
{
this->m_220v = p_220v;
}
void use18vCurrent() {
cout << "Adapater中使用电流" << endl;
this->m_220v->use220vCurrent();
}
private:
Current220v * m_220v;
};
int main() {
Current220v *p_220v = new Current220v;
Adapater *p_adapater = new Adapater(p_220v);
p_adapater->use18vCurrent();
delete p_adapater;
delete p_220v;
return 0;
}
10.组合模式
单个对象和组合对象的使用具有一致性,将单个对象组合成树形结构以表示“部分–整体”
class IFile {
public:
virtual void display() = 0;
virtual int add(IFile *ifile) = 0;
virtual int remove(IFile *ifile) = 0;
virtual list<IFile *> *getChild() = 0;
};
class File : public IFile {
public:
File(string name) {
this->m_name = name;
m_list = NULL;
}
~File() {
if(m_list != NULL)
delete m_list;
}
virtual void display() {
cout << m_name << endl;
}
virtual int add(IFile *ifile) {
return -1;
}
virtual int remove(IFile *ifile) {
return -1;
}
virtual list<IFile *> *getChild() {
return NULL;
}
private:
list<IFile *> *m_list;
string m_name;
};
class Folder : public IFile {
public:
Folder(string name) {
this->m_name = name;
m_list = new list<IFile *>;
}
~Folder() {
if(m_list != NULL)
delete m_list;
}
virtual void display() {
cout << m_name << endl;
}
virtual int add(IFile *ifile) {
m_list->push_back(ifile);
return 0;
}
virtual int remove(IFile *ifile) {
m_list->remove(ifile);
return 0;
}
virtual list<IFile *> *getChild() {
return m_list;
}
private:
list<IFile *> *m_list;
string m_name;
};
void show_tree(IFile *ifile, int level) {
list<IFile *> *l = NULL;
for (int i = 0; i < level; i++)
printf("\t");
ifile->display();
l = ifile->getChild();
if (l) {
++level;
for (list<IFile *>::const_iterator it = l->begin(); it != l->end(); it++) {
if ((*it)->getChild() == NULL)
{
for (int i = 0; i < level; i++)
printf("\t");
(*it)->display();
}
else {
show_tree((*it), level);
}
}
}
}
int main() {
Folder *root = new Folder("root");
Folder *d1 = new Folder("dir1");
File *f1 = new File("f1");
Folder *d2 = new Folder("dir2");
File *f2 = new File("f2");
root->add(d1);
root->add(d2);
d1->add(f1);
d2->add(f2);
show_tree(root, 0);
delete f2;
delete d2;
delete f1;
delete d1;
delete root;
return 0;
}
11.桥接模式