结构型设计模式---适配器模式(adapter)
2014-08-13 11:24
393 查看
SGI STL stack系以deque作为底部容器完成其所有工作,而具有这种“修改某物接口,
形成另一种风貌”之性质者,称为适配器(adapter),stack被归类为 container adapter,
这种容器适配器还有queue, priority_queue,它们没有迭代器。
适用情况:
使用一个已经存在的类,如果它的接口和你实际要求的不一致时,可以考虑使用适配器模式
#include <iostream>
using namespace std;
class Deque
{
public:
void push_back(int x) { cout << "push_back " << x << endl; }
void pop_back() { cout << "pop_back" << endl; }
void push_front(int x) { cout << "push_front " << x << endl; }
void pop_front() {cout << "pop_front" << endl; }
};
class Stack
{
public:
void push(int x) { sequence.push_back(x); }
void pop() { sequence.pop_back(); }
private:
Deque sequence;
};
int main()
{
Stack st;
st.push(10);
st.pop();
return 0;
}
形成另一种风貌”之性质者,称为适配器(adapter),stack被归类为 container adapter,
这种容器适配器还有queue, priority_queue,它们没有迭代器。
适用情况:
使用一个已经存在的类,如果它的接口和你实际要求的不一致时,可以考虑使用适配器模式
#include <iostream>
using namespace std;
class Deque
{
public:
void push_back(int x) { cout << "push_back " << x << endl; }
void pop_back() { cout << "pop_back" << endl; }
void push_front(int x) { cout << "push_front " << x << endl; }
void pop_front() {cout << "pop_front" << endl; }
};
class Stack
{
public:
void push(int x) { sequence.push_back(x); }
void pop() { sequence.pop_back(); }
private:
Deque sequence;
};
int main()
{
Stack st;
st.push(10);
st.pop();
return 0;
}
相关文章推荐
- 设计模式(6) 结构型模式和适配器模式(ADAPTER)
- [设计模式笔记]二. 结构型模式--7.Adapter模式(适配器模式)(一)
- 设计模式06: Adapter 适配器模式(结构型模式)
- 设计模式(五)适配器模式Adapter(结构型)
- .Net设计模式 Adapter 适配器模式(结构型模式)Adapter Pattern
- 设计模式-结构型- 适配器模式(Adapter)
- 设计模式学习(结构型模式)—适配器模式(Adapter)
- 跟着实例学习设计模式(8)-适配器模式adapter(结构型)
- Java设计模式之适配器模式Adapter(结构型)
- 设计模式(结构型)之适配器模式(Adapter Pattern)
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式(6)-结构型-适配器模式(Adapter)(个人笔记)
- 设计模式 一 适配器模式 Adapter(结构型模式)
- [设计模式笔记]二. 结构型模式--7.Adapter模式(适配器模式)(二)
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式学习之适配器模式(Adapter,结构型模式)(14)
- 【设计模式学习笔记七】【结构型模式】【适配器模式(Adapter)】
- 设计模式--适配器模式Adapter(结构型)
- 设计模式--适配器模式Adapter(结构型)
- 设计模式(五)适配器模式Adapter(结构型)