抽象工厂

  抽象工厂,即工厂的工厂,该模式属于创建型模式,即创建对象。

  本例中,将交通工具抽象,分为三大类:航空、火车、汽车。围绕三大类分别对应三个工厂,依次为:AirplaneFactory、TrainFactory、BusFactory,抽象工厂即交通工具为TrafficFactory 。三类交通工具都需要实现自己的交通方式,所以每一类对应一个接口,依次为 IAirplane、ITrain、IBus 。三类交通工具继承抽象交通工具,并实现各自对应的交通方式(飞行、铁轨、高速公路)。其中,每一种交通工具都可以按照自己的类别再一次划分,比如,航空可再划分为东方航空、南方航空等,火车可以划分为高铁、动车等。所以,还需要建立对应的具体交通工具。

具体交通工具:

1     public class EasternAir : IAirplane
2     {
3         public string Fly()
4         {
5             return "Eastern Air Fly";
6         }
7     }
EasternAir
1     public class SouthernAir : IAirplane
2     {
3         public string Fly()
4         {
5             return "SouthernAir Air Fly";
6         }
7     }
SouthernAir
1     public class BulletTrains : ITrain
2     {
3         public string Railroad()
4         {
5             return "Bullet Trains Railroad";
6         }
7     }
BulletTrains
1     public class RailsHigh : ITrain
2     {
3         public string Railroad()
4         {
5             return "CRH(China Rails High) Railroad";
6         }
7     }
RailsHigh

交通工具对的交通方式:

1     public interface IAirplane
2     {
3         string Fly();
4     }
IAirplane
1     public interface ITrain
2     {
3         string Railroad();
4     }
ITrain
1     public interface IBus
2     {
3     }
IBus

交通工具抽象工厂:

 1     /// <summary>
 2     /// 抽象工厂类,工厂的工厂
 3     /// 对交通工具的抽象
 4     /// </summary>
 5     public abstract class TrafficFactory
 6     {
 7         public abstract IAirplane Airplane(string flightCompany);
 8 
 9         public abstract ITrain Train(string trainType);
10 
11         public abstract IBus Bus(string busType);
12     }
TrafficFactory

交通工具工厂:

 1     /// <summary>
 2     /// 飞机工厂,继承抽象交通工具类,重写飞机
 3     /// </summary>
 4     public class AirplaneFactory : TrafficFactory
 5     {
 6         public override IAirplane Airplane(string flightCompany)
 7         {
 8             IAirplane airplane = null;
 9             switch (flightCompany)
10             {
11                 case "ceair":
12                     airplane = new EasternAir();
13                     break;
14                 case "csair":
15                     airplane = new SouthernAir();
16                     break;
17                 default:
18                     break;
19             }
20             return airplane;
21         }
22 
23         public override IBus Bus(string busType)
24         {
25             throw new NotImplementedException();
26         }
27 
28         public override ITrain Train(string trainType)
29         {
30             throw new NotImplementedException();
31         }
32     }
AirplaneFactory
 1     /// <summary>
 2     /// 火车工厂,继承抽象交通工具类,重写火车
 3     /// </summary>
 4     public class TrainFactory : TrafficFactory
 5     {
 6         public override IAirplane Airplane(string flightCompany)
 7         {
 8             throw new NotImplementedException();
 9         }
10 
11         public override IBus Bus(string busType)
12         {
13             throw new NotImplementedException();
14         }
15 
16         public override ITrain Train(string trainType)
17         {
18             ITrain train = null;
19             switch (trainType)
20             {
21                 case "bullet":
22                     train = new BulletTrains();
23                     break;
24                 case "CRH":
25                     train = new RailsHigh();
26                     break;
27                 default:
28                     break;
29             }
30             return train;
31         }
32     }
TrainFactory
 1     /// <summary>
 2     /// 汽车工厂,继承抽象交通工具类,重写汽车
 3     /// </summary>
 4     public class BusFactory : TrafficFactory
 5     {
 6         public override IAirplane Airplane(string flightCompany)
 7         {
 8             throw new NotImplementedException();
 9         }
10 
11         public override IBus Bus(string busType)
12         {
13             return null;
14         }
15 
16         public override ITrain Train(string trainType)
17         {
18             throw new NotImplementedException();
19         }
20     }
BusFactory

工厂的工厂:

 1     public class Factory
 2     {
 3         public static TrafficFactory GetTrafficFactory(string trafficType)
 4         {
 5             TrafficFactory trafficFactory = null;
 6 
 7             switch (trafficType)
 8             {
 9                 case "AIRPLANE":
10                     trafficFactory = new AirplaneFactory();
11                     break;
12                 case "TRAIN":
13                     trafficFactory = new TrainFactory();
14                     break;
15                 case "BUS":
16                     trafficFactory = new BusFactory();
17                     break;
18                 default:
19                     break;
20             }
21 
22             return trafficFactory;
23         }
24     }
Factory

客户端:

 1         static void Main(string[] args)
 2         {
 3             //航空
 4             var airplane = AbstractFactory.Factory.GetTrafficFactory("AIRPLANE");
 5             //东方航空
 6             var ceair = airplane.Airplane("ceair");
 7             Console.WriteLine(ceair.Fly());
 8             //南方航空
 9             var csair = airplane.Airplane("csair");
10             Console.WriteLine(csair.Fly());
11 
12             //火车
13             var train = AbstractFactory.Factory.GetTrafficFactory("TRAIN");
14             //高铁
15             var bullet = train.Train("bullet");
16             Console.WriteLine(bullet.Railroad());
17             //动车
18             var crh = train.Train("CRH");
19             Console.WriteLine(crh.Railroad());
20 
21             Console.ReadLine();
22         }
Main

  本例中的交通工具分类不够严格,比如航空按照航空公司分类,但火车却没有按照承运段分。

posted on 2018-10-14 01:39  庭前花满留晚照  阅读(193)  评论(0)    收藏  举报

导航