2、 ----------《设计模式》GOF结构图(Struct): 生活实例: 适用性: 1.当一个类不知道它所必须创建的对象类的时候。 2.当一个类希望由它子类来指定它所创建对象的时候。 3.当类将创建对象的职责委托给多个帮助子类中的某个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。实例代码:CarFactory类:1 public abstract class CarFactory2 {3 public abstract Car CarCreat
3、e();4 }Car类:1 public abstract class Car2 {3 public abstract void StartUp();4 public abstract void Run();5 public abstract void Stop();6 7 }HongQiCarFactory类:1 public class HongQiCarFactory:CarFactory2 {3 public ove
4、rride Car CarCreate()4 {5 return new HongQiCar();6 }7 }BMWCarFactory类:1 public class BMWCarFactory:CarFactory2 {3 public override Car CarCreate()4 {5 return new BMWCar();6 }7 }HongQiCar类: 1 publi
5、c class HongQiCar:Car 2 { 3 public override void StartUp() 4 { 5 Console.WriteLine("Test HongQiCar start-up speed!"); 6 } 7 public override void Run() 8 { 9 Console.WriteLine("The HongQiCar run is very quickly!");
6、10 }11 public override void Stop()12 {13 Console.WriteLine("The slow stop time is 3 second ");14 }15 }BMWCar类: 1 public class BMWCar:Car 2 { 3 public override void StartUp() 4 { 5 Console.WriteLine
7、("The BMWCar start-up speed is very quickly"); 6 } 7 public override void Run() 8 { 9 Console.WriteLine("The BMWCar run is quitely fast and safe!!!");10 }11 public override void Stop()12 {13 Console.Write
8、Line("The slow stop time is 2 second");14 }15 }app.config1 2