class A
{
public:
    // Interface.
    void foo() { cout <<"A::foo()." <<endl; }
};
class B
{
public:
    // Interface.
    void bar() { cout <<"B::bar()." <<endl; }
};
class C
{
public:
    // Interface.
    void baz() { cout <<"C::baz()." <<endl; }
};

class Facade
{
    // data.
    A *m_a;
    B *m_b;
    C *m_c;

public:
    Facade(A *a, B *b, C *c) : m_a(a), m_b(b), m_c(c) {}

public:
    // Interface.
    void qux()
    {
        // 这里封装了复杂的操作. 但是 Facade 对外, 却提供了简单的
        // 'qux()' 接口. 这就将负责的操作隐藏在了 Facade 之中.
        // Facade 与 Bridge 异曲同工, 都是使用一个独立的类, 封装变化.
        // Facade 封装的是多个对象的复杂性, 而 Bridge 封装的是单个对象的衍变.
        m_a->foo();
        m_b->bar();
        m_c->baz();
    }
};

int main(int argc, char *argv[])
{
    Facade f(new A, new B, new C);

    f.qux();

    // TODO: Release memory.

    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-12-21
  • 2021-12-19
  • 2021-10-03
  • 2021-07-20
  • 2021-08-21
猜你喜欢
  • 2022-02-04
  • 2021-05-27
  • 2021-09-13
  • 2021-05-30
相关资源
相似解决方案