zoukankan      html  css  js  c++  java
  • 大话设计模式-外观模式

    外观模式:为子系统中的一组接口提供一个一致的界面,从模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

     1 // facade.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include <iostream>
     6 
     7 using namespace std;
     8 
     9 class systemone{
    10 
    11 public:
    12     void method1(){
    13         cout << "systemone : method1" << endl;
    14     }
    15 };
    16 
    17 class systemtwo{
    18 public:
    19     void method2(){
    20         cout << "systemtwo : method2" << endl;
    21     }
    22 };
    23 class systemthree{
    24 public:
    25     void method3(){
    26         cout << "systemthree : method3" << endl;
    27     }
    28 };
    29 class systemfour{
    30 public:
    31     void method4(){
    32         cout << "systemfour : method4" << endl;
    33     }
    34 };
    35 
    36 class Facade{
    37 
    38 public:
    39     void Fund(){
    40         sysone = new systemone();
    41         systwo = new systemtwo();
    42         systhree = new systemthree();
    43         systwo = new systemtwo();
    44         sysfour = new systemfour();
    45     }
    46 
    47     void Method1(){
    48 
    49         sysone->method1();
    50         systwo->method2();
    51         sysfour->method4();
    52     }
    53 
    54     void Method2(){
    55         systwo->method2();
    56         systhree->method3();
    57         sysfour->method4();
    58     }
    59 private:
    60     systemone *sysone;
    61     systemtwo *systwo;
    62     systemthree *systhree;
    63     systemfour *sysfour;
    64 };
    65 int _tmain(int argc, _TCHAR* argv[])
    66 {
    67     Facade *f = new Facade();
    68     f->Method1();
    69     f->Method2();
    70     return 0;
    71 }

    使用场景:

    (1)设计初期,有意识的将不同的两个层进行分离,比如经典的三层架构,就要考虑数据访问层和业务逻辑层,业务逻辑层与表现层,层与层之间建立外观facade。降低耦合性。

    (2)开发阶段,子系统随着不断的重构演化会变得越来越复杂,会产生很多很小的类。增加外观facade提供一个简单的接口,减少他们的依赖。

    (3)在维护一个遗留的大型系统时,可能这个系统很难维护,就可以开发外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰的结接口,让新系统与facade对象交互,facade与遗留系统进行交互复杂的工作。

  • 相关阅读:
    搭建Flask+Vue及配置Vue 基础路由
    vue dev 环境下的跨域访问
    vue 调用常量的config.js文件
    PyCharm 通过Github和Git上管理代码
    MongoDB 配置服务
    2018年11月12日
    搭建 flask 应用
    Python 错误总结
    搭建 Django 平台
    go实现主线程等待子线程都运行完再退出
  • 原文地址:https://www.cnblogs.com/zhangjxblog/p/8886149.html
Copyright © 2011-2022 走看看