1.简介
相比于简单工厂,工厂方法是使用一个工厂类去创建一个对象
IRace接口和Human类、NE类都和上文简单工厂一样
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public interface IRace { void ShowKing(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { class Human : IRace { public void ShowKing() { Console.WriteLine("这里是人类的国王"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { class NE : IRace { public void ShowKing() { Console.WriteLine("这里是精灵族的国王"); } } }
然后我们添加一个Human工厂HumanFactory,用这个类来实例化Human
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class HumanFactory { public IRace CreateInstance() { return new Human(); } } }
同理添加一个NE工厂NEFactory
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class NEFactory:IFactory { public IRace CreateInstance() { Console.WriteLine("实例化之前NE执行一些操作"); return new NE(); } } }
Program:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { class Program { static void Main(string[] args) { HumanFactory humanFactory = new HumanFactory(); IRace race1 = humanFactory.CreateInstance(); race1.ShowKing();
//NE同理 Console.Read(); } } }
从这里看,我们可能会觉得工厂方法只会增加代码量,没有什么实际意义,不过在某些场合还是非常适合的
比如:我们需要在实例化之前执行一些操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class HumanFactory { public IRace CreateInstance() { Console.WriteLine("实例化之前Human执行一些操作"); return new Human(); } } }
2.用简单工厂方法管理工厂方法的工厂类
我们也可以结合简单工厂和工厂方法
使用简单工厂方法对工厂类HumanFactory进行管理,对Program解耦
添加一个接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public interface IFactory { IRace CreateInstance(); } }
使HumanFactory和NEFactory继承这个接口
然后添加一个简单工厂类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { public class SimpleFactory { public static IFactory GetFactory(FactoryType factoryType) { switch (factoryType) { case FactoryType.HumanFactory: return new HumanFactory(); case FactoryType.NEFactory: return new NEFactory(); default: throw new Exception("wrong"); } } } public enum FactoryType { HumanFactory, NEFactory } }
Program:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryMethod { class Program { static void Main(string[] args) { IFactory factory1 = SimpleFactory.GetFactory(FactoryType.NEFactory); IRace race1= factory1.CreateInstance(); race1.ShowKing(); IFactory factory2 = SimpleFactory.GetFactory(FactoryType.HumanFactory); IRace race2 = factory2.CreateInstance(); race2.ShowKing(); Console.Read(); } } }
像这种把对多个工厂从细节代码变成抽象代码的方法,也叫做抽象工厂模式
要进一步解耦,可以使用反射,请参考上一章