您的位置:首页 > 其它

结构型设计模式---适配器模式(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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: