8.Bridge 桥接模式(结构型模式)
【1】抽象A==>实现细节b
【2】抽象A==>抽象B<==实现细节b
【3】【抽象B】==相对稳定,也可能变化 【实现细节b】==频繁变化
【4】抽象B 不稳定?
思考问题:一个变化是平台(抽象B)的变化,另一个变化是型号(实现细节b)的变化。
【抽象部分B】
public abstract class Tank
{
protected TankPlatformImplementation tankImple;
public Tank(TankPlatformImplementation tankImple){this.tankImple=tankImple;}
public abstract void Shot();
public abstract void Run();
public abstract void Trun();
}
public abstract class TankPlatformImplementation
{
public abstract void MoveTankTO(Point to);//移动到某个点
public abstract void DrawTank(); //画一个Tank
public abstract void DoShot(); //播放声音,画出轨迹,特效等
}
【实现细节b】各种实际实现通过继承来实现
public class T50:Tank { }
public class T75:Tank { }
public class T90:Tank { }
各种平台不同的实现:
//PC //Mobile
public class PCT50:T50 { } public class MobileT50:T50 { }
public class PCT75:T75 { } public class MobileT75:T75 { }
public class PCT90:T90 { } public class MobileT90:T90 { }
软件加上时间轴,随着时间的变化,动态的创作软件系统。
using System;
namespace ConsoleApp2
{
internal class Tank_Factory
{
public static void Main()
{
TankPlatformImplementation tankImpl = new MobileTankImplementation();
T50 tank = new T50(tankImpl);
tank.Show();
tank.TankImpl = new MobileTankImplementation();
tank.Show();
Console.WriteLine(tank.ToString());
Console.WriteLine("Tank_Factory01");
Console.ReadLine();
}
}
public class Point
{
}
//--Tank------------
public abstract class Tank
{
protected TankPlatformImplementation tankImpl;
protected Tank(TankPlatformImplementation tankImpl)
{
this.tankImpl = tankImpl;
}
public TankPlatformImplementation TankImpl
{
get { return this.tankImpl; }
set { this.tankImpl = value; }
}
public abstract void Show();
public abstract void Run();
public abstract void Stop();
}
//--TankPlatformImplementation------------
public abstract class TankPlatformImplementation
{
public abstract void MoveTank(Point to);
public abstract void DrawTank();
public abstract void DoShot();
}
//--PCTankImplementation------------------
public class PCTankImplementation : TankPlatformImplementation
{
public override void DoShot()
{
Console.WriteLine("PCTankImplementation");
}
public override void DrawTank()
{
}
public override void MoveTank(Point to)
{
}
}
//--MobileTankImplementation--------------
public class MobileTankImplementation : TankPlatformImplementation
{
public override void DoShot()
{
Console.WriteLine("MobileTankImplementation");
}
public override void DrawTank()
{
}
public override void MoveTank(Point to)
{
}
}
//--T50-------------
public class T50 : Tank
{
public T50(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
//--T75-------------
public class T75 : Tank
{
public T75(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
//--T90--------------
public class T90 : Tank
{
public T90(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
//--PCT50--------------
public class PCT50 : T50
{
public PCT50(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
//--PCT75--------------
public class PCT75 : T75
{
public PCT75(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
//--PCT90--------------
public class PCT90 : T90
{
public PCT90(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
//--MobileT50-------------
public class MobileT50 : T50
{
public MobileT50(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
//--MobileT75-------------
public class MobileT75 : T75
{
public MobileT75(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
//--MobileT90-------------
public class MobileT90 : T90
{
public MobileT90(TankPlatformImplementation tankImpl) : base(tankImpl)
{
}
public override void Run()
{
tankImpl.MoveTank(new Point());
}
public override void Show()
{
tankImpl.DoShot();
}
public override void Stop()
{
tankImpl.DrawTank();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
ImageImp temp1 = new LinuxImp();
ImageImp temp = new WindowImp();
Image imp = new JPGImage();
imp.SetImageImp(temp);
imp.Show("zhongguo");
Image imp1= new PNGImage();
imp1.SetImageImp(temp1);
imp1.Show("zhongguo");
Console.WriteLine("Program02--------------");
Console.ReadLine();
}
}
//-----------------------------------------------
public class Matix
{
public int x;
public int y;
public Matix(int x, int y)
{
this.x = x;
this.y = y;
}
}
public abstract class ImageImp { public abstract void DoPaint(Matix m); }
public class WindowImp : ImageImp
{
public override void DoPaint(Matix m)
{
Console.WriteLine("在window系统中显示图像。");
}
}
public class LinuxImp : ImageImp
{
public override void DoPaint(Matix m)
{
Console.WriteLine("在Linux系统中显示图像。");
}
}
public class UnixxImp : ImageImp
{
public override void DoPaint(Matix m)
{
Console.WriteLine("在Linux系统中显示图像。");
}
}
public abstract class Image
{
protected ImageImp imageImp;
public void SetImageImp(ImageImp imageImp) { this.imageImp = imageImp; }
public abstract void Show(string s);
}
public class JPGImage : Image
{
public override void Show(string s)
{
imageImp.DoPaint(new Matix(10, 20));
Console.WriteLine("JpgImp---");
}
}
public class PNGImage : Image
{
public override void Show(string s)
{
imageImp.DoPaint(new Matix(10, 30));
Console.WriteLine("PNGImp---");
}
}
//----------------------------------------------
public abstract class Implemetor//实施者
{
public abstract void OperationImpl();//活动,业务
}
public class ConcreteImplementor : Implemetor
{
public override void OperationImpl()
{
throw new NotImplementedException();
}
}
public abstract class Abstraction
{
protected Implemetor impl; //定义实现类接口对象
public void SetImpl(Implemetor impl)
{
this.impl = impl;
}
public abstract void Operation();//声明抽象业务方法
}
public class RefinedAbstraction : Abstraction
{
public override void Operation()
{
impl.OperationImpl(); //调用实现类代码
}
}
}
--------------------------------------------------------------------------------------------------------------------------------
桥接模式意图定义:将抽象部分与它他的实现部分解耦,使得两者都能够独立变化。--(GoF)
---------------------------------------------------------------------------------------------------------------------------------