工厂方法模式

 


 

1.UML类图

2.核心代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8     interface IEngineFactory
 9     {
10         Engine CreateEngine();
11     }
12 
13     class GaosolineEngineFactory:IEngineFactory
14     {
15         public Engine CreateEngine()
16         {
17             return new GasolineEngine();
18         }
19     }
20 
21     class SolarEngineFactory : IEngineFactory
22     {
23         public Engine CreateEngine()
24         {
25             return new SolarEngine();
26         }
27     }
28 
29     class NuclearEngineFactory : IEngineFactory
30     {
31         public Engine CreateEngine()
32         {
33             return new NuclearEngine();
34         }
35     }
36 
37 
38 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.Write("请你输入G/S/N,来选择不同动力的小汽车");
13             string Choice = Console.ReadLine();
14 
15             IEngineFactory enginefactory = null;
16 
17             switch (Choice)
18             {
19                 case "G":
20                     enginefactory = new GaosolineEngineFactory();
21                     break;
22                 case "S":
23                     enginefactory = new SolarEngineFactory();
24                     break;
25                 case "N":
26                     enginefactory = new NuclearEngineFactory();
27                     break;
28             }
29 
30             Car car = new Car();
31             car.engine = enginefactory.CreateEngine();
32             car.Drive();
33             Console.ReadLine();
34 
35         }
36     }
37 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8     class Car
 9     {
10         private Engine _engine;
11 
12         public Engine engine
13         {
14             get { return _engine; }
15             set { _engine = value; }
16         }
17 
18         public void Drive()
19         {
20             _engine.Launch();
21 
22             Console.Write("小汽车已启动");
23         }
24     }
25 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8     class Engine
 9     {
10         public virtual void Launch()
11         {
12 
13         }
14     }
15 
16     class GasolineEngine:Engine
17     {
18         public override void Launch()
19         {
20             Console.Write("汽油动力小汽车启动成功!");
21         }
22     }
23 
24     class SolarEngine : Engine
25     {
26         public override void Launch()
27         {
28             Console.Write("太阳能动力小汽车启动成功!");
29         }
30     }
31 
32     class NuclearEngine : Engine
33     {
34         public override void Launch()
35         {
36             Console.Write("核能动力小汽车启动成功!");
37         }
38     }
39 }

 3.view on github

https://github.com/mengx8/test

posted @ 2015-12-31 13:49  mengx8  阅读(110)  评论(0)    收藏  举报