Bridge桥接模式
做一个"坦克大战"的游戏,在手机上和PC上同时实现:
传统的实现方式:
oldSchool
public abstract class Tank
{
public abstract void Shot();
public abstract void Run();
public abstract void Stop();
}
public class T50 : Tank
{
public override void Shot()
{
//.T50
}
public override void Run()
{
//.T50
}
public override void Stop()
{
//.T50
}
}
public class PCT50 : T50
{
}
public class MobileT50 : T50
{
}
public class T75 : Tank
{
public override void Shot()
{
//.T75
}
public override void Run()
{
//.T75
}
public override void Stop()
{
//.T75
}
}
public class PCT75 : T75
{ }
public class MobileT75 : T75
{ }
public class T90 : Tank
{
public override void Shot()
{
//.T90
}
public override void Run()
{
//.T90
}
public override void Stop()
{
//.T90
}
}
public class PCT90 : T90
{ }
public class MobleT90 : T90
{ }
public abstract class Tank
{
public abstract void Shot();
public abstract void Run();
public abstract void Stop();
}
public class T50 : Tank
{
public override void Shot()
{
//.T50
}
public override void Run()
{
//.T50
}
public override void Stop()
{
//.T50
}
}
public class PCT50 : T50
{
}
public class MobileT50 : T50
{
}
public class T75 : Tank
{
public override void Shot()
{
//.T75
}
public override void Run()
{
//.T75
}
public override void Stop()
{
//.T75
}
}
public class PCT75 : T75
{ }
public class MobileT75 : T75
{ }
public class T90 : Tank
{
public override void Shot()
{
//.T90
}
public override void Run()
{
//.T90
}
public override void Stop()
{
//.T90
}
}
public class PCT90 : T90
{ }
public class MobleT90 : T90
{ }
上述做法导致高耦合。
后面是使用Bridge桥接模式,将平台的 变化引出来在外面变换,使得坦克型号和平台的两个维度上的变换不再混在一起
new
public abstract class Tank
{
protected TankPlatformImplementation tankImpl;
public Tank(TankPlatformImplementation tankImpl)
{
this.tankImpl = tankImpl;
}
public TankPlatformImplementation TankImpl
{
get
{
return this.tankImpl;
}
set
{
this.tankImpl = value;
}
}
public abstract void Shot();
public abstract void Run();
public abstract void Stop();
}
public class T50 : Tank
{
public T50 (TankPlatformImplementation tankImpl)
: base(tankImpl)
{
}
public override void Shot()
{
//.T50
tankImpl.DoShot();
//.T50
}
public override void Run()
{
//.T50
//tankImpl;
}
public override void Stop()
{
//.T50
//tankImpl;
}
}
public class T75 : Tank
{
public T75(TankPlatformImplementation tankImpl)
: base(tankImpl)
{
}
public override void Shot()
{
//.T75
tankImpl.DoShot();
//.T75
}
public override void Run()
{
//.T75
//tankImpl;
}
public override void Stop()
{
//.T75
//tankImpl;
}
}
public class T90 : Tank
{
public T90(TankPlatformImplementation tankImpl)
: base(tankImpl)
{
}
public override void Shot()
{
tankImpl.DoShot();
//.T90
}
public override void Run()
{
//tankImpl;
//.T90
}
public override void Stop()
{
//tankImpl;
//.T90
}
}
public abstract class TankPlatformImplementation
{
public abstract void MoveTankTo(Point to);
public abstract void DrawTank();
public abstract void DoShot();
}
public class PCTankPlatformImplementation : TankPlatformImplementation
{
public override void MoveTankTo(Point to)
{
//
}
public override void DrawTank()
{
//
}
public override void DoShot()
{
//..
}
}
public class MobileTankPlatformImplementation : TankPlatformImplementation
{
public override void MoveTankTo(Point to)
{
//
}
public override void DrawTank()
{
//
}
public override void DoShot()
{
//..
}
}
public class App
{
public static void Main()
{
TankPlatformImplementation tankImpl = new MobileTankPlatformImplementation();
T50 tank = new T50(tankImpl);
}
}
public abstract class Tank
{
protected TankPlatformImplementation tankImpl;
public Tank(TankPlatformImplementation tankImpl)
{
this.tankImpl = tankImpl;
}
public TankPlatformImplementation TankImpl
{
get
{
return this.tankImpl;
}
set
{
this.tankImpl = value;
}
}
public abstract void Shot();
public abstract void Run();
public abstract void Stop();
}
public class T50 : Tank
{
public T50 (TankPlatformImplementation tankImpl)
: base(tankImpl)
{
}
public override void Shot()
{
//.T50
tankImpl.DoShot();
//.T50
}
public override void Run()
{
//.T50
//tankImpl;
}
public override void Stop()
{
//.T50
//tankImpl;
}
}
public class T75 : Tank
{
public T75(TankPlatformImplementation tankImpl)
: base(tankImpl)
{
}
public override void Shot()
{
//.T75
tankImpl.DoShot();
//.T75
}
public override void Run()
{
//.T75
//tankImpl;
}
public override void Stop()
{
//.T75
//tankImpl;
}
}
public class T90 : Tank
{
public T90(TankPlatformImplementation tankImpl)
: base(tankImpl)
{
}
public override void Shot()
{
tankImpl.DoShot();
//.T90
}
public override void Run()
{
//tankImpl;
//.T90
}
public override void Stop()
{
//tankImpl;
//.T90
}
}
public abstract class TankPlatformImplementation
{
public abstract void MoveTankTo(Point to);
public abstract void DrawTank();
public abstract void DoShot();
}
public class PCTankPlatformImplementation : TankPlatformImplementation
{
public override void MoveTankTo(Point to)
{
//
}
public override void DrawTank()
{
//
}
public override void DoShot()
{
//..
}
}
public class MobileTankPlatformImplementation : TankPlatformImplementation
{
public override void MoveTankTo(Point to)
{
//
}
public override void DrawTank()
{
//
}
public override void DoShot()
{
//..
}
}
public class App
{
public static void Main()
{
TankPlatformImplementation tankImpl = new MobileTankPlatformImplementation();
T50 tank = new T50(tankImpl);
}
}