概念:
有一个工厂类根据传入的参数,动态决定应该创建哪一个产品类的实例
角色和职责:
工厂类:
简单工厂模式的核心,它负责实现创建所有实例的内部逻辑。工厂类可以被客户端直接调用,创建所需要的对象。
抽象产品类:
简单工厂所创建的所有对象的父类,它负责描述所有实例的公共接口。
具体产品类:
是简单工厂模式创建目标,所有创建的对象都是充当这个角色的某一个具体的实例。
优点:
工厂类包含了必要了逻辑判断,根据外部传入的信息决定创建哪一个具体产品对象,通过使用工厂可以直接创建具体的产品对象,负责消费即可,无需考虑这些对象是如何组织和创建的,明确了各自的职责和权利。
缺点:
由于工厂类集中了所有实例的创建逻辑,违反了高内聚责任分配原则,将全部创建逻辑都集中到了一个类中,此类将会较为复杂,它需要创建的对象需要事先考虑到,如果需要添加新的类,就只能需要改动工厂类的代码了。
UML:
Demo:
namespace SampleFactory { public class SimpleFactory { /// <summary> /// opertaion enum /// </summary> public enum OperationEnum { None = 0, Add, Sub, Mul, Div } /// <summary> /// create operation by factory /// </summary> /// <param name="operation"></param> /// <returns></returns> public static Operation Create(OperationEnum operation) { Operation oper = null; switch(operation) { case OperationEnum.Add: oper = new Add(); break; case OperationEnum.Sub: oper = new Sub(); break; case OperationEnum.Mul: oper = new Mul(); break; case OperationEnum.Div: oper = new Div(); break; } return oper; } } /// <summary> /// operation interface /// </summary> public class Operation { public double NumA { get; set; } public double NumB { get; set; } public virtual double GetResult() { double result = 0; return result; } } /// <summary> /// add /// </summary> public class Add : Operation { public override double GetResult() { double result = 0; result = NumA + NumB; return result; } } /// <summary> /// sub /// </summary> public class Sub : Operation { public override double GetResult() { double result = 0; result = NumA - NumB; return result; } } /// <summary> /// mul /// </summary> public class Mul : Operation { public override double GetResult() { double result = 0; result = NumA * NumB; return result; } } /// <summary> /// div /// </summary> public class Div : Operation { public override double GetResult() { double result = 0; result = NumA / NumB; return result; } } } // Client class Program { static void Main(string[] args) { // use factory create object to do something Operation operation = SimpleFactory.Create(SimpleFactory.OperationEnum.Add); operation.NumA = 1; operation.NumB = 2; Console.WriteLine("result:" +operation.GetResult()); operation = SimpleFactory.Create(SimpleFactory.OperationEnum.Sub); operation.NumA = 1; operation.NumB = 2; Console.WriteLine("result:" + operation.GetResult()); operation = SimpleFactory.Create(SimpleFactory.OperationEnum.Mul); operation.NumA = 1; operation.NumB = 2; Console.WriteLine("result:" + operation.GetResult()); operation = SimpleFactory.Create(SimpleFactory.OperationEnum.Div); operation.NumA = 1; operation.NumB = 2; Console.WriteLine("result:" + operation.GetResult()); } }
enum OperationEnum { None = 0, eAdd, eSub, eMul, eDiv }; // define operation interface class Operation { public: void SetNumA(double num); void SetNumB(double num); double GetNumA(); double GetNumB(); virtual double GetResult() { return 0; }; double NumA; double NumB; }; // add class Add : public Operation { public: double GetResult() { return NumA + NumB; }; }; // sub class Sub : public Operation { public: double GetResult() { return NumA - NumB; }; }; // mul class Mul :public Operation { public: double GetResult() { return NumA * NumB; }; }; // div class Div :public Operation { public: double GetResult() { return NumA / NumB; }; }; // the simple factory class SimpleFactory { public: static Operation* Create(OperationEnum opEnum) { Operation* operation; switch (opEnum) { case eAdd: operation = new Add(); break; case eSub: operation = new Sub(); break; case eMul: operation = new Mul(); break; case eDiv: operation = new Div(); break; default: break; } return operation; } }; // Client Operation* operation = SimpleFactory::Create(eAdd); operation->NumA = 1; operation->NumB = 2; cout << "result:" << operation->GetResult() << endl; delete operation; operation = NULL; operation = SimpleFactory::Create(eSub); operation->NumA = 1; operation->NumB = 2; cout << "result:" << operation->GetResult() << endl; delete operation; operation = NULL; operation = SimpleFactory::Create(eMul); operation->NumA = 1; operation->NumB = 2; cout << "result:" << operation->GetResult() << endl; delete operation; operation = NULL; operation = SimpleFactory::Create(eDiv); operation->NumA = 1; operation->NumB = 2; cout << "result:" << operation->GetResult() << endl; delete operation; operation = NULL;