文章目录
设计模式结构可以合理的划分成七种不重叠的模式,其他的创建型模式或行为型模式多多少少会蕴含结构型的设计模式
一实例对多实例
缓存有相同父类对象
Flyweight 享元结构
运用共享技术来有效地支持大量细粒度对象的复用
#include <iostream>
#include <map>
#include <cassert>
enum class BaseType {
A,B,C
};
class Base {};
class SubA:public Base {};
class SubB:public Base {};
class SubC:public Base {};
class BasePool
{
std::map<BaseType,Base*> cache_;
public:
virtual ~BasePool()
{
for(auto [type,ptr] : cache_) {
if(ptr) delete ptr;
}
cache_.clear();
}
Base* Get(const BaseType type)
{
auto it = cache_.find(type);
if(it != cache_.end())return it->second;
Base* ptr = nullptr;
switch(type) {
case BaseType::A:
ptr = new SubA();
break;
case BaseType::B:
ptr = new SubA();
break;
case BaseType::C:
ptr = new SubA();
break;
default:
break;
}
cache_.insert({type,ptr});
return ptr;
}
};
int main()
{
BasePool pool;
auto x = pool.Get(BaseType::A);
auto y = pool.Get(BaseType::A);
assert(x == y);
return 0;
}
组合有相同父类对象
Composite 组合结构
将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
#include <iostream>
#include <string>
#include <set>
class Component
{
public:
virtual void AddChild(Component* const child) {};
virtual void operation_1() = 0;
};
class Composite:public Component
{
std::set<Component*> children_;
public:
void AddChild(Component* const child) override
{
children_.insert(child);
}
void operation_1() override
{
for(auto child:children_) {
if(child) child->operation_1();
}
}
};
class Person:public Component
{
public:
std::string name;
Person(const std::string& name):name(name){}
void operation_1() override
{
std::cout << "name is "<<name<<std::endl;
}
};
int main()
{
Person x{"x"};
Person y{"y"};
Person z{"z"};
Person w{"w"};
Composite root;
Composite com_1;
root.AddChild(&x);
root.AddChild(&com_1);
com_1.AddChild(&y);
com_1.AddChild(&z);
com_1.AddChild(&w);
root.operation_1();
return 0;
}
整合不同类对象
Facade 外观结构
又叫作门面模式,是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式。
class SubSys1{
public:
void operation1(){}
};
class SubSys2{
public:
void operation2(){}
};
class SubSys3{
public:
void operation3(){}
};
class Facade {
SubSys1 sys1;
SubSys2 sys2;
SubSys3 sys3;
public:
void operation_a(){
sys1.operation1();
}
void operation_b(const bool b){
b ? sys2.operation2() : sys3.operation3();
}
};
一实例对一实例
不同接口之间
Adapter 适配器结构
将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。
#include <iostream>
class Target
{
public:
virtual void target_operation() = 0;
};
class Adaptee
{
public:
virtual void adaptee_operation()
{
std::cout << "adaptee function" << std::endl;
}
};
class Adapter : public Target
{
Adaptee* adaptee_;
public:
Adapter(Adaptee* const adaptee):adaptee_(adaptee) {}
void target_operation() override
{
if(adaptee_)adaptee_->adaptee_operation();
}
};
int main()
{
Adaptee tee;
Adapter ter(&tee);
ter.target_operation();
return 0;
}
Bridge 桥结构
将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度。
#include <iostream>
class Implementor
{
public:
virtual void impl_operation() = 0;
};
class Abstraction
{
protected:
Implementor* impl_;
public:
Abstraction(Implementor* const impl):impl_(impl) {}
virtual void operation() = 0;
};
class RefinedAbstraction1:public Abstraction
{
public:
RefinedAbstraction1(Implementor* const impl):Abstraction(impl) {};
void operation() override
{
std::cout << "RefinedAbstraction1 operation & ";
if (this->impl_)impl_->impl_operation();
std::endl(std::cout);
}
};
class RefinedAbstraction2:public Abstraction
{
public:
RefinedAbstraction2(Implementor* const impl):Abstraction(impl) {};
void operation() override
{
std::cout << "RefinedAbstraction2 operation & ";
if (this->impl_)impl_->impl_operation();
std::endl(std::cout);
}
};
class ConcreteImplementor1 : public Implementor
{
public:
void impl_operation() override
{
std::cout <<"ConcreteImplementor1 operation";
}
};
class ConcreteImplementor2 : public Implementor
{
public:
void impl_operation() override
{
std::cout <<"ConcreteImplementor2 operation";
}
};
class ConcreteImplementor3 : public Implementor
{
public:
void impl_operation() override
{
std::cout <<"ConcreteImplementor3 operation";
}
};
int main()
{
ConcreteImplementor1 c1;
ConcreteImplementor2 c2;
ConcreteImplementor3 c3;
Abstraction* a[] {new RefinedAbstraction1(&c1),new RefinedAbstraction1(&c2),new RefinedAbstraction1(&c3),
new RefinedAbstraction2(&c1),new RefinedAbstraction2(&c2),new RefinedAbstraction2(&c3)
};
for(auto p:a) {
p->operation();
delete p;
}
return 0;
}
相同接口之间
针对某些方法
Decorator 装饰器结构
指在不改变现有对象结构的情况下,动态地给该对象增加一些职责
#include <iostream>
class Component
{
public:
virtual void operation() = 0;
};
class ConcreteComponent:public Component
{
public:
void operation() override
{
std::cout << "Component operation" << std::endl;
}
};
class Decorator:public Component
{
Component* comp_;
public:
Decorator(Component* const comp):comp_(comp) {}
void operation() override
{
decorator_operation();
if(comp_)comp_->operation();
}
protected:
virtual void decorator_operation() = 0;
};
class ConcreteDecorator :public Decorator
{
public:
ConcreteDecorator(Component* const comp):Decorator(comp) {}
protected:
void decorator_operation() override
{
std::cout << "decorator_operation" << std::endl;
}
};
int main()
{
ConcreteComponent origin;
ConcreteDecorator compon{&origin};
Component* comp = &compon;
comp->operation();
return 0;
}
针对对象
Proxy 代理结构
由于某些原因需要给某对象提供一个代理以控制对该对象的访问。
#include <iostream>
class ISubject
{
public:
virtual int operation() = 0;
};
class SubjectInner : public ISubject
{
int value = 0;
public:
SubjectInner(const int v):value(v) {}
int operation()
{
value += 10;
return value;
}
};
class Subject: public ISubject
{
SubjectInner *self = nullptr;
void clear_self()
{
if(self) {
delete self;
self= nullptr;
}
}
public:
Subject(const int v = 0):self(new SubjectInner(v)) {}
Subject(Subject& other)
{
clear_self();
self = new SubjectInner(*other.self);
}
Subject(Subject&& other)
{
clear_self();
std::swap(self,other.self);
}
virtual ~Subject()
{
clear_self();
}
int operation() override
{
if(!self)return 0;
return self->operation();
}
};
int main()
{
Subject s;
std::cout << s.operation() << std::endl;
Subject c(std::move(s));
std::cout << s.operation()<< std::endl;
std::cout << c.operation()<< std::endl;
return 0;
}
- 下一章节 GoF设计模式(二)-行为型模式简介