设计模式C++实现(3)——适配器模式
适配器(Adaptor)模式

对象适配器
Client ExistingClass RequiredClass ______________ requiredMethod() __________ usefulMethod()
NewClass ________ requiredMethod() Newclass类的实例也是RequiredClass的实例,在其内部通过产生 的Existingclass的实例来适配usefulMethod方法
类适配器简单例子
public interface Print { public abstract void printWeak(); public abstract void printStrong(); } -------------------------public class Banner { private String string; public Banner(String string) { this.string = string; } public void showWithParen() { System.out.println("(" + string + ")"); } public void showWithAster() { System.out.println("*" + string + "*"); } }
OozimozRocket
ห้องสมุดไป่ตู้
public class OozinozRocket extends PhysicalRocket implements RocketSim{ private double time; Public OozinozRocket(double burnArea,double burnRate,double fuelMass,double totalMass){ super(burnArea,burnRate,fuelMass,totalMass); } public double getMass(){ } public double getThrust(){ } public void setSimTime(double time){this.time=time;} }
中介者模式和适配器模式的区别

中介者模式和适配器模式的区别中介者模式和适配器模式是设计模式中的两个重要概念。
它们在软件开发中都有着广泛的应用,但是它们在功能上有着很大的区别。
本文将从不同的角度,深入探讨这两种模式的定义、实现方式、使用场景等方面的区别与联系。
一、定义中介者模式是指通过一个中介对象来封装一系列的对象交互,从而使原来的对象不再相互引用,而是通过中介者对象来完成相互的交互。
适配器模式则是指将一个类的接口转换成客户希望的另外一个接口,从而使原本由于接口不兼容而无法在一起工作的两个类能够在一起工作。
二、实现方式1. 中介者模式的实现中介者模式的实现方式通常包含两个角色:中介者角色和同事角色。
其中,中介者角色通常负责协调同事角色之间的互动关系,而同事角色则负责执行具体的任务。
中介者模式的核心思想是将各个同事对象解耦,让它们不需要知道彼此的存在中介者模式可以使用观察者模式来实现,即将中介者模式作为被观察者,同事对象作为观察者。
2. 适配器模式的实现适配器模式的实现方式通常包含三个角色:目标角色、适配器角色和被适配者角色。
其中,目标角色是客户端希望使用的接口,被适配者角色是已有的接口,适配器角色则是在两者之间进行接口的转换。
适配器模式的实现核心思想就是将原本不兼容的接口进行转换,让它们能够协同工作。
适配器模式通常有两种实现方式:类适配器和对象适配器。
类适配器是通过多重继承实现,而对象适配器则是通过组合来实现。
三、使用场景1. 中介者模式的使用场景中介者模式通常适用于大型、复杂的系统中,系统中有许多对象需要互相通信,但是它们之间的关系比较复杂。
中介者模式可以将这些对象的关系进行解耦,从而达到简化系统的目的。
中介者模式还适用于系统中的对象出现频繁的变化,如果每个对象的变化都会对其他对象产生影响,那么这个时候可以采用中介者模式来将这些对象的变化隔离开来。
2. 适配器模式的使用场景适配器模式通常适用于已有的接口无法满足客户端的需求的情况下。
C#23种设计模式汇总

C#23种设计模式汇总创建型模式在⼯⼚⽅法模式中,⼯⼚⽅法⽤来创建客户所需要的产品,同时还向客户隐藏了哪种具体产品类将被实例化这⼀细节。
⼯⼚⽅法模式的核⼼是⼀个抽象⼯⼚类,各种具体⼯⼚类通过抽象⼯⼚类将⼯⼚⽅法继承下来。
如此使得客户可以只关⼼抽象产品和抽象⼯⼚,完全不⽤理会返回的是哪⼀种具体产品,也不⽤关系它是如何被具体⼯⼚创建的。
抽象⼯⼚模式的主要优点是隔离了具体类的⽣成,使得客户不需要知道什么被创建了。
犹豫这种隔离,更换⼀个具体⼯⼚就变得相对容易。
所有的具体⼯⼚都实现了抽象⼯⼚中定义的那些公共接⼝,因此只需改变具体⼯⼚的实例,就可以在某种程度上改变这个软件的系统的⾏为。
另外,应⽤抽象⼯⼚模式符合GRASP纯虚构的模式,可以实现⾼内聚低耦合的设计⽬的,因此抽象⼯⼚模式得到了⼴泛应⽤。
建造者模式将⼀个复杂对象的⽣成责任作了很好的分配。
它把构造过程放在指挥者的⽅法中,把装配过程放到具体建造者类中。
建造者模式的产品之间都有共通点,但有时候,产品之间的差异性很⼤,这就需要借助⼯⼚⽅法模式或抽象⼯⼚模式。
另外,如果产品的内部变化复杂,Builder的每⼀个⼦类都需要对应到不同的产品去做构建的动作、⽅法,这就需要定义很多个具体建造类来实现这种变化。
Singleton单例模式为⼀个⾯向对象的应⽤程序提供了对象唯⼀的访问点,不管它实现何种功能,此种模式都为设计及开发团队提供了共享的概念。
然⽽,Singleton对象类派⽣⼦类就有很⼤的困难,只有在⽗类没有被实例化时才可以实现。
值得注意的是,有些对象不可以做成Singleton,⽐如.net的数据库链接对象(Connection),整个应⽤程序同享⼀个Connection对象会出现连接池溢出错误。
另外,.net提供了⾃动废物回收的技术,因此,如果实例化的对象长时间不被利⽤,系统会认为它是废物,⾃动消灭它并回收它的资源,下次利⽤时⼜会重新实例化,这种情况下应注意其状态的丢失。
第7次实验--适配器、桥接模式设计模式实验

实验7—适配器模式和桥接模式实验
专业软件工程班级java2班实验日期:2015 年4月3日报告退发(订正、重做) 课程:体系结构与设计实验名称:适配器模式和桥接模式
学号:123012012137 姓名:张超红
实验目的:加深对适配器模式和桥接设计模式原理的理解
实验环境:C#.Net/VC++.Net或MyEclipse(Java)等
演示内容:算法适配
现有一个接口DataOperation定义了排序方法Sort(int[])和查找方法search(int[],int),已知类QuickSort的quickSort(int[])方法实现了快速排序,类BinarySearch的binarySearch(int[],int)方法实现了二分查找算法,现使用适配器模式设计一个系统,在不修改源码的情况下将类QuickSort和类BinarySearch的方法适配到DataOperation接口中。
绘制类图并编程实现。
⑴实验过程:
①、构建实现场景,画出UML类图
②、实现代码,见演示源码
实验内容(一):
修改实例仿生机器人,使得机器人可以像鸟一样叫,并像狗一样的跑,请绘制类图并
编程实现。
⑴实验过程:
①、构建实现场景,画出UML类图
②、实现代码
⑵实验讨论(效果分析):
实验内容(二):
如果系统中某对象有三个维度,如某日志记录器既可以支持不同的操作系统,还可以支持多种编程语言,并且可以使用不同的输出方式。
请使用桥接模式设计该系统。
⑴实验过程:
①构建实现场景,画出UML类图
②实现代码
⑵实验讨论(效果分析):。
17种设计模式代码

1、适配器模式adapter#include <iostream>using namespace std;class Deque{public:void push_back(int x) { cout<<"Deque push_back"<<endl; } void push_front(int x) { cout<<"Deque push_front"<<endl; } void pop_back() { cout<<"Deque pop_back"<<endl; }void pop_front() { cout<<"Deque pop_front"<<endl; }};//顺序容器class Sequence{public:virtual void push(int x) = 0;virtual void pop() = 0;};//栈class Stack: public Sequence{public:void push(int x) { deque.push_back(x); }void pop() { deque.pop_back(); }private:Deque deque; //双端队列};//队列class Queue: public Sequence{public:void push(int x) { deque.push_back(x); }void pop() { deque.pop_front(); }private:Deque deque; //双端队列};int main(){Sequence *s1 = new Stack();Sequence *s2 = new Queue();s1->push(1); s1->pop();s2->push(1); s2->pop();delete s1; delete s2;return 0;}2、建造者模式building#include <iostream>using namespace std;class Builder{public:virtual void BuildHead() {}virtual void BuildBody() {}virtual void BuildLeftArm(){}virtual void BuildRightArm() {}virtual void BuildLeftLeg() {}virtual void BuildRightLeg() {}};//构造瘦人class ThinBuilder : public Builder{public:void BuildHead() { cout<<"build thin body"<<endl; }void BuildBody() { cout<<"build thin head"<<endl; }void BuildLeftArm() { cout<<"build thin leftarm"<<endl; }void BuildRightArm() { cout<<"build thin rightarm"<<endl; } void BuildLeftLeg() { cout<<"build thin leftleg"<<endl; }void BuildRightLeg() { cout<<"build thin rightleg"<<endl; } };//构造胖人class FatBuilder : public Builder{public:void BuildHead() { cout<<"build fat body"<<endl; }void BuildBody() { cout<<"build fat head"<<endl; }void BuildLeftArm() { cout<<"build fat leftarm"<<endl; }void BuildRightArm() { cout<<"build fat rightarm"<<endl; } void BuildLeftLeg() { cout<<"build fat leftleg"<<endl; }void BuildRightLeg() { cout<<"build fat rightleg"<<endl; } };//构造的指挥官class Director{private:Builder *m_pBuilder;public:Director(Builder *builder) { m_pBuilder = builder; }void Create(){m_pBuilder->BuildHead();m_pBuilder->BuildBody();m_pBuilder->BuildLeftArm();m_pBuilder->BuildRightArm();m_pBuilder->BuildLeftLeg();m_pBuilder->BuildRightLeg();}};int main(){FatBuilder thin;Director director(&thin);director.Create();return 0;}3、策略模式#include <iostream>using namespace std;class COperation{ public:int m_nFirst;int m_nSecond;virtual double GetResult(){double dResult=0;return dResult;}};//策略具体类—加法类class AddOperation : public COperation {public:AddOperation(int a,int b){m_nFirst=a;m_nSecond=b;}virtual double GetResult(){return m_nFirst+m_nSecond;}};class SubstrOperation : public COperation{public:SubstrOperation(int a,int b){m_nFirst=a;m_nSecond=b;}virtual double GetResult(){return m_nFirst-m_nSecond;}};class Context{private:COperation* op;public:Context(COperation* temp){op=temp;}double GetResult(){return op->GetResult();}};//客户端int main(){int a,b;char c;cin >> a >> b;cout<<"请输入运算符";cin>>c;switch(c){case '+':{Context *context1=new Context(new AddOperation(a,b));cout<< context1->GetResult() << endl;break;}case '-':{Context *context2=new Context(new SubstrOperation(a,b));cout<< context2->GetResult() << endl;break;}}return 0;}4、抽象模式#include <iostream>class Button{public:virtual void paint() = 0;};class WinButton : public Button{public:void paint (){std::cout << " Window Button \n";}};class MacButton : public Button{public:void paint (){std::cout << " Mac Button \n";}};class ScrollBar{public:virtual void paint() = 0;};class WinScrollBar : public ScrollBar{public:void paint (){std::cout << " Window ScrollBar \n";}};class MacScrollBar : public ScrollBar { public:void paint (){std::cout << " Mac ScrollBar \n";}};class GUIFactory{public:virtual Button* createButton () = 0;virtual ScrollBar* createScrollBar () = 0; };class WinFactory : public GUIFactory{public:Button* createButton (){return new WinButton;}ScrollBar* createScrollBar (){return new WinScrollBar;}};class MacFactory : public GUIFactory{public:Button* createButton (){return new MacButton;}ScrollBar* createScrollBar (){return new MacScrollBar;}};int main(){GUIFactory* guiFactory;Button *btn;ScrollBar *sb;guiFactory = new MacFactory;btn = guiFactory->createButton();btn -> paint();sb = guiFactory->createScrollBar();sb -> paint();guiFactory = new WinFactory;btn = guiFactory->createButton();btn -> paint();sb = guiFactory->createScrollBar();sb -> paint();return 0;}5、代理模式#include <iostream>#include <string>using namespace std;class BigImage{public:BigImage(string name): m_imageName(name) {} virtual ~BigImage() {}virtual void Show() {}protected:string m_imageName;};//真实类class RealBigImage: public BigImage{public:RealBigImage(string name):BigImage(name) {}~RealBigImage() {}void Show(){cout<<"Show big image : "<<m_imageName<<endl;}};//代理class Proxy: public BigImage{private:RealBigImage *m_bigImage;public:Proxy(string name):BigImage(name),m_bigImage(0){}~Proxy(){delete m_bigImage;}void Show(){if(m_bigImage == NULL){cout<<"please wait ..."<<endl;m_bigImage = new RealBigImage(m_imageName); //代理创建真实对象}m_bigImage->Show();}};int main(){BigImage *image = new Proxy("SomeBigPic.jpg"); //使用代理image->Show(); //代理的操作delete image;return 0;}6、非享元模式#include <iostream>#include <vector>#include <string>using namespace std;//棋子颜色enum PieceColor {BLACK, WHITE};//棋子位置struct PiecePos{int x;int y;PiecePos(int a, int b): x(a), y(b) {}};//棋子定义class Piece{protected:PieceColor m_color; //颜色PiecePos m_pos; //位置public:Piece(PieceColor color, PiecePos pos): m_color(color), m_pos(pos) {} ~Piece() {}virtual void Draw() {}};class BlackPiece: public Piece{public:BlackPiece(PieceColor color, PiecePos pos): Piece(color, pos) {}~BlackPiece() {}void Draw() { cout<<"绘制一颗黑棋"<<endl;}};class WhitePiece: public Piece{public:WhitePiece(PieceColor color, PiecePos pos): Piece(color, pos) {}~WhitePiece() {}void Draw() { cout<<"绘制一颗白棋"<<endl;}};class PieceBoard{private:vector<Piece*> m_vecPiece; //棋盘上已有的棋子string m_blackName; //黑方名称string m_whiteName; //白方名称public:PieceBoard(string black, string white): m_blackName(black), m_whiteName(white){} ~PieceBoard() { Clear(); }void SetPiece(PieceColor color, PiecePos pos) //一步棋,在棋盘上放一颗棋子{Piece * piece = NULL;if(color == BLACK) //黑方下的{piece = new BlackPiece(color, pos); //获取一颗黑棋cout<<m_blackName<<"在位置("<<pos.x<<','<<pos.y<<")";piece->Draw(); //在棋盘上绘制出棋子}else{piece = new WhitePiece(color, pos);cout << m_whiteName<<"在位置("<<pos.x<<','<<pos.y<<")";piece->Draw();}m_vecPiece.push_back(piece); //加入容器中}void Clear() //释放内存{int size = m_vecPiece.size();for(int i = 0; i < size; i++)delete m_vecPiece[i];}};int main(){PieceBoard pieceBoard("A","B");pieceBoard.SetPiece(BLACK, PiecePos(4, 4));pieceBoard.SetPiece(WHITE, PiecePos(4, 16));pieceBoard.SetPiece(BLACK, PiecePos(16, 4));pieceBoard.SetPiece(WHITE, PiecePos(16, 16));return 0;}7、工厂方法#include <iostream>using namespace std;class Button{public:virtual void paint() = 0;};class OSXButton: public Button{public:void paint(){cout << "OSX button \n";}};class WindowsButton: public Button {public:void paint(){cout << "Windows button \n";}};class GUIFactory{public:virtual Button *createButton() = 0; };class Factory1: public GUIFactory{public:Button *createButton(){return new WindowsButton;}};class Factory2: public GUIFactory{public:Button *createButton(){return new OSXButton;}};int main(){GUIFactory* guiFactory1,* guiFactory2;Button *btn1,*btn2;guiFactory1 = new Factory1;btn1 = guiFactory1->createButton();btn1 -> paint();guiFactory2 = new Factory2;btn2 = guiFactory2->createButton();btn2 -> paint();return 0;}8、观察者模式#include <iostream>#include <list>#include <string>using namespace std;class Observer{public:Observer() {}virtual ~Observer() {}virtual void Update() {}};//博客class Blog{public:Blog() {}virtual ~Blog() {}void Attach(Observer *observer) { m_observers.push_back(observer); } //添加观察者void Remove(Observer *observer) { m_observers.remove(observer); } //移除观察者void Notify() //通知观察者{list<Observer*>::iterator iter = m_observers.begin();for(; iter != m_observers.end(); iter++)(*iter)->Update();}virtual void SetStatus(string s) { m_status = s; } //设置状态virtual string GetStatus() { return m_status; } //获得状态private:list<Observer* > m_observers; //观察者链表protected:string m_status; //状态};//具体博客类class BlogCSDN : public Blog{private:string m_name; //博主名称public:BlogCSDN(string name): m_name(name) {}~BlogCSDN() {}void SetStatus(string s) { m_status = "CSDN通知: " + m_name + s; } //具体设置状态信息string GetStatus() { return m_status; }};//具体观察者class ObserverBlog : public Observer{private:string m_name; //观察者名称Blog *m_blog; //观察的博客,当然以链表形式更好,就可以观察多个博客public:ObserverBlog(string name,Blog *blog): m_name(name), m_blog(blog) {}~ObserverBlog() {}void Update() //获得更新状态{string status = m_blog->GetStatus();cout<<m_name<<"-------"<<status<<endl;}};//测试案例int main(){Blog *blog = new BlogCSDN("wuzhekai1985");Observer *observer1 = new ObserverBlog("tutupig", blog);blog->Attach(observer1);blog->SetStatus("发表设计模式C++实现(15)——观察者模式");blog->Notify();delete blog; delete observer1;return 0;}9、命令模式#include <iostream>#include <vector>using namespace std;class Command{public:virtual void execute() = 0;};class NoCommand : public Command{public:void execute() {};};class Light{public:Light(string location);void on();void off();private:string m_sLocation;};class LightOffCommand : public Command{public:LightOffCommand(string location):m_Light(location) {}void execute();private:Light m_Light;};class LightOnCommand : public Command{public:LightOnCommand(string location):m_Light(location) {}void execute();private:Light m_Light;};class Stereo{public:Stereo(string location);void on();void off();void setCD();void setDVD();void setRadio();void setVolume(int volume);private:string m_sLocation;};class StereoOnWithCDCommand : public Command{ public:StereoOnWithCDCommand(string location):m_Stereo(location) {}void execute();private:Stereo m_Stereo;};class StereoOffCommand : public Command{ public:StereoOffCommand(string location):m_Stereo(location) {}void execute();private:Stereo m_Stereo;};class RemoteControl{ public:RemoteControl();~RemoteControl();void setCommand(int slot, Command* pOnCommand, Command* pOffCommand);void onButtonWasPushed(int slot);void offButtonWasPushed(int slot);private:vector<Command*> m_OnCommands;vector<Command*> m_OffCommands;};Light::Light(string location){m_sLocation = location;}void Light::on(){printf("%s light is on\n",m_sLocation.c_str());}void Light::off(){printf("%s light is off\n",m_sLocation.c_str());}void LightOffCommand::execute(){m_Light.off();}void LightOnCommand::execute(){m_Light.on();}Stereo::Stereo(string location){m_sLocation = location;}void Stereo::on(){printf("%s stereo is on\n",m_sLocation.c_str());}void Stereo::off(){ printf("%s stereo is off\n",m_sLocation.c_str());}void Stereo::setCD(){printf("%s stereo is set for CD input\n",m_sLocation.c_str());}void Stereo::setDVD(){printf("%s stereo is set for DVD input\n",m_sLocation.c_str());}void Stereo::setRadio(){printf("%s stereo is set for Radio\n",m_sLocation.c_str());}void Stereo::setVolume(int volume){printf("%s Stereo volume set to %d\n",m_sLocation.c_str(),volume); }void StereoOnWithCDCommand::execute(){m_Stereo.on();m_Stereo.setCD();m_Stereo.setVolume(11);}void StereoOffCommand::execute(){m_Stereo.off();}RemoteControl::RemoteControl(){for (int i = 0; i < 7; i++){Command* noCommandOn = new NoCommand();m_OnCommands.push_back(noCommandOn);Command* noCommandOff = new NoCommand();m_OffCommands.push_back(noCommandOff);}}RemoteControl::~RemoteControl(){for (int i = 0; i < 7; i++){delete m_OnCommands.at(i);delete m_OffCommands.at(i);}m_OnCommands.clear();m_OffCommands.clear();}void RemoteControl::setCommand(int slot, Command* pOnCommand, Command* pOffCommand){delete m_OnCommands.at(slot);m_OnCommands.at(slot) = pOnCommand;delete m_OffCommands.at(slot);m_OffCommands.at(slot) = pOffCommand;}void RemoteControl::onButtonWasPushed(int slot){m_OnCommands.at(slot)->execute();}void RemoteControl::offButtonWasPushed(int slot){m_OffCommands.at(slot)->execute();}int main(){RemoteControl remoteControl;LightOffCommand* pLivingRoomLightOff = new LightOffCommand("Living Room");LightOffCommand* pKitchenLightOff = new LightOffCommand("Kitchen");LightOnCommand* pLivingRoomLightOn = new LightOnCommand("Living Room");LightOnCommand* pKitchenLightOn = new LightOnCommand("Kitchen");StereoOnWithCDCommand* pStereoOnWithCD = new StereoOnWithCDCommand("Living Room");StereoOffCommand* pStereoOff = new StereoOffCommand("Living Room");remoteControl.setCommand(0,pLivingRoomLightOn,pLivingRoomLightOff);remoteControl.setCommand(1,pKitchenLightOn,pKitchenLightOff);remoteControl.setCommand(2,pStereoOnWithCD,pStereoOff);remoteControl.onButtonWasPushed(0);remoteControl.offButtonWasPushed(0);remoteControl.onButtonWasPushed(1);remoteControl.offButtonWasPushed(1);remoteControl.onButtonWasPushed(2);remoteControl.offButtonWasPushed(2);return 0;}10、桥接模式#include <iostream>using namespace std;class OS{public:virtual void InstallOS_Imp() {}};class WindowOS: public OS{public:void InstallOS_Imp() { cout<<"安装Window操作系统"<<endl; }};class LinuxOS: public OS{public:void InstallOS_Imp() { cout<<"安装Linux操作系统"<<endl; }};class UnixOS: public OS{public:void InstallOS_Imp() { cout<<"安装Unix操作系统"<<endl; } };//计算机class Computer{public:virtual void InstallOS(OS *os) {}};class DellComputer: public Computer{public:void InstallOS(OS *os) { os->InstallOS_Imp(); }};class AppleComputer: public Computer{public:void InstallOS(OS *os) { os->InstallOS_Imp(); }};class HPComputer: public Computer{public:void InstallOS(OS *os) { os->InstallOS_Imp(); }};int main(){OS *os1 = new WindowOS();OS *os2 = new LinuxOS();Computer *computer1 = new AppleComputer();computer1->InstallOS(os1);computer1->InstallOS(os2);}11、外观模式#include <iostream>using namespace std;class Scanner{public:void Scan() { cout<<"词法分析"<<endl; }};class Parser{public:void Parse() { cout<<"语法分析"<<endl; }};class GenMidCode{public:void GenCode() { cout<<"产生中间代码"<<endl; } };class GenMachineCode{public:void GenCode() { cout<<"产生机器码"<<endl;} };//高层接口class Compiler{public:void Run(){Scanner scanner;Parser parser;GenMidCode genMidCode;GenMachineCode genMacCode;scanner.Scan();parser.Parse();genMidCode.GenCode();genMacCode.GenCode();}};int main(){Compiler compiler;compiler.Run();return 0;}12、享元模式#include <vector>#include <iostream>#include <conio.h>#include <string>using namespace std;class Flyweight{public:Flyweight(){}virtual ~Flyweight(){}virtual void Operation(const string extrinsicState) = 0;//接受客户的外部状态};class ConcreteFlyweight : public Flyweight{public:char _intrinsicState;ConcreteFlyweight(char intrinsicState){this->_intrinsicState = intrinsicState;cout<<intrinsicState<<" is created!"<<endl;}~ConcreteFlyweight(){}char GetIntrinsicState(){return this->_intrinsicState;}//返回内部状态变量void Operation(const string extrinsicState){if (!extrinsicState.empty()){cout<<"<"<<extrinsicState<<"> "<<_intrinsicState<<endl;}elsecout<<"<Normal> "<<_intrinsicState<<endl;}};class FlyweightFactory{public://作为对象池,存贮共享的Flyweight对象,也可用hash表等其它结构vector <ConcreteFlyweight *>_flyVector;FlyweightFactory(){}~FlyweightFactory(){}Flyweight * GetFlyweight(char key){//引入迭代器实现按顺序访问池中的各Flyweight对象vector <ConcreteFlyweight *>:: iterator it = _flyVector.begin();ConcreteFlyweight * flyTemp;if (_flyVector.size() == 0)//当容器内无任何对象时,先push一个对象{flyTemp = new ConcreteFlyweight(key);_flyVector.push_back(flyTemp);return flyTemp;}for (; it != _flyVector.end(); it++)//遍历对象{if ((*it)->GetIntrinsicState() == key)//若存在{cout<<key<<" is already existed.."<<endl;return *it;}flyTemp = new ConcreteFlyweight(key);_flyVector.push_back(flyTemp);return flyTemp;}}};int main(){FlyweightFactory * fac1 = new FlyweightFactory();Flyweight *fly1 = fac1->GetFlyweight('a');Flyweight *fly2 = fac1->GetFlyweight('b');Flyweight *fly3 = fac1->GetFlyweight('a');fly1->Operation("BigSize");fly2->Operation("");fly3->Operation("Rotated");getch();/*等待按键继续*/return 0;}13、修饰模式#include <iostream>#include <string>using namespace std;class Person{public:Person(){}Person(string str):name(str){}string GetName(){return name;}virtual void Show(){cout << "装扮的" << GetName() << endl;}private:string name;};// 装饰类class PStyle : public Person{public:void Decorator(Person *conponent){this->person = conponent;}void Show(){if (person != NULL){person->Show();}}protected:Person *person; // 记住,动态的条用虚函数必须通过对象的指针或者引用};// 具体装饰class TShirt : public PStyle{public:void Show(){cout << " T恤";PStyle::Show();}};// 具体装饰class Throuse : public PStyle{public:void Show(){cout << " c长裤子";PStyle::Show();}};class Sweter : public PStyle{public:void Show(){cout << " 大毛衣";PStyle::Show();}};void main(){Person xiaocai("小菜");TShirt tx;Throuse th;Sweter sw;tx.Decorator(&xiaocai);th.Decorator(&tx);sw.Decorator(&th);sw.Show();}14、原型模式#include <iostream>using namespace std;class Resume{protected:char *name;public:Resume() {}virtual ~Resume() {}virtual Resume* Clone(){ return NULL; }virtual void Set(char *n) {}virtual void Show() {}};class ResumeA : public Resume{public:ResumeA(const char *str); //构造函数ResumeA(const ResumeA &r); //拷贝构造函数~ResumeA(); //析构函数Resume* Clone(); //克隆,关键所在void Show(); //显示内容};ResumeA::ResumeA(const char *str){if(str == NULL) {name = new char[1];name[0] = '\0';}else {name = new char[strlen(str)+1];strcpy(name, str);}}ResumeA::~ResumeA() { delete [] name;} ResumeA::ResumeA(const ResumeA &r) {name = new char[strlen()+1];strcpy(name, );}Resume* ResumeA::Clone() {return new ResumeA(*this);}void ResumeA::Show() {cout<<"ResumeA name : "<<name<<endl;}int main(){Resume *r1 = new ResumeA("A");// Resume *r2 = new ResumeB("B");Resume *r3 = r1->Clone();// Resume *r4 = r2->Clone();r1->Show();//r2->Show();//删除r1,r2delete r1;//delete r2;r1 = NULL;// r2 = NULL;//深拷贝所以对r3,r4无影响r3->Show();//r4->Show();delete r3;//delete r4;r3 = NULL;// r4 = NULL;return 0;}15、职责连模式#include <iostream>#include <string>using namespace std;class Manager{protected:Manager *m_manager;string m_name;public:Manager(Manager *manager, string name):m_manager(manager), m_name(name){}virtual void DealWithRequest(string name, int num) {}};//经理class CommonManager: public Manager{public:CommonManager(Manager *manager, string name):Manager(manager,name) {}void DealWithRequest(string name, int num){if(num < 500) //经理职权之内{cout<<"经理"<<m_name<<"批准"<<name<<"加薪"<<num<<"元"<<endl<<endl;}elsecout<<"经理"<<m_name<<"无法处理,交由总监处理"<<endl;m_manager->DealWithRequest(name, num);}}};//总监class Majordomo: public Manager{public:Majordomo(Manager *manager, string name):Manager(manager,name) {}void DealWithRequest(string name, int num){if(num < 1000) //总监职权之内{cout<<"总监"<<m_name<<"批准"<<name<<"加薪"<<num<<"元"<<endl<<endl;}else{cout<<"总监"<<m_name<<"无法处理,交由总经理处理"<<endl;m_manager->DealWithRequest(name, num);}}};//总经理class GeneralManager: public Manager{public:GeneralManager(Manager *manager, string name):Manager(manager,name) {}void DealWithRequest(string name, int num) //总经理可以处理所有请求{cout<<"总经理"<<m_name<<"批准"<<name<<"加薪"<<num<<"元"<<endl<<endl;}};int main(){Manager *general = new GeneralManager(NULL, "A"); //设置上级,总经理没有上级Manager *majordomo = new Majordomo(general, "B"); //设置上级Manager *common = new CommonManager(majordomo, "C"); //设置上级common->DealWithRequest("D",300); //员工D要求加薪common->DealWithRequest("E", 600);common->DealWithRequest("F", 1000);delete common; delete majordomo; delete general;}16、状态模式#include <iostream>using namespace std;/*一个状态接口,不同的状态从该接口中继承出来,想要增加新的状态时,只需从此接口中派生出新的类即可,适合于当判断较多(多个状态切换频繁)的场合这里引入一个程序员写工程程序的例子,程序员在一天的不同时间会处于不同的状态如果程序写不完还得加班,即程序员的状态会随着时间发生改变*/class Work;/*状态接口State*/class State{public:virtual void WriteProgram(Work *w) = 0;//写程序的行为(与Work相关的行为)};//工作类class Work{private:State *current;//当前状态double hour;bool taskFinish;public:Work(){taskFinish = false;}void SetTime(double hour){this->hour = hour;}double GetTime(){return this->hour;}//设置状态void SetState(State *s){current = s;}//设置工程是否完成void SetFinish(){taskFinish = true;}bool GetFinish(){return taskFinish;}void WriteProgram(){current->WriteProgram(this);}};//下班休息状态class RestState : public State{public:void WriteProgram(Work *w){cout<<"当前时间:"<<w->GetTime()<<"点工作完成,下班回家了"<<endl;}};//睡眠工作状态class SleepingState : public State{public:void WriteProgram(Work *w){cout<<"受不了了,"<<w->GetTime()<<"点了,先睡吧"<<endl;}};//晚上工作状态class EveningState : public State{public:void WriteProgram(Work *w){//任务完成了,可以休息了if(w->GetFinish()){w->SetState(new RestState());w->WriteProgram();}else{if(w->GetTime()<21){cout<<"当前时间:"<<w->GetTime()<<"点加班了,疲惫至极"<<endl;}else{//找过21点w->SetState(new SleepingState());w->WriteProgram();}}}};//下午工作状态class AfternoonState : public State{public:void WriteProgram(Work *w){if(w->GetTime()<17){cout<<"当前时间:"<<w->GetTime()<<"点状态还不错,继续努力"<<endl;}else{w->SetState(new EveningState());w->WriteProgram();}}};//中午工作状态class NoonState : public State{public:void WriteProgram(Work *w){if (w->GetTime()<13)cout<<"当前时间:"<<w->GetTime()<<"点饿了,午饭:犯困,午休"<<endl;else。
c++设计模式 讲解

c++设计模式讲解摘要:1.设计模式的概念2.C++设计模式的分类3.常见的C++设计模式4.C++设计模式的应用和实例5.总结正文:1.设计模式的概念设计模式是指在软件设计中,为了解决某一类问题或满足某一需求,而采用的一种设计方法或方案。
设计模式可以使代码更加模块化、易于维护和扩展。
C++设计模式就是指在C++语言中使用的设计模式。
2.C++设计模式的分类C++设计模式主要可以分为以下几类:(1)创建型模式:用于创建对象的模式,包括单例模式、工厂方法模式等。
(2)结构型模式:用于组成复杂对象的模式,包括适配器模式、桥接模式等。
(3)行为型模式:用于对象间协作的模式,包括责任链模式、命令模式等。
3.常见的C++设计模式(1)单例模式:保证一个类仅有一个实例,并提供全局访问点。
(2)工厂方法模式:定义一个创建对象的接口,让子类决定实例化哪个类。
(3)观察者模式:定义了一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都会自动更新。
(4)适配器模式:将一个类的接口转换成客户希望的另一个接口。
(5)策略模式:定义了一系列算法,将每一个算法封装起来并可以相互替换使用,使得算法可以独立于使用它的客户而变化。
4.C++设计模式的应用和实例在实际的C++编程中,我们可以根据需求和场景选择合适的设计模式。
例如,当我们需要确保一个类只有一个实例时,可以使用单例模式;当我们需要根据不同条件创建不同类型的对象时,可以使用工厂方法模式;当我们需要实现一个灵活的算法时,可以使用策略模式等。
5.总结C++设计模式是C++编程中重要的技术手段,可以帮助我们更好地组织代码,提高代码的可读性和可维护性。
常见的设计模式及应用场景

常见的设计模式及应用场景设计模式是软件开发中常用的一种代码组织和重用的技术。
它提供了一种解决问题的方案,能够帮助开发人员有效地解决常见的设计问题,并提供可维护、可扩展和可重用的软件。
下面是一些常见的设计模式及其应用场景。
1. 单例模式(Singleton Pattern):单例模式保证一个类只有一个实例,并提供一个全局访问点。
常用于需要共享资源的对象,例如线程池、日志类等。
2. 工厂模式(Factory Pattern):工厂模式用于创建对象,将对象的创建逻辑与客户端代码分离。
常用于创建复杂的对象或者需要隐藏对象创建过程的场景。
3. 观察者模式(Observer Pattern):观察者模式定义了对象间的一对多关系,使得当一个对象状态改变时,其所有依赖对象都能收到通知并自动更新。
常用于事件处理、消息通知等场景。
4. 装饰者模式(Decorator Pattern):装饰者模式在不改变对象原有结构的基础上,动态地为对象添加新的功能。
常用于增强对象的功能或者修改对象的外观。
5. 策略模式(Strategy Pattern):策略模式定义了一系列算法,并将每个算法都封装起来,使得它们可以相互替换。
常用于根据不同的条件选择不同的算法。
6. 适配器模式(Adapter Pattern):适配器模式将一个类的接口转换成另一个接口,以满足客户端的需求。
常用于将不兼容的接口进行适配。
7. 外观模式(Facade Pattern):外观模式提供一个统一的接口,用于访问子系统的一群接口。
常用于简化复杂的子系统调用。
命令模式将一个请求封装成一个对象,使得可以用不同的请求对客户进行参数化。
常用于实现撤销、重做、任务队列等功能。
9. 建造者模式(Builder Pattern):建造者模式通过将复杂对象的构建逻辑与对象本身分离,使得同样的构建过程可以创建不同的表示。
常用于创建复杂的对象。
10. 模板方法模式(Template Method Pattern):模板方法模式定义了一个操作中的算法框架,把一些步骤推迟到子类实现。
设计模式总复习题

总复习题复习参考资料:1.GoF 设计模式2.设计模式解析3.易学设计模式4.大话设计模式5.深入浅出设计模式以参考资料为主,模拟试题1—5中有些题答案在一些附加资料中模拟试题1一、选择题(分值20)1。
设计模式一般用来解决什么样的问题()A。
同一问题的不同表相 B不同问题的同一表相C.不同问题的不同表相D.以上都不是2. 下列属于面向对象基本原则的是( )A。
继承 B.封装 C.里氏代换 D都不是3. Open—Close原则的含义是一个软件实体( )A。
应当对扩展开放,对修改关闭。
B.应当对修改开放,对扩展关闭C。
应当对继承开放,对修改关闭D.以上都不对4。
当我们想创建一个具体的对象而又不希望指定具体的类时,可以使用()模式. A.创建型 B。
结构型 C行为型 D。
以上都可以5。
要依赖于抽象,不要依赖于具体。
即针对接口编程,不要针对实现编程,是( )的表述A. 开—闭原则B. 接口隔离原则C。
里氏代换原则D。
依赖倒转原则6。
依据设计模式思想,程序开发中应优先使用的是()关系实现复用。
A,委派 B.继承 C创建 D。
以上都不对7。
设计模式的两大主题是( )A.系统的维护与开发 B 对象组合与类的继承C。
系统架构与系统开发 D。
系统复用与系统扩展8. 单体模式中,两个基本要点( )和单体类自己提供单例A .构造函数私有 B.唯一实例C.静态工厂方法D.以上都不对9. 下列模式中,属于行为模式的是( )A.工厂模式 B观察者 C适配器以上都是10。
“不要和陌生人说话”是( )原则的通俗表述A.接口隔离 B。
里氏代换 C.依赖倒转 D。
迪米特二、填空题(分值20)1. 软件体系结构是指一个系统的有目的的设计和规划,这个设计规划既不描述 ,也不描述,它只描述系统的及其相互的。
2.一个UML模型只描述了一个系统,它并没告诉我们系统是 .3.接口是可以在整个模型中反复使用的一组行为,是一个没有而只有的类。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
设计模式C++实现(3)——适配器模式
分类:设计模式2011-08-06 17:21 2163人阅读评论(7) 收藏举报软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径。
设计模式中运用了面向对象编程语言的重要特性:封装、继承、多态,真正领悟设计模式的精髓是可能一个漫长的过程,需要大量实践经验的积累。
最近看设计模式的书,对于每个模式,用C++写了个小例子,加深一下理解。
主要参考《大话设计模式》和《设计模式:可复用面向对象软件的基础》(DP)两本书。
本文介绍适配器模式的实现。
DP上的定义:适配器模式将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
它包括类适配器和对象适配器,本文针对的是对象适配器。
举个例子,在STL中就用到了适配器模式。
STL实现了一种数据结构,称为双端队列(deque),支持前后两段的插入与删除。
STL实现栈和队列时,没有从头开始定义它们,而是直接使用双端队列实现的。
这里双端队列就扮演了适配器的角色。
队列用到了它的后端插入,前端删除。
而栈用到了它的后端插入,后端删除。
假设栈和队列都是一种顺序容器,有两种操作:压入和弹出。
下面给出相应的UML图,与DP上的图差不多。
根据上面的UML图,很容易给出实现。
[cpp]view plaincopyprint?
1.//双端队列
2.class Deque
3.{
4.public:
5.void push_back(int x) { cout<<"Deque push_back"<<endl; }
6.void push_front(int x) { cout<<"Deque push_front"<<endl; }
7.void pop_back() { cout<<"Deque pop_back"<<endl; }
8.void pop_front() { cout<<"Deque pop_front"<<endl; }
9.};
10.//顺序容器
11.class Sequence
12.{
13.public:
14.virtual void push(int x) = 0;
15.virtual void pop() = 0;
16.};
17.//栈
18.class Stack: public Sequence
19.{
20.public:
21.void push(int x) { deque.push_back(x); }
22.void pop() { deque.pop_back(); }
23.private:
24. Deque deque; //双端队列
25.};
26.//队列
27.class Queue: public Sequence
28.{
29.public:
30.void push(int x) { deque.push_back(x); }
31.void pop() { deque.pop_front(); }
32.private:
33. Deque deque; //双端队列
34.};
使用方式如下:
[cpp]view plaincopyprint?
1.int main()
2.{
3. Sequence *s1 = new Stack();
4. Sequence *s2 = new Queue();
5. s1->push(1); s1->pop();
6. s2->push(1); s2->pop();
7.delete s1; delete s2;
8.return 0;
9.}。