using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DesignMode { public class BridgeParth { public void Initiale() { } private Animal _animal; public Animal CurAnimal { get { return _animal; } set { _animal = value; } } public virtual void Age() { _animal.Age(); } public virtual void Weight() { _animal.Weight(); } public virtual void SerialNum() { _animal.SerialNum(); } } /// <summary> /// 动物的抽象类 /// </summary> public abstract class Animal { public abstract void Age(); public abstract void Weight(); public abstract void SerialNum(); } public class Crow : Animal { public override void Age() { Console.WriteLine("Crow Age"); } public override void SerialNum() { Console.WriteLine("Crow SerialNum"); } public override void Weight() { Console.WriteLine("Crow Weight"); } } public class Fish : Animal { public override void Age() { Console.WriteLine("Fish Age"); } public override void SerialNum() { Console.WriteLine("Fish SerialNum"); } public override void Weight() { Console.WriteLine("Fish Weight"); } } } static void Main(string[] args) { BridgeParth bridgeParth = new BridgeParth(); Crow crow = new Crow(); Fish fish = new Fish(); bridgeParth.CurAnimal = crow; bridgeParth.Age(); bridgeParth.SerialNum(); bridgeParth.Weight(); bridgeParth.CurAnimal = fish; bridgeParth.Age(); bridgeParth.SerialNum(); bridgeParth.Weight(); Console.Read(); }
优点:
把抽象接口与其实现解耦。
抽象和实现可以独立扩展,不会影响到对方。
实现细节对客户透明,对用于隐藏了具体实现细节。
拓展性比较好,在同一框架下的不同的功能块,更换变化不影响整体性;
易于维护和拓展;
缺点: 增加了系统的代码量,