类图
抽象部分
using System; using System.Collections.Generic; using System.Text; namespace BridgePattern { public class Worker { private string _Age; private string _Name; private string _Tall; private string _Weight; private IWork _IWork; public Worker(string name,string tall,string weight,string age,IWork iwork) { _Name = name; _Tall = tall; _Weight = weight; _Age = age; _IWork = iwork; } public string Name { get { return _Name; } set { _Name = value; } } public string Age { get { return _Age; } set { _Age = value; } } public string Tall { get { return _Tall; } set { _Tall = value; } } public string Weight { get { return _Weight; } set { _Weight = value; } } public IWork IWork { get { return _IWork; } set { _IWork = value; } } public IWork IWork1 { get { throw new System.NotImplementedException(); } set { } } public void Do() { _IWork.Do(); } } }
抽象部分的实现A
using System; using System.Collections.Generic; using System.Text; namespace BridgePattern { public class MachineFactoryWorker : Worker { public MachineFactoryWorker(string name, string tall, string weight, string age, IWork iwork) : base(name, tall, weight, age, iwork) { } } }
抽象部分的实现B
using System; using System.Collections.Generic; using System.Text; namespace BridgePattern { public class ClothesFactoryWorker:Worker { public ClothesFactoryWorker(string name, string tall, string weight, string age, IWork iwork):base(name,tall,weight,age,iwork) { } } }
行为部分
using System; using System.Collections.Generic; using System.Text; namespace BridgePattern { public interface IWork { void Do(); } }
行为部分的实现A
using System; using System.Collections.Generic; using System.Text; namespace BridgePattern { public class MakeClothes : IWork { #region IWork 成员 public void Do() { Console.WriteLine("我能洗衣服"); } #endregion } }
行为部分的实现B
using System; using System.Collections.Generic; using System.Text; namespace BridgePattern { public class MakeMachine : IWork { #region IWork 成员 public void Do() { Console.WriteLine("我能装配机器"); } #endregion } }
调用代码
using System; using System.Collections.Generic; using System.Text; namespace BridgePattern { class Program { static void Main(string[] args) { IWork makeClothes = new MakeClothes(); IWork makeMachine = new MakeMachine(); Worker clothesWorker = new ClothesFactoryWorker("张三", "160CM", "60KG", "22",makeClothes); Worker machineWorker = new MachineFactoryWorker("李四", "180CM", "70KG", "30", makeMachine); clothesWorker.Do(); machineWorker.Do(); Console.ReadLine(); } } }
执行结果:
我能洗衣服
我能装配机器