桥接模式(从多个角度对实现进行分类)

桥接模式(Bridge):

  将抽象部分与它的实现部分分离,使他们都能独立地变化。

说白了,就是“实现系统可能有多个角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来,让他们独立变化,减少它们之间的耦合。

一、UML结构图

二、示例代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace 桥接模式
  7 {
  8     class Program
  9     {
 10         static void Main(string[] args)
 11         {
 12             //定义扫雷游戏
 13             Implementor i1 = new ConcreteImplementorA();
 14             //苹果手机
 15             Abstraction ab1 = new RefinedAbstractionA();
 16             //在苹果手机上运行扫雷游戏
 17             ab1.SetImplementor(i1);
 18             //运行
 19             ab1.Operation();
 20             
 21            
 22             //三星手机
 23             Abstraction ab2 = new RefinedAbstractionB();
 24             //在三星手机上运行扫雷游戏
 25             ab2.SetImplementor(i1);
 26             //运行
 27             ab2.Operation();
 28 
 29             Console.Read();
 30 
 31         }
 32     }
 33 
 34 #region 实现层维度划分
 35 
 36     /// <summary>
 37     /// 实现层(例如:手机游戏)
 38     /// </summary>
 39     public abstract class Implementor
 40     {
 41         public abstract void Operation();
 42     }
 43     /// <summary>
 44     /// 具体实现A(扫雷游戏)
 45     /// </summary>
 46     public class ConcreteImplementorA : Implementor
 47     {
 48         public override void Operation()
 49         {
 50             Console.WriteLine("我是具体实现A");
 51         }
 52     }
 53     /// <summary>
 54     /// 具体实现B(雪人游戏)
 55     /// </summary>
 56     public class ConcreteImplementorB : Implementor
 57     {
 58         public override void Operation()
 59         {
 60             Console.WriteLine("我是具体实现B");
 61         }
 62     }
 63     
 64     #endregion
 65 
 66     /// <summary>
 67     /// 抽象层(从另一个维度,对实现层进行分类)(在不同手机品牌上运行)
 68     /// </summary>
 69     public  class Abstraction
 70     {
 71         protected Implementor m_Implementor;
 72         public void SetImplementor(Implementor implementor)
 73         {
 74             this.m_Implementor = implementor;
 75         }
 76         /// <summary>
 77         /// 运行游戏
 78         /// </summary>
 79         public virtual void Operation()
 80         {
 81             m_Implementor.Operation();
 82         }
 83 
 84     }
 85     /// <summary>
 86     /// 具体抽象类(在苹果手机上运行)
 87     /// </summary>
 88     public class RefinedAbstractionA : Abstraction
 89     {
 90         /// <summary>
 91         /// 在手机上运行
 92         /// </summary>
 93         public override void Operation()
 94         {
 95             Console.WriteLine("我是苹果手机,正在将游戏适配苹果手机硬件");
 96             m_Implementor.Operation();
 97         }
 98     }
 99 
100     /// <summary>
101     /// 具体抽象类(在三星手机上运行)
102     /// </summary>
103     public class RefinedAbstractionB : Abstraction
104     {
105         /// <summary>
106         /// 在手机上运行
107         /// </summary>
108         public override void Operation()
109         {
110             Console.WriteLine("我是三星手机,正在将游戏适配三星手机硬件");
111             m_Implementor.Operation();
112         }
113     }
114 
115 
116 }
不同品牌手机上运行不同游戏
原文地址:https://www.cnblogs.com/qiupiaohujie/p/11975302.html