zoukankan      html  css  js  c++  java
  • C++设计模式之桥接模式

    [DP]书上定义:将抽象部分与它的实现部分分离,使它们都可以独立地变化。考虑装操作系统,有多种配置的计算机,同样也有多款操作系统。如何运用桥接模式呢?可以将操作系统和计算机分别抽象出来,让它们各自发展,减少它们的耦合度。当然了,两者之间有标准的接口。这样设计,不论是对于计算机,还是操作系统都是非常有利的。下面给出这种设计的UML图,其实就是桥接模式的UML图。

    UML图:

    // Bridge.cpp : 定义控制台应用程序的入口点。
    //
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    class OS
    {
    public:
     virtual void InstallOS_Imp(){}
    };
    class WindowOS:public OS
    {
    public:
     virtual void InstallOS_Imp() override
     {
      cout<<"安装windows操作系统"<<endl;
     }
    };
    class LinuxOS:public OS
    {
    public:
     virtual void InstallOS_Imp() override
     {
      cout<<"安装Linux操作系统"<<endl;
     }
    };
    class UnixOS: public OS
    {
    public:
     void InstallOS_Imp() { cout<<"安装Unix操作系统"<<endl; }
    };
    class Computer
    {
    public:
     virtual void InstallOS(OS *os) {}
    };
    class DellComputer:public Computer
    {
    public:
     DellComputer()
     {
      cout<<"Dell电脑"<<endl;
     }
     void InstallOS(OS *os) override
     {
      os->InstallOS_Imp();
     }
    };
    class AppleComputer:public Computer
    {
    public:
     AppleComputer()
     {
      cout<<"苹果电脑"<<endl;
     }
     void InstallOS(OS *os) override
     {
      os->InstallOS_Imp();
     }
    };
    class HPComputer:public Computer
    {
    public:
     HPComputer()
     {
      cout<<"HP电脑"<<endl;
     }
     void InstallOS(OS *os) override
     {
      os->InstallOS_Imp();
     }
    };
    int _tmain(int argc, _TCHAR* argv[])
    {
     cout<<"桥接模式"<<endl;
     OS *os1 = new WindowOS();
     OS *os2 = new LinuxOS();
     Computer *computer1 = new AppleComputer();
     computer1->InstallOS(os1);
     computer1->InstallOS(os2);
     system("pause");
     return 0;
    }
     

     

  • 相关阅读:
    centos7.6安装Oracle11g过程记录(下)
    centos7.6 安装解压缩软件
    centos7.6 安装及配置ftp服务
    MySQL8.0的主从配置过程记录
    解决 /dev/mapper/centos-root 空间不足的问题
    ASP判断当前页面上是否有参数ID传递过来
    通过ASP禁止指定IP和只允许指定IP访问网站的代码
    asp自动补全html标签自动闭合(正则表达式)
    asp中utf8不会出现乱码的写法
    通过安全字符串过滤非法字符
  • 原文地址:https://www.cnblogs.com/wxmwanggood/p/9295635.html
Copyright © 2011-2022 走看看