设计模式2—结构型模式
2023-09-11 14:13:58 时间
结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式:
1. 代理模式(Proxy Pattern)就是为其他对象提供一种代理以控制对这个对象的访问。
2. 装饰者模式(Decorator Pattern)动态的给一个对象添加一些额外的职责。就增加功能来说,此模式比生成子类更为灵活。
3. 适配器模式(Adapter Pattern)是将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
4. 组合模式(Composite Pattern)是将对象组合成树形结构以表示“部分--整体”的层次结构。使得用户对单个对象和组合对象的使用具有一致性。
5. 桥接模式(Bridge Pattern)是将抽象部分与实际部分分离,使它们都可以独立的变化。
6. 外观模式(Facade Pattern)是为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
7. 享元模式(Flyweight Pattern)是以共享的方式高效的支持大量的细粒度的对象。
1. 代理模式(Proxy Pattern)就是为其他对象提供一种代理以控制对这个对象的访问。
2. 装饰者模式(Decorator Pattern)动态的给一个对象添加一些额外的职责。就增加功能来说,此模式比生成子类更为灵活。
3. 适配器模式(Adapter Pattern)是将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
4. 组合模式(Composite Pattern)是将对象组合成树形结构以表示“部分--整体”的层次结构。使得用户对单个对象和组合对象的使用具有一致性。
5. 桥接模式(Bridge Pattern)是将抽象部分与实际部分分离,使它们都可以独立的变化。
6. 外观模式(Facade Pattern)是为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
7. 享元模式(Flyweight Pattern)是以共享的方式高效的支持大量的细粒度的对象。
1. 代理模式
#include <iostream>
#include <stdlib.h>
using namespace std;
//
/*
为其他对象提供一种代理以控制对这个对象的访问。
(就是把一个类注入到另一个类中,也就是作为参数,或者成员)
*/
//
class BookShop //书店
{
public:
virtual ~BookShop()
{
}
virtual void sailBook() = 0; //提供卖书接口
protected:
private:
};
class RealBookShop :public BookShop //实体书店
{
public:
virtual void sailBook()
{
cout << "实体书店卖书" << endl;
}
protected:
private:
};
//
/*
实现方法1 没有继承父类
*/
class DangDangProxy //当当网代理卖书,也卖其他的东西
{
public:
~DangDangProxy()
{
delete book;
book = NULL;
}
void sailBook()
{
book = new RealBookShop; //通过父类的指针来调用子类的方法.
book->sailBook();
sailShose();
}
void sailShose()
{
cout << "代理卖鞋" << endl;
}
protected:
private:
BookShop *book;
};
//
/*
实现方法2 继承父类
继承父类,可以实现通过父类的指针或者引用来调用子类的方法,
*/
class TaoBao :public BookShop
{
public:
~TaoBao()
{
delete book;
book = NULL;
}
virtual void sailBook()
{
book = new RealBookShop; //通过父类的指针来调用子类的方法.
dazhe();
book->sailBook();
dazhe();
}
void dazhe()
{
cout << "节日打折" << endl;
}
protected:
private:
BookShop *book;
};
//
/*
cocos2d 就是使用代理模式
*/
//
class Protocol
{
public:
virtual bool applicationDidFinish() = 0;
protected:
private:
};
class Application :public Protocol
{
public:
Application();
int run();
static Application *pointerApp;
static Application* getPointerApp();
protected:
private:
};
Application* Application::pointerApp = NULL; //静态变量初始化
Application::Application() //构造函数
{
pointerApp = this;//静态变量赋值
}
Application* Application::getPointerApp()
{
return pointerApp;
}
int Application::run()
{
applicationDidFinish();
return 0;
}
class AppDelegate : private Application //注意是private继承
{
public:
virtual bool applicationDidFinish();
protected:
private:
};
bool AppDelegate::applicationDidFinish()
{
cout << "start game" << endl;
return true;
}
int main()
{
cout << "-----------DangDang------------" << endl;
DangDangProxy dangdang;
dangdang.sailBook();
cout << "-----------TaoBao------------" << endl;
BookShop* book = new TaoBao;
book->sailBook();
delete book;
cout << "------------coco2d 入口函数分析------------" << endl;
AppDelegate appdelete;
Application::getPointerApp()->run();
system("pause");
return 0;
}
2. 装饰者模式
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//
/*
装饰( Decorator )模式又叫做包装模式。
通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案。
装饰模式就是把要添加的附加功能分别放在单独的类中,并让这个类包含它要装饰的对象,
当需要执行时,客户端就可以有选择地、按顺序地使用装饰功能包装对象。
其实就是在一个类中声明一个父类的指针,通过父类的指针指向另外一个子类,并调用那个子类的函数
*/
//
class Car //汽车
{
public:
virtual void show() = 0;
virtual ~Car(){}
protected:
private:
};
class RunCar : public Car //跑车
{
public:
RunCar(Car* car = NULL)
{
this->m_car = car;
}
~RunCar()
{
delete m_car;
m_car = NULL;
}
virtual void show()
{
if (m_car != NULL)m_car->show();
run();
}
void run()
{
cout << "可以跑" << endl;
}
protected:
private:
Car *m_car;
};
class SwimCar : public Car
{
public:
SwimCar(Car *car=NULL)
{
this->m_car = car;
}
~SwimCar()
{
delete m_car;
m_car = NULL;
}
void swim()
{
cout << "会游泳" << endl;
}
virtual void show()
{
if (m_car!=NULL)m_car->show();
swim();
}
protected:
private:
Car *m_car;
};
class FlyCar : public Car
{
public:
FlyCar(Car *car)
{
this->m_car = car;
}
~FlyCar()
{
delete m_car;
m_car = NULL;
}
void fly()
{
cout << "会飞行" << endl;
}
virtual void show()
{
if (this->m_car != NULL)m_car->show();
fly();
}
protected:
private:
Car *m_car;
};
int main()
{
Car* myCar = NULL;
myCar = new RunCar;
myCar->show();
cout << "--------------------" << endl;
FlyCar* flyCar = new FlyCar(myCar);
flyCar->show();
cout << "--------------------" << endl;
SwimCar *swimCar = new SwimCar(flyCar);
swimCar->show();
system("pause");
delete swimCar; swimCar = NULL;
delete flyCar; flyCar = NULL;
delete myCar; myCar = NULL;
return 0;
}
3. 适配器模式
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
//
/*
Adapter模式也叫适配器模式,是构造型模式之一,通过Adapter模式可以改变已有类(或外部类)的接口形式。
适用于:
是将一个类的接口转换成客户希望的另外一个接口。
使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
220v 电压 <----相互适配----> 110v 电压
*/
//
class DianYa
{
public:
protected:
int v; //扩展:可以使用v表示电压,改变v的值
};
class Current220V :virtual public DianYa
{
public:
virtual void use220V()
{
cout << "220V 电压" << endl;
}
protected:
private:
};
class Current110V : virtual public DianYa
{
public:
virtual void use110V()
{
cout << "110V 电压" << endl;
}
protected:
private:
};
class Adapter : public Current110V, public Current220V
{
public:
Adapter()
{
m_110V = NULL;
m_220V = NULL;
}
Adapter(Current110V* m_110V)
{
this->m_110V = m_110V;
}
Adapter(Current220V* m_220V)
{
this->m_220V = m_220V;
}
virtual void use110V()
{
if (m_220V != NULL)
{
cout << "适配器 适配 220V" << endl;
m_220V->use220V();
}
else cout << "110V" << endl;
}
virtual void use220V()
{
if (m_110V != NULL)
{
cout << "适配器 适配 110V" << endl;
m_110V->use110V();
}
else cout << "220V" << endl;
}
protected:
private:
Current110V* m_110V;
Current220V* m_220V;
};
int main()
{
Current110V* current110V=new Current110V;
Current220V* current220V=new Current220V;
Adapter* adap = NULL;
adap = new Adapter(current110V); //输入110V
adap->use220V(); //经适配器后,输出220V
delete adap; adap = NULL;
adap = new Adapter(current220V); //输入220V
adap->use110V(); //经适配器后,输出110V
delete adap; adap = NULL;
delete current220V; current220V = NULL;
delete current110V; current110V = NULL;
system("pause");
return 0;
}
4. 组合模式
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <list>
#include <string>
using namespace std;
//
/*
Composite模式也叫组合模式,是构造型的设计模式之一。
通过递归手段来构造树形的对象结构,并可以通过一个对象来访问整个对象树。
*/
//
class IFile
{
public:
virtual void disPaly() = 0;
virtual int add(IFile *ifile) = 0;
virtual int remove(IFile* ifile) = 0;
virtual list<IFile*>* getChild() = 0;
protected:
private:
};
//文件 节点
class File : public IFile
{
public:
File(string filename)
{
this->m_filename = filename;
}
virtual void disPaly()
{
cout << "文件名:" << m_filename << endl;
}
virtual int add(IFile *ifile)
{
return -1;
}
virtual int remove(IFile* ifile)
{
return -1;
}
virtual list<IFile*>* getChild()
{
return NULL;
}
protected:
private:
string m_filename;
};
//目录 节点
class Dir : public IFile
{
public:
Dir(string dirName)
{
this->m_dirName = dirName;
this->m_list = new list < IFile* > ;
m_list->clear();
}
virtual void disPaly()
{
cout << "文件夹名:" << m_dirName << 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;
}
protected:
private:
string m_dirName;
list<IFile*>* m_list;
};
// level用来控制显示层次结构
void showTree(IFile* root,int level)
{
int i = 0;
if (root == NULL)return;
for (i = 0; i < level; i++)cout << "\t";
root->disPaly(); //显示根节点
//2 若根结点 有孩子
//判读孩子是文件,显示名字 )
//判断孩子是目录,showTree(子目录)
list<IFile*> *myList = root->getChild();
if (myList != NULL) //说明是一个目录
{
for (list<IFile*>::iterator it = myList->begin(); it != myList->end();it++)
{
if ((*it)->getChild() == NULL) //文件直接打印
{
for (i = 0; i <= level; i++) //注意 <=
{
printf("\t");
}
(*it)->disPaly();
}
else
{
//目录再递归调用,层次加1
showTree(*it, level+1);
}
}
}
}
int main()
{
Dir* root = new Dir("C");
root->disPaly();
File *fileAAA = new File("aaa.txt");
File *fileBBB = new File("bbb.txt");
File *fileCCC = new File("ccc.txt");
Dir *dir001 = new Dir("001");
Dir *dir002 = new Dir("002");
Dir* dir003 = new Dir("003");
root->add(dir001);
root->add(dir002);
root->add(fileAAA);
dir002->add(dir003);
dir002->add(fileBBB);
dir003->add(fileCCC);
list<IFile*>* myList = root->getChild();
for (list<IFile *>::iterator it = myList->begin(); it != myList->end(); it++)
{
(*it)->disPaly();
}
cout << "通过 showTree 方式 显示 root 结点下的 所有子结点" << endl;
showTree(root, 0);
system("pause");
return 0;
}
5. 桥接模式
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
//
/*
桥接模式 : 就是多对多的关系;
发动机有多种型号,汽车也有多个品牌
每个型号的发动机可以被各个型号的汽车使用
每个品牌的汽车也可以使用各个型号的发动机
等于在中间架设一座桥,(在抽象类中声明另一个抽象类的指针,用来调用另一个抽象类子类的方法)
*/
//
class Car; //前向声明
class Engine
{
public:
virtual void engineType() = 0;
protected:
Car* car;
private:
};
class Type2900 :public Engine
{
public:
virtual void engineType()
{
cout << "2900 型号发动机" << endl;
}
protected:
private:
};
class Type3500 :public Engine
{
public:
virtual void engineType()
{
cout << "3500 型号发动机" << endl;
}
protected:
private:
};
class Type7200 :public Engine
{
public:
virtual void engineType()
{
cout << "7200 型号发动机" << endl;
}
protected:
private:
};
class Car
{
public:
virtual void carType() = 0;
Car(Engine* engine)
{
this->m_engine = engine;
}
protected:
Engine *m_engine;
private:
};
class BMW : public Car
{
public:
BMW(Engine* engine) :Car(engine){}
virtual void carType()
{
cout << "BMW Car: ";
m_engine->engineType();
}
protected:
private:
};
class Jeep : public Car
{
public:
Jeep(Engine* engine) :Car(engine){}
virtual void carType()
{
cout << "Jeep Car: ";
m_engine->engineType();
}
protected:
private:
};
int main()
{
Engine *engine = NULL;
Car* car = NULL;
engine = new Type2900;
car = new BMW(engine);
car->carType();
delete car; car = NULL;
delete engine; engine = NULL;
engine = new Type3500;
car = new BMW(engine);
car->carType();
delete car; car = NULL;
delete engine; engine = NULL;
engine = new Type3500;
car = new Jeep(engine);
car->carType();
delete car; car = NULL;
delete engine; engine = NULL;
system("pause");
return 0;
}
6. 外观模式
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
//
/*
外观模式就是封装了一层外壳,提供统一的操作界面
也就是把“操作相似的类” 注入到另一个类里面,提供统一操作
*/
//
class SubSystemA
{
public:
void doSomething()
{
cout << "SubSystemA run" << endl;
}
protected:
private:
};
class SubSystemB
{
public:
void doSomething()
{
cout << "SubSystemB run" << endl;
}
protected:
private:
};
class SubSystemC
{
public:
void doSomething()
{
cout << "SubSystemC run" << endl;
}
protected:
private:
};
class Facade
{
public:
Facade()
{
sysA = new SubSystemA;
sysB = new SubSystemB;
sysC = new SubSystemC;
}
~Facade()
{
delete sysA;
delete sysB;
delete sysC;
}
void doSomething()
{
sysA->doSomething();
sysB->doSomething();
sysC->doSomething();
}
protected:
private:
SubSystemA *sysA;
SubSystemB *sysB;
SubSystemC *sysC;
};
int main()
{
cout << "没有Facade类时的调用方式" << endl;
SubSystemA *sysA = new SubSystemA;
SubSystemB *sysB = new SubSystemB;
SubSystemC *sysC = new SubSystemC;
sysA->doSomething();
sysB->doSomething();
sysC->doSomething();
delete sysA;
delete sysB;
delete sysC;
cout << "使用Facade类的调用方式" << endl;
Facade *f = new Facade;
f->doSomething();
delete f;
system("pause");
}
7. 享元模式
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <map>
#include <string>
using namespace std;
//
/*
Flyweight模式也叫享元模式,是构造型模式之一,
它通过与其他类似对象共享数据来减小内存占用。
所谓的享元模式“就是有相同的不用管,没有相同的继续添加”。
只需要在添加的时候判断下有没有相同的。
*/
//
class Person
{
public:
Person(string name, int age) :m_name(name), m_age(age){}
virtual void printT() = 0;
protected:
string m_name;
int m_age;
private:
};
class Teacher : public Person
{
public:
Teacher(string name, int age, string id) :Person(name, age),m_id(id){}
virtual void printT()
{
cout << "name:" << m_name << " age:" << m_age << " m_id:" << m_id << endl;
}
protected:
private:
string m_id;
};
class FlyWeightTeacher
{
public:
FlyWeightTeacher()
{
mapTeachers.clear();
}
~FlyWeightTeacher()
{
while (!mapTeachers.empty())
{
Person *tmp = NULL;
map<string, Person *>::iterator it = mapTeachers.begin();
tmp = it->second;
mapTeachers.erase(it); //把第一个结点 从容器中删除
delete tmp;
}
}
Person* getTeacher(string id)
{
Person* tmp = NULL;
map<string, Person*>::iterator it;
it = mapTeachers.find(id);
if (it == mapTeachers.end()) //没有找到
{
string tmpName;
int tmpAge;
cout << "\n输入老师的Name:";
cin >> tmpName;
cout << "\n输入老师的Age:";
cin >> tmpAge;
tmp = new Teacher(tmpName,tmpAge,id);
mapTeachers.insert(pair<string,Person*>(id,tmp));
}
else
{
tmp = it->second;
}
return tmp;
}
protected:
private:
map<string, Person*> mapTeachers;
};
int main()
{
Person *p1 = NULL;
Person *p2 = NULL;
FlyWeightTeacher * fwty = new FlyWeightTeacher;
p1->printT();
p2 = fwty->getTeacher("001");
p2->printT();
delete fwty;
system("pause");
return 0;
}
相关文章
- 设计模式之原型模式
- 看透设计模式-结构型模式
- 设计模式(十)享元模式Flyweight(结构型)
- 设计模式(五)适配器模式Adapter(结构型)
- C#设计模式(17)——观察者模式(Observer Pattern)
- 不一样的中介模式(设计模式十四)
- java设计模式三种工厂模式简单介绍
- C#设计模式——职责链模式(Chain Of Responsibility Pattern)
- C#设计模式——单件模式(Singleton Pattern)
- 设计模式 -- 组合模式 (Composite Pattern)
- 《大话设计模式》--模板模式
- 不一样的工厂模式(设计模式六)
- Java设计模式之双向责任链COR模式
- windows程序设计(五)---映射模式(转换设备坐标和逻辑坐标)
- 简单工厂模式(设计模式02)
- C# 观察者模式.
- Atitit 反模式 黑名单 异常处理 反模式(antipatterns) 目录 1.1. 记录并抛出(log and throw)1 1.2. 抛出异常基类(Throwing Excepti
- C++设计模式之策略模式
- 实践GoF的设计模式:工厂方法模式
- Linux命令: 编辑模式移动光标
- (三十四)设计模式之发布订阅模式
- 浅析JAVA设计模式之工厂模式(一)
- 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)
- 使用Lombok和建造者模式简化代码
- PHP设计模式-策略模式 转
- 中介模式C++实现
- 保留状态的秘密 - 深入了解Java备忘录模式(Memento Pattern)
- 23种类设计模式--2原型模式