结构型模式 - 桥接模式 (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
解耦抽象(是实现的更高层次的抽象)和它的实现,使他们可以独立变化
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
相关文章推荐
- 设计模式笔记--结构型模式之二--桥接 Bridge
- 设计模式07: Bridge 桥接模式(结构型模式)
- 结构型模式之桥接模式(Bridge)
- 设计模式之结构型模式(一)-----桥接(bridge)模式
- 设计模式--桥接模式Bridge(结构型)
- 桥接(bridge) 模式--结构型模式之五
- Bridge 桥接(结构型模式)
- C#面向对象设计模式纵横谈 笔记8 Bridge 桥接(结构型模式)
- Bridge(桥接)-对象结构型模式
- 结构型模式:桥接(Bridge)
- 结构型模式--Bridge桥接模式
- 2.2 Bridge(桥接) -- 对象结构型模式
- 七、桥接模式Bridge(结构型)
- 结构型—桥接(Bridge)模式
- 结构型模式2:桥接模式(Bridge Pattern)
- 设计模式学习之桥接模式(Bridge,结构型模式)(15)
- [导入]C#面向对象设计模式纵横谈(8):Bridge 桥接模式(结构型模式).zip(9.01 MB)
- 结构型模式-桥接模式Bridge
- Bridge 桥接(结构型模式)
- 结构型模式-----桥接模式(Bridge)