using System; using System.Collections.Generic; using System.Text; ///Bridge(桥接)模式将抽象部分与它的实现部分进行分离,使它们都可以独立地变化。 ///add by jzh 2007-04-18 namespace DesignPattern { class Abstraction { protected Implementation impDefault; public void SetImplementation(Implementation i) { impDefault = i; } /// <summary> /// virtual 关键字用于修改方法或属性的声明, /// 在这种情况下,方法或属性被称作虚拟成员。 /// 虚拟成员的实现可由派生类中的重写成员更改。 /// </summary> /// <param name="str"></param> virtual public void GetAction(string str) { impDefault.DoAction(str); } } class UserAbstraction : Abstraction { /// <summary> /// 实现一个重写方法override /// </summary> /// <param name="str"></param> override public void GetAction(string str) { str += "/ver"; impDefault.DoAction(str); } } class Implementation { public virtual void DoAction(string str) { Console.WriteLine("默认"); Console.WriteLine("string = {0}", str); } } class UserImplementation : Implementation { override public void DoAction(string str) { Console.WriteLine("派生"); Console.WriteLine("string = {0}", str); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { Abstraction SetupFactAbstraction() { Abstraction a = new UserAbstraction(); a.SetImplementation(new UserImplementation()); return a; } public static void Main(string[] args) { Client c = new Client(); Abstraction a = c.SetupFactAbstraction(); a.GetAction("getUser"); Console.ReadLine(); } } }