/*适配器模式:适配器继承或依赖已有的类,实现想要的目标接口,易造成凌乱*/
#include<iostream>
using namespace std;
class Deque //已有类,被适配
{
public:
void push_front(){cout<<"push front"<<endl;}
void push_back(){cout<<"push back"<<endl;}
void pop_front(){cout<<"pop front"<<endl;}
void pop_back(){cout<<"pop back"<<endl;}
};
class Sequence
{
public:
virtual void push() = 0;
virtual void pop() = 0;
};
class Stack:public Sequence, private Deque //私有继承被适配的类,基类函数为私有函数
{
public:
void push()
{
push_back();
}
void pop()
{
pop_back();
}
};
class Queue:public Sequence, private Deque
{
public:
void push()
{
push_back();
}
void pop()
{
pop_front();
}
};
int main()
{
Stack s;
Queue q;
s.push();
q.pop();
return 0;
}
设计模式-适配器模式
最新推荐文章于 2024-10-11 19:57:51 发布