电子商务网站运营流程,天元建设集团有限公司第九建筑工程公司,怎么做网站上的模拟动画,vscode 网站开发前言
结构模式可以让我们把很多小的东西通过结构模式组合起来成为一个打的结构#xff0c;但是又不影响各自的独立性#xff0c;尽可能减少各组件之间的耦合。
Adapter Class/Object(适配器#xff09;
Bridge(桥接#xff09;
Composite(组合)
Decorator(装饰)
动态…前言
结构模式可以让我们把很多小的东西通过结构模式组合起来成为一个打的结构但是又不影响各自的独立性尽可能减少各组件之间的耦合。
Adapter Class/Object(适配器
Bridge(桥接
Composite(组合)
Decorator(装饰)
动态地给一个对象添加一些额外的职责不重要的功能只是偶然一次要执行就增加功能来说装饰模式比生成子类更为灵活。建造过程不稳定按正确的顺序串联起来进行控制。 GOOD:当你向旧的类中添加新代码时一般是为了添加核心职责或主要行为。而当需要加入的仅仅是一些特定情况下才会执行的特定的功能时简单点就是不是核心应用的功能就会增加类的复杂度。装饰模式就是把要添加的附加功能分别放在单独的类中并让这个类包含它要装饰的对象当需要执行时客户端就可以有选择地、按顺序地使用装饰功能包装对象。 decorator.h #ifndef CLION_TEST_DECORATOR_H
#define CLION_TEST_DECORATOR_H#include string
#include iostream
using namespace std;// 人
class Person
{
private:string m_strName;
public:Person(string strName) {m_strName strName;}Person() {}virtual void Show(){cout装扮的是:m_strNameendl;}
};// 装饰类
class Finery:public Person
{
protected:Person* m_component;
public:void Decorate(Person* component) {m_component component;}void Show() override{m_component-Show();}
};// T恤
class TShirts:public Finery
{
public:void Show() final{coutT Shirts ;m_component-Show();}
};// 裤子
class BigTrouer: public Finery
{void Show() final{coutBig Trouer ;m_component-Show();}
};#endif //CLION_TEST_DECORATOR_H
main.cpp
#include iostream
#include decorator.husing namespace std;int main() {system(chcp 65001);// 装饰模式Person *p new Person(小李);BigTrouer *bt new BigTrouer();TShirts *ts new TShirts();bt-Decorate(p);ts-Decorate(bt);ts-Show();return 0;
}
输出
T Shirts Big Trouer 装扮的是:小李调用关系
TShirts::Show BigTrouer::Show Person::ShowFacade(外观)
Flyweight(享元)
Proxy(代理)
代理模式
GOOD远程代理可以隐藏一个对象在不同地址空间的事实 虚拟代理通过代理来存放需要很长时间实例化的对象 安全代理用来控制真实对象的访问权限 智能引用当调用真实对象时代理处理另外一些事
proxy.h
#ifndef CLION_TEST_PROXY_H
#define CLION_TEST_PROXY_H#include string
#include iostreamusing namespace std;// 定义接口
class Interface {
public:virtual void Request() 0;
};// 真实类
class RealClass : public Interface {
public:void Request() final {cout 真实的请求 endl;}
};// 代理类
class ProxyClass : public Interface {
private:RealClass *m_realClass;
public:void Request() final {if (m_realClass nullptr) {m_realClass new RealClass();}m_realClass-Request();}
};#endif //CLION_TEST_PROXY_Hmain.cpp
int main() {ProxyClass* test new ProxyClass();test-Request();return 0;
}调用关系
ProxyClass::Request RealClass::Request 后记