C#設計模式-橋接模式
阿新 • • 發佈:2018-12-18
using System; using System.Collections.Generic; namespace TestCS { class Program { static void Main(string[] args) { Vehicle vehicle = new Maserati(new MaseratiEngine()); vehicle.Run(); vehicle = new Maserati(new BMWEngine()); vehicle.Run(); vehicle = new BMW(new BMWEngine()); vehicle.Run(); Console.ReadKey(); } abstract class Vehicle { protected Engine m_Engine; public Vehicle(Engine engine) { m_Engine = engine; } public abstract void Run(); } abstract class Engine { public abstract void Launch(); } class Maserati : Vehicle { public Maserati(Engine engine) : base(engine) { } public override void Run() { Console.WriteLine("Maserati Run"); m_Engine.Launch(); } } class BMW : Vehicle { public BMW(Engine engine) : base(engine) { } public override void Run() { Console.WriteLine("BMW Run"); m_Engine.Launch(); } } class MaseratiEngine : Engine { public override void Launch() { Console.WriteLine("Maserati Engine Launch"); } } class BMWEngine : Engine { public override void Launch() { Console.WriteLine("BMW Engine Launch"); } } } }