您的位置:首页 > 其它

结构型模式 - 桥接模式 (Structual Patterns - Bridge)

2012-08-31 16:27 246 查看
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
{
void Decode();
}
public class Mp3Decode : IDecode
{
public void Decode()
{
Console.WriteLine("Mp3 .... reading");
}
}
public class APEDecode : IDecode
{
public void Decode()
{
Console.WriteLine("APE .... reading");
}
}
public abstract class MusicPlayer
{
public IDecode Implementor
{
set;
get;
}
public abstract void Play();
}

public class WebMusicPlayer:MusicPlayer
{

public override void Play()
{
Console.WriteLine("This is a web Music Player");
Implementor.Decode();
}
}
public class WinformMusicPlayer : MusicPlayer
{
public override void Play()
{
Console.WriteLine("This is a Winform Music Player");
Implementor.Decode();
}
}
}

-- Client Codes

58 MusicPlayer player = new WinformMusicPlayer();
59 IDecode decode = new Mp3Decode();
60 player.Implementor = decode;
61 player.Play();

63 player = new WebMusicPlayer();
64 decode = new APEDecode();
65 player.Implementor = decode;
66 player.Play();

原文链接:http://www.dofactory.com/Patterns/PatternBridge.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: