代理模式
#include<iostream>
#include<string>
using namespace std;
class AbstractSubject
{
public:
virtual void BuyTicket() = 0;
virtual ~AbstractSubject() {};
};
class Man :public AbstractSubject
{
public:
void BuyTicket()
{
cout << "Man 购票成功!" << endl;
}
};
class Woman :public AbstractSubject
{
public:
void BuyTicket()
{
cout << "Woman 购票成功!" << endl;
}
};
class Meituan : AbstractSubject
{
public:
Meituan(AbstractSubject* obj)
{
this->meituanUser = obj;
}
void BuyTicket()
{
cout << "正在通过美团进行购票..." << endl;
meituanUser->BuyTicket();
}
private:
AbstractSubject* meituanUser;
};
int main()
{
AbstractSubject* man = new Man;
man->BuyTicket();
AbstractSubject* woman = new Woman;
woman->BuyTicket();
AbstractSubject* meituanOfMan = (AbstractSubject*) new Meituan(man);
meituanOfMan->BuyTicket();
AbstractSubject* meituanOfWoman = (AbstractSubject*) new Meituan(woman);
meituanOfWoman->BuyTicket();
delete meituanOfWoman;
delete meituanOfMan;
delete woman;
delete man;
system("pause");
return 0;
}
装饰者模式
#include<iostream>
#include<string>
using namespace std;
class People
{
public:
string wearingState;
virtual void show() = 0;
virtual ~People() {};
};
class Girl : public People
{
public:
Girl()
{
this->wearingState = "女孩穿着,长裤睡衣,长袖睡衣";
}
void show()
{
cout << this->wearingState << endl;
}
};
class Boy : People
{
public:
Boy()
{
this->wearingState = "男孩穿着,短裤睡衣,短袖睡衣";
}
void show()
{
cout << this->wearingState << endl;
}
};
class ChangingClothes
{
public:
string wearingState;
virtual void show() = 0;
virtual ~ChangingClothes() {};
protected:
People* people;
};
class ChangeIntoSchoolUniform : public ChangingClothes
{
public:
ChangeIntoSchoolUniform(People* people)
{
this->people = people;
this->wearingState = this->people->wearingState + ", 但是最后换成了‘校服’";
}
void show()
{
cout << this->wearingState << endl;
}
};
class ChangeIntoSportswear : public ChangingClothes
{
public:
ChangeIntoSportswear(People* people)
{
this->people = people;
this->wearingState = this->people->wearingState + ", 但是最后换成了‘运动装’";
}
void show()
{
cout << this->wearingState << endl;
}
};
int main()
{
People* boy = (People*) new Boy;
boy->show();
People* girl = (People*) new Girl;
girl->show();
ChangingClothes* newBoy = new ChangeIntoSportswear(boy);
newBoy->show();
ChangingClothes* newGirl = new ChangeIntoSchoolUniform(girl);
newGirl->show();
delete boy;
delete girl;
delete newBoy;
delete newGirl;
system("pause");
return 0;
}
适配器模式
对象适配器
#include<iostream>
#include<string>
using namespace std;
class ThreePlug
{
public:
void threePlugCharge()
{
cout << "开始充电..." << endl;
}
};
class AbstractTwoPlug
{
public:
virtual void twoPlugCharge() = 0;
virtual ~AbstractTwoPlug() {};
};
class TwoPlug
{
public:
void twoPlugCharge()
{
cout << "开始充电..." << endl;
}
};
class PlugConverter : public AbstractTwoPlug
{
public:
PlugConverter(ThreePlug* threePlug)
{
this->threePlug = threePlug;
}
void installTheConverter()
{
cout << "在三插头上安装转换器成功!现在已经变为两插头!" << endl;
}
void twoPlugCharge()
{
this->installTheConverter();
this->threePlug->threePlugCharge();
}
protected:
ThreePlug* threePlug;
};
int main()
{
ThreePlug* threePlug = new ThreePlug;
threePlug->threePlugCharge();
PlugConverter* twoPlug = new PlugConverter(threePlug);
twoPlug->twoPlugCharge();
delete threePlug;
delete twoPlug;
system("pause");
return 0;
}
类适配器
#include<iostream>
#include<string>
using namespace std;
class ThreePlug
{
public:
void threePlugCharge()
{
cout << "开始充电..." << endl;
}
};
class AbstractTwoPlug
{
public:
virtual void twoPlugCharge() = 0;
virtual ~AbstractTwoPlug() {};
};
class TwoPlug
{
public:
void twoPlugCharge()
{
cout << "开始充电..." << endl;
}
};
class PlugConverter : public AbstractTwoPlug, public ThreePlug
{
public:
void twoPlugCharge()
{
this->installTheConverter();
threePlugCharge();
}
void installTheConverter()
{
cout << "在三插头上安装转换器成功!现在已经变为两插头!" << endl;
}
};
int main()
{
ThreePlug* threePlug = new ThreePlug;
threePlug->threePlugCharge();
PlugConverter* twoPlug = new PlugConverter;
twoPlug->twoPlugCharge();
delete threePlug;
delete twoPlug;
system("pause");
return 0;
}
桥接模式
#include<iostream>
#include<string>
using namespace std;
class Color
{
public:
virtual void fillColor() = 0;
virtual ~Color() {};
};
class Red : public Color
{
public:
Red() : colorType("Red") {}
void fillColor()
{
cout << "填充颜色:" << colorType << endl;
}
private:
string colorType;
};
class Blue : public Color
{
public:
Blue() : colorType("Blue") {}
void fillColor()
{
cout << "填充颜色:" << colorType << endl;
}
private:
string colorType;
};
class Shape
{
public:
virtual void setColor(Color*) = 0;
virtual void generateShape() = 0;
virtual ~Shape() {};
protected:
virtual void drawShape() = 0;
virtual void fillColor() = 0;
Color* color;
};
class Circle : public Shape
{
public:
Circle()
{
this->shapeType = "Circle";
}
void setColor(Color* color)
{
this->color = color;
}
void generateShape()
{
this->drawShape();
this->fillColor();
}
private:
void drawShape()
{
cout << "绘制:" << shapeType << endl;
}
void fillColor()
{
this->color->fillColor();
}
string shapeType;
};
class Rectangle: public Shape
{
public:
Rectangle()
{
this->shapeType = "Rectangle";
}
void setColor(Color* color)
{
this->color = color;
}
void generateShape()
{
this->drawShape();
this->fillColor();
}
private:
void drawShape()
{
cout << "绘制:" << shapeType << endl;
}
void fillColor()
{
this->color->fillColor();
}
string shapeType;
};
int main()
{
Color* red = new Red;
Color* blue = new Blue;
Shape* circle = new Circle;
Shape* rectangle = new Rectangle;
circle->setColor(red);
circle->generateShape();
rectangle->setColor(blue);
rectangle->generateShape();
delete red;
delete blue;
delete circle;
delete rectangle;
system("pause");
return 0;
}
外观模式
#include<iostream>
#include<string>
using namespace std;
class CppSyntaxAnalyzer
{
public:
void syntaxParser()
{
cout << "分析语法中..." << endl;
}
};
class CppMidCode
{
public:
void generateMidCode()
{
cout << "生成中间代码中..." << endl;
}
};
class CppAssemblyCode
{
public:
void generateAssemblyCode()
{
cout << "生成汇编代码中..." << endl;
}
};
class CppBinaryCode
{
public:
void generateBinaryCode()
{
cout << "生成机器码中..." << endl;
}
};
class CppLink
{
public:
void generateProgram()
{
cout << "生成可执行程序中..." << endl;
}
};
class IDE
{
public:
void compile()
{
CppSyntaxAnalyzer sa;
CppMidCode mc;
CppAssemblyCode ac;
CppBinaryCode bc;
CppLink lk;
sa.syntaxParser();
mc.generateMidCode();
ac.generateAssemblyCode();
bc.generateBinaryCode();
lk.generateProgram();
cout << "程序运行中..." << endl;
}
};
int main()
{
IDE cppIDE;
cppIDE.compile();
system("pause");
return 0;
}
享元模式
#include<iostream>
#include<string>
#include<map>
#include<memory>
using namespace std;
class User
{
private:
string username;
public:
User(string name) : username(name) {}
string getName() const { return username; }
};
class AbstractWebsite
{
public:
virtual void showPage(const User& user) = 0;
virtual ~AbstractWebsite() {}
};
class Website : AbstractWebsite
{
private:
string websiteName;
public:
Website(string name) : websiteName(name) {}
void showPage(const User& user)
{
cout << "网站:" << websiteName << ",正在为用户:" << user.getName() << ",展示页面" << endl;
}
};
class WebsiteFactory
{
private:
map<string, shared_ptr<Website>> webMap;
public:
shared_ptr<Website> generateWebsite(string websiteName)
{
if (webMap.find(websiteName) == webMap.end())
{
webMap.insert(make_pair(websiteName, make_shared<Website>(websiteName)));
}
return webMap[websiteName];
}
int checkWebsiteCount()
{
return webMap.size();
}
};
int main()
{
shared_ptr<WebsiteFactory> webFactory = make_shared<WebsiteFactory>();
shared_ptr<Website> web1 = webFactory->generateWebsite("华为官网");
shared_ptr<Website> web2 = webFactory->generateWebsite("小米官网");
shared_ptr<Website> web3 = webFactory->generateWebsite("腾讯官网");
shared_ptr<User> user1 = make_shared<User>("小明");
shared_ptr<User> user2 = make_shared<User>("小帅");
shared_ptr<User> user3 = make_shared<User>("小美");
web1->showPage(*user1);
web2->showPage(*user2);
web3->showPage(*user3);
system("pause");
return 0;
}
组合模式
#include<iostream>
#include<string>
#include<list>
using namespace std;
class AbstractDirectory
{
public:
virtual void showName() = 0;
virtual void add(AbstractDirectory*) = 0;
virtual void remove(AbstractDirectory*) = 0;
virtual list<AbstractDirectory*> getChild() = 0;
virtual ~AbstractDirectory() {}
};
class Directory : AbstractDirectory
{
private:
string directoryName;
list<AbstractDirectory*> container;
public:
Directory(string name) : directoryName(name) {};
void showName()
{
cout << "目录: " << directoryName << endl;
}
void add(AbstractDirectory* obj)
{
container.push_back(obj);
}
void remove(AbstractDirectory* obj)
{
container.remove(obj);
}
list<AbstractDirectory*> getChild()
{
return container;
}
};
class File : AbstractDirectory
{
private:
string fileName;
list<AbstractDirectory*> container;
public:
File(string name) : fileName(name) {}
void showName() { cout << "文件: " << fileName << endl; }
void add(AbstractDirectory*) {}
void remove(AbstractDirectory*) {}
list<AbstractDirectory*> getChild() { return container; }
};
void showDirectoryTree(AbstractDirectory* obj, int lineWidth)
{
list<AbstractDirectory*> container;
int i = 0;
for (int i = 0; i < lineWidth; i++)
{
cout << "---";
}
obj->showName();
container = obj->getChild();
if (!container.empty())
{
for (auto item : container)
{
if (item->getChild().empty())
{
for (int i = 0; i <= lineWidth; i++)
{
cout << "---";
}
item->showName();
}
else
{
showDirectoryTree(item, lineWidth + 1);
}
}
}
}
int main()
{
AbstractDirectory* root = (AbstractDirectory*) new Directory("C:");
AbstractDirectory* directory = (AbstractDirectory*) new Directory("mydir");
root->add(directory);
AbstractDirectory* file1 = (AbstractDirectory*) new File("main.cpp");
root->add(file1);
AbstractDirectory* file2 = (AbstractDirectory*) new File("run.py");
directory->add(file2);
showDirectoryTree(root, 5);
delete root;
delete directory;
delete file1;
delete file2;
system("pause");
return 0;
}