zoukankan      html  css  js  c++  java
  • 结构型模式 桥接模式 (Structual Patterns Bridge)

    Definition
     

    解耦抽象(是实现的更高层次的抽象)和它的实现,使他们可以独立变化

    UML class diagram

     

    Partticipants
    Abstraction (MusicPlayer)
    定义一个抽象的Interface
    维护一个一个实现对象的引用
    RefinedAbstraction (WebMusicPlayer, WinformMusicPlayer)
    实现Abstraction的接口
    Implementor (IDecode)

    定义实现的类的Interface
    这个接口不必与抽象的Interface相同,而实际上这俩个接口可以完全不同,常常是 实现接口紧紧提供一个现实对象的操作,
    而抽象接口定义是基于一批现实对象的一个高层次的操作,其实我们可以找个看成俩个维度,其中一个维度是另外一个维度的
    更高层次的抽象
    ConcreteImplement ( Mp3Decode, APEDecode)

    实现了Implementor 的Interface 并实现了具体的操作 

    Sample Codes in C# 
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Sman.DesignPattern.SampleCodes.Structual
     8 {
     9     public interface IDecode
    10     {
    11        void Decode();
    12     }
    13     public class Mp3Decode : IDecode
    14     {
    15         public void Decode()
    16         {
    17             Console.WriteLine("Mp3 .... reading");
    18         }
    19     }
    20     public class APEDecode : IDecode
    21     {
    22         public void Decode()
    23         {
    24             Console.WriteLine("APE .... reading");
    25         }
    26     }
    27     public abstract class MusicPlayer
    28     {
    29         public IDecode Implementor 
    30         {
    31             set;
    32             get;
    33         }
    34         public abstract void Play();
    35     }
    36 
    37     public class WebMusicPlayer:MusicPlayer 
    38     {
    39 
    40         public override void Play()
    41         {
    42             Console.WriteLine("This is a web Music Player");
    43             Implementor.Decode();
    44         }
    45     }
    46     public class WinformMusicPlayer : MusicPlayer
    47     {
    48         public override void Play()
    49         {
    50             Console.WriteLine("This is a Winform Music Player");
    51             Implementor.Decode();
    52         }
    53     }
    54 }
    55 
    56 -- Client Codes
    57 
    58 MusicPlayer player = new WinformMusicPlayer();
    59 IDecode decode = new Mp3Decode();
    60 player.Implementor = decode;
    61 player.Play();
    62 
    63 player = new  WebMusicPlayer();
    64 decode = new APEDecode();
    65 player.Implementor = decode;
    66 player.Play();

    原文链接:http://www.dofactory.com/Patterns/PatternBridge.aspx

  • 相关阅读:
    基于正方系统的抢课软件教程系列一模拟登录1
    基于正方系统的抢课软件教程系列二正则表达式使用
    基于正方系统的抢课软件教程系列一模拟登录3之验证码识别
    eCos中断模型
    eCos下针对MIPS指令集的backtrace
    开源许可证GPL
    获取ListView中的ImageButton
    Rancher v2.4.8 容器管理平台集群搭建(基于k8s)
    Linux awk 替换文本字符串内容
    go: go.mod file not found in current directory or any parent directory; see 'go help mod 解决
  • 原文地址:https://www.cnblogs.com/huangbaixun/p/2665594.html
Copyright © 2011-2022 走看看