1. 簡單工廠模式簡介
1.1 定義
簡單工廠模式:定義一個Factory類,可以根據引數的不同返回不同類的例項,被建立的例項通常有共同的父類。
簡單工廠模式:只需要一個Factory類。
簡單工廠模式:又稱為靜態工廠模式(Static Factory Pattern),Factory類為靜態類或包含靜態方法。
簡單工廠模式:不屬於23種GOF設計模式。
簡單工廠模式:實質是由一個工廠類根據傳入的引數,動態決定應該建立哪一個產品類例項。
1.2 使用頻率
中
2. 簡單工廠模式結構
2.1 結構圖
2.2 參與者
簡單工廠模式參與者:
◊ Product:抽象產品類,將具體產品類公共的程式碼進行抽象和提取後封裝在一個抽象產品類中。
◊ ConcreteProduct:具體產品類,將需要建立的各種不同產品物件的相關程式碼封裝到具體產品類中。
◊ Factory:工廠類,提供一個工廠類用於建立各種產品,在工廠類中提供一個建立產品的工廠方法,該方法可以根據所傳入引數的不同建立不同的具體產品物件。
◊ Client:客戶端類,只需呼叫工廠類的工廠方法並傳入相應的引數即可得到一個產品物件。
3. 簡單工廠模式結構實現
3.1 Product類抽象實現
Product.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Structural { public abstract class Product { } }
ConcreteProduct.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Structural { public class ConcreteProduct : Product { } }
Factory.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Structural { public class Factory { /// <summary> /// 靜態方法建立Product例項 /// </summary> public static Product CreateProduct() { return new ConcreteProduct(); } } }
Program.cs
using System; using System.Collections.Generic; using System.Text; using DesignPatterns.SimpleFactoryPattern.Structural; namespace DesignPatterns.SimpleFactoryPattern { class Program { static void Main(string[] args) { Product product = Factory.CreateProduct(); Console.WriteLine("Created {0}", product.GetType().Name); } } }
執行結果:
Created ConcreteProduct
請按任意鍵繼續. . .
3.2 Product介面類實現
IProduct.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation { public interface IProduct { void Display(); } }
Product.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation { public class Product : IProduct { public void Display() { Console.WriteLine("DesignPatterns.SimpleFactoryPattern.Structural.Product"); } } }
Factory.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation { public class Factory { /// <summary> /// Factory返回IProduct的靜態方法 /// </summary> /// <returns></returns> public static IProduct Create() { // 使用new直接建立介面的具體類 //return new DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation.Product(); // 通過對映建立介面的具體類 return (IProduct)Assembly.Load("DesignPatterns.SimpleFactoryPattern").CreateInstance("DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation.Product"); } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation; namespace DesignPatterns.SimpleFactoryPattern { class Program { static void Main(string[] args) { IProduct product = Factory.Create(); product.Display(); } } }
4. 簡單工廠模式實踐應用
4.1 實踐應用——運算操作
Operation.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { /// <summary> /// 運算類 /// </summary> public abstract class Operation { public double NumberA { get; set; } public double NumberB { get; set; } public virtual double GetResult() { const double result = 0; return result; } } }
Plus.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { /// <summary> /// 加法運算 /// </summary> public class Plus : Operation { public override double GetResult() { return NumberA + NumberB; } } }
Minus.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { /// <summary> /// 減法運算 /// </summary> public class Minus : Operation { public override double GetResult() { return NumberA - NumberB; } } }
Multiply.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { public class Multiply : Operation { public override double GetResult() { return NumberA * NumberB; } } }
Divide.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { public class Divide :Operation { public override double GetResult() { return NumberA / NumberB; } } }
OperationFactory.cs
using System; using System.Collections.Generic; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { public class OperationFactory { public static Operation CreateOperate(string operate) { Operation operation = null; switch (operate) { case "+": operation = new Plus(); break; case "-": operation = new Minus(); break; case "*": operation = new Multiply(); break; case "/": operation = new Divide(); break; } return operation; } } }
Program.cs
using System; using System.Collections.Generic; using System.Text; using DesignPatterns.SimpleFactoryPattern.Practical; namespace DesignPatterns.SimpleFactoryPattern { class Program { static void Main(string[] args) { Operation operateion = OperationFactory.CreateOperate("+"); operateion.NumberA = 10; operateion.NumberB = 5; Console.WriteLine(operateion.GetResult()); } } }
4.2 實踐應用——銀行支付介面
IPayment.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { public interface IPayment { bool Payfor(decimal money); } }
ABCPayment.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { public class ABCPayment : IPayment { public bool Payfor(decimal money) { // 呼叫中國農業銀行支付介面進行支付 return true; } } }
ICBCPayment.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { public class ICBCPayment : IPayment { public bool Payfor(decimal money) { // 呼叫中國工商銀行支付介面進行支付 return true; } } }
PaymentFactory.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { public class PaymentFactory { public static IPayment CreatePayment(string bank) { IPayment payment = null; switch (bank) { case "ABC": payment = new ABCPayment(); break; case "ICBC": payment = new ICBCPayment(); break; } return payment; } } }
OrderService.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical { public class OrderService { public bool CreateOrder(string bank) { decimal money=100m; var payment = PaymentFactory.CreatePayment(bank); return payment.Payfor(money); } } }
在OrderService類中,不依賴具體的支付類,只通過PaymentFactory來獲取真正的支付類。
5. 簡單工廠模式應用分析
5.1 簡單工廠模式優點
◊ 實現建立和使用分離;
◊ Client無需知道所建立的ConcreteProduct類名,只需要知道ConcreteProduct所對應的引數。
5.2 簡單工廠模式缺點
◊ Factory類集中所有ConcreteProduct的建立邏輯,職責過重。一旦需要新增新的ConcreteProduct,則需要修改Factory邏輯。這樣違背了OCP(開放-關閉原則)。
◊ 由於使用了static方法,造成Factory無法形成基於繼承的結構。