博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式——5.桥模式
阅读量:5057 次
发布时间:2019-06-12

本文共 6071 字,大约阅读时间需要 20 分钟。

桥模式(Bridge)

Bridge模式简介:

抽象部分与它的实现部分分离,使它们都可以独立的变化

合成、聚合复用原则:

  • 合成/组合(Composition),一种强拥有关系,体现严格的整体部分的关系,整体部分的生命周期一样。
  • 聚合(Aggregation),一种弱的拥有关系,体现整体包含对象的关系,对象不是整体的某一部分。

Bridge模式结构:

stg3JBl.png

C++代码

Implemantor类 & ConcreteImplementorA和B派生类:

//file: Implementor.h    #pragma once        class Implementor    {    public:        Implementor();        virtual ~Implementor();                virtual void operation();    };        class ConcreteImplementorA : public Implementor    {    public:        ConcreteImplementorA();        virtual ~ConcreteImplementorA();            virtual void operation();    };        class ConcreteImplementorB : public Implementor    {    public:        ConcreteImplementorB();        virtual ~ConcreteImplementorB();            virtual void operation();    };
//file: Implementor.cpp    #include "pch.h"    #include "Implementor.h"    #include 
using namespace std; //Implementor Implementor::Implementor() {} Implementor::~Implementor() {} void Implementor::operation() {} //ConcreteImplementorA ConcreteImplementorA::ConcreteImplementorA() {} ConcreteImplementorA::~ConcreteImplementorA() {} void ConcreteImplementorA::operation() { cout << "Concrete Implement A's Func ." << endl; } //ConcreteImplementorB ConcreteImplementorB::ConcreteImplementorB() {} ConcreteImplementorB::~ConcreteImplementorB() {} void ConcreteImplementorB::operation() { cout << "Concrete Implement B's Func ." << endl; }

Abstraction类 & ConcreteAbstraction1和2派生类:

//file: Abstraction.h    #pragma once    #include "Implementor.h"        class Abstraction    {    protected:        Implementor * m_pImp;        public:        Abstraction();        virtual ~Abstraction();            Abstraction(Implementor * pImp);        virtual void operation();    };        class ConcreteAbstraction1 : public Abstraction    {    public:        ConcreteAbstraction1();        ConcreteAbstraction1(Implementor *pImp);        virtual ~ConcreteAbstraction1();            virtual void operation();    };        class ConcreteAbstraction2 : public Abstraction    {    public:        ConcreteAbstraction2();        ConcreteAbstraction2(Implementor * pImp);        virtual ~ConcreteAbstraction2();            virtual void operation();    };
//file: Abstraction.cpp    #include "pch.h"    #include "Abstraction.h"    #include 
//Abstraction Abstraction::Abstraction() {} Abstraction::~Abstraction() { delete m_pImp; } Abstraction::Abstraction(Implementor *pImp) { m_pImp = pImp; } void Abstraction::operation() {} //ConcreteAbstraction1 ConcreteAbstraction1::ConcreteAbstraction1() {} ConcreteAbstraction1::ConcreteAbstraction1(Implementor *pImp) : Abstraction(pImp) {} ConcreteAbstraction1::~ConcreteAbstraction1() {} void ConcreteAbstraction1::operation() { std::cout << "Concrete Abstraction 1's Addition Action ." << std::endl; m_pImp->operation(); } //ConcreteAbstraction2 ConcreteAbstraction2::ConcreteAbstraction2() {} ConcreteAbstraction2::ConcreteAbstraction2(Implementor *pImp) : Abstraction(pImp) {} ConcreteAbstraction2::~ConcreteAbstraction2() {} void ConcreteAbstraction2::operation() { std::cout << "Concrete Abstraction 2's Addition Action ." << std::endl; m_pImp->operation(); }

客户端代码:

//file: BridgePattern.cpp : This file contains the 'main' function. Program execution begins and ends there.    #include "pch.h"    #include "Abstraction.h"    #include 
using namespace std; int main() { Implementor *imp = new ConcreteImplementorA(); Abstraction *a1 = new ConcreteAbstraction1(imp); a1->operation(); Abstraction *a2 = new ConcreteAbstraction2(new ConcreteImplementorB()); a2->operation(); char c; cin >> c; return 0; }

C#代码

Implemantor类 & ConcreteImplementorA和B派生类:

public abstract class Implementor    {        public abstract void Operation();    }    public class ConcreteImplementorA : Implementor    {        public override void Operation()        {            Console.WriteLine("Concrete Implementor A's Func .");        }    }    public class ConcreteImplementorB : Implementor    {        public override void Operation()        {            Console.WriteLine("Concrete Implementor B's Func .");        }    }

Abstraction类 & ConcreteAbstraction1和2派生类:

public class Abstraction    {        protected Implementor m_imple;        public Abstraction() { }        public Abstraction(Implementor implementor)        {            this.m_imple = implementor;        }        public void SetImple(Implementor implementor)        {            this.m_imple = implementor;        }        public virtual void Operation()        {            if (m_imple != null)                m_imple.Operation();        }    }    public class ConcreteAbstraction1 : Abstraction    {        public ConcreteAbstraction1(Implementor implementor) : base(implementor) { }        public override void Operation()        {            Console.WriteLine("The ConcreteAbstraction 1's additional action .");            base.Operation();        }    }    public class ConcreteAbstraction2 : Abstraction    {        public ConcreteAbstraction2(Implementor implementor) : base(implementor) { }        public override void Operation()        {            Console.WriteLine("The ConcreteAbstraction 2's additional action .");            base.Operation();        }    }

客户端代码:

class Program    {        static void Main(string[] args)        {            Implementor imp = new ConcreteImplementorA();            Abstraction absA = new ConcreteAbstraction1(imp);            absA.Operation();            Abstraction absB = new ConcreteAbstraction2(new ConcreteImplementorB());            absB.Operation();            Console.ReadKey(false);        }    }

REF

书籍:

设计模式与游戏开发、大话设计模式

GitHub:

转载于:https://www.cnblogs.com/sylvan/p/9745669.html

你可能感兴趣的文章
Qt中QTableView中加入Check列实现
查看>>
“富豪相亲大会”究竟迷失了什么?
查看>>
控制文件的备份与恢复
查看>>
返回代码hdu 2054 A==B?
查看>>
Flink独立集群1
查看>>
iOS 8 地图
查看>>
20165235 第八周课下补做
查看>>
[leetcode] 1. Two Sum
查看>>
iOS 日常工作之常用宏定义大全
查看>>
PHP的SQL注入技术实现以及预防措施
查看>>
MVC Razor
查看>>
软件目录结构规范
查看>>
Windbg调试Sql Server 进程
查看>>
linux调度器系列
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
SVN服务器搭建和使用(三)(转载)
查看>>
Android 自定义View (三) 圆环交替 等待效果
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>