• 外观模式


    一、简介

    1、外观模式为子系统中的一组接口提供一个统一的高层接口,这一接口使得子系统更加容易使用。

    2、举例 :房间里有3盏灯,每一盏灯都有一个开关控制它的开和关两种状态,由于它们经常一起开关,所以就设置一个总的开关,一下子同时控制所有的灯。

    3、UML图

    4、所属类别:结构型

    二、C++程序

     1 // 外观模式.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include<iostream>
     6 using namespace std;
     7 class Led1
     8 {
     9 public:
    10     Led1(){}
    11     ~Led1(){}
    12     void led_on()
    13     {
    14         cout<<"打开led1"<<endl;
    15     }
    16     void led_off()
    17     {
    18         cout<<"关闭led1"<<endl;
    19     }
    20 };
    21 class Led2
    22 {
    23 public:
    24     Led2(){}
    25     ~Led2(){}
    26     void led_on()
    27     {
    28         cout<<"打开led2"<<endl;
    29     }
    30     void led_off()
    31     {
    32         cout<<"关闭led2"<<endl;
    33     }
    34 };
    35 class Led3
    36 {
    37 public:
    38     Led3(){}
    39     ~Led3(){}
    40     void led_on()
    41     {
    42         cout<<"打开led3"<<endl;
    43     }
    44     void led_off()
    45     {
    46         cout<<"关闭led3"<<endl;
    47     }
    48 };
    49 
    50 class Facade
    
    51 {
    52 private:
    53     Led1 *led1;
    54     Led2 *led2;
    55     Led3 *led3;
    56 public:
    57     Facade()
    58     {
    59         led1=new Led1();
    60         led2=new Led2();
    61         led3=new Led3();
    62     }
    63     ~Facade(){}
    64     void led_on()
    65     {
    66         led1->led_on();
    67         led2->led_on();
    68         led3->led_on();
    69     }
    70     void led_off()
    71     {
    72         led1->led_off();
    73         led2->led_off();
    74         led3->led_off();
    75     }
    76 };
    77 int _tmain(int argc, _TCHAR* argv[])
    78 {
    79     Facade *led=new Facade();
    80     led->led_on();
    81     led->led_off();
    82     return 0;
    83 }
  • 相关阅读:
    LeetCode "Top K Frequent Elements"
    LeetCode "Integer Break"
    HackerRank "Angry Children 2"
    HackerRank "Kitty and Katty"
    HackerRank "Minimum Penalty Path"
    HackerRank "Larry's Array"
    HackerRank "TBS Problem" ~ NPC
    HackerRank "Morgan and a String"
    HackerRank "Favorite sequence"
    Windows不分区VHD装Linux多系统(三):VM虚拟机安装ubuntu18.04
  • 原文地址:https://www.cnblogs.com/bewolf/p/4232925.html
Copyright © 2020-2023  润新知