适配器模式原理:适配器模式属于结构型模式,主要作用是完成功能的转换,
1.通过一个类继承目标类。
2.需要适配的类
3.适配器
代码如下:
#include <iostream> using namespace std; class Deque { public: void push_back(int x) { cout << "push from back of Deque" <<endl; } void pop_back() { cout << "pop from back of Deque" <<endl; } void push_front(int x) { cout << "push from front of Deque" <<endl; } void pop_front() { cout << "pop from front of Deque" <<endl; } }; class Sequence { public: virtual void push(){} virtual void pop(){} }; class Stack:public Sequence { public: virtual void push(int x){deque.push_back(x);} virtual void pop(){deque.pop_back();} private: Deque deque; }; class Qeque:public Sequence { public: virtual void push(int x){deque.push_back(x);} virtual void pop(){deque.pop_front();} private: Deque deque; }; int main() { Stack s; s.push(1); s.pop(); return 0; }