桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化.
如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的联系。
设计要求实现化角色的任何改变不应当影响客户端,或者说实现化角色的改变对客户端是完全透明的。
一个构件有多于一个的抽象化角色和实现化角色,系统需要它们之间进行动态耦合。
虽然在系统中使用继承是没有问题的,但是由于抽象化角色和具体化角色需要独立变化,设计要求需要独立管理这两者.
public abstract class Implementor
{
public abstract void Operation();
}
public class IheritImplement : Implementor
{
public override void Operation()
{
Console.WriteLine("IheritImplement Operation");
}
}
//桥联
public class Abstraction
{
protected Implementor imp;
public Implementor GetImplement
{
set
{
imp = value;
}
}
public virtual void UseOperation()
{
imp.Operation();
}
}
public class AbstactionImplement : Abstraction
{
public override void UseOperation()
{
imp.Operation();
}
}
实现
Abstraction abstraction = new AbstactionImplement();
//Implementor未实例化
abstraction.GetImplement = new IheritImplement();
abstraction.UseOperation();
Console.ReadLine();
如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的联系。
设计要求实现化角色的任何改变不应当影响客户端,或者说实现化角色的改变对客户端是完全透明的。
一个构件有多于一个的抽象化角色和实现化角色,系统需要它们之间进行动态耦合。
虽然在系统中使用继承是没有问题的,但是由于抽象化角色和具体化角色需要独立变化,设计要求需要独立管理这两者.
public abstract class Implementor
{
public abstract void Operation();
}
public class IheritImplement : Implementor
{
public override void Operation()
{
Console.WriteLine("IheritImplement Operation");
}
}
//桥联
public class Abstraction
{
protected Implementor imp;
public Implementor GetImplement
{
set
{
imp = value;
}
}
public virtual void UseOperation()
{
imp.Operation();
}
}
public class AbstactionImplement : Abstraction
{
public override void UseOperation()
{
imp.Operation();
}
}
实现
Abstraction abstraction = new AbstactionImplement();
//Implementor未实例化
abstraction.GetImplement = new IheritImplement();
abstraction.UseOperation();
Console.ReadLine();
浙公网安备 33010602011771号