桥接模式(Bridge Pattern)
意图:将抽象部分与实现部分分离,使它们都可以独立的变化。
生活中的例子
桥接模式将抽象部分与它的实现分离,使它们能够独立地变化。一个普通的开关控制的电灯、电风扇等等,都是桥接的例子。开关的目的是将设备打开或关闭。实际的开关可以是简单的双刀拉链开关,也可以是调光开关。
短信,Email,站内信的发送
Public abstract Class AbstractRoad{ private ICar _car; public ICar Car{ set{_car=value;} } public abstract void GoRun(); } public class Street:AbstractRoad{ public override void GoRun(){ this._car.maxSpeed=60; this._car.minSpeed=10; this._car.run(); } } public class SpeedRoad: AbstractRoad{ public override void GoRun(){ this._car.maxSpeed=100; this._car.minSpeed=60; this._car.run(); } } public interface ICar { int maxSpeed{get;set;} int minSpeed{get;set;} void run(); } public class Bus:ICar{ public int maxSpeed{get;set;} public int minSpeed{get;set;} public void run(){ } } public class MiniCar:ICar{ public int maxSpeed{get;set;} public int minSpeed{get;set;} public void run(){ } } //执行 AbstractRoad street=new Street(); street.Car=new Bus(); street.run(); AbstractRoad speedRoad=new SpeedRoad(); speedRoad.Car=new MiniCar(); speedRoad.run();