簡單工廠模式介紹:
簡單工廠模式是屬於創建型模式,又叫做靜態工廠方法(Static Factory Method)模式,但不屬於23種GOF設計模式之一。簡單工廠模式是由一個工廠對象決定創建出哪一種產品類的實例。簡單工廠模式是工廠模式家族中最簡單實用的模式,可以理解為是不同工廠模式的一個特殊實現。
結構模式圖:

角色分類:
工廠(Creator)角色
簡單工廠模式的核心,它負責實現創建所有實例的內部邏輯。工廠類的創建產品類的方法可以被外界直接調用,創建所需的產品對象。
抽象產品(Product)角色
簡單工廠模式所創建的所有對象的父類,它負責描述所有實例所共有的公共接口。
具體產品(Concrete Product)角色
是簡單工廠模式的創建目標,所有創建的對象都是充當這個角色的某個具體類的實例。
引入實際情況:
如果有一個住戶管理系統,裡面的住戶類型是可變的,每一種租戶類型的租金計算公式都存在差異
A類型的住戶租金額=天數*單價+績效*0.005
B類型的住戶租金額=月份*(每月價格+performance*0.001)
分析:
1. 商店存在共有的計算方法,這是實體商店的行為,然而他們的行為的方式不一樣,所有我們抽象商店類,代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleFactory.App.IFactroy
{
public interface Ishop
{
double Getrent(int days, double dayprice, double performance);
}
}
2.在抽象了商店之後,我們要對創建具體產品類,這裡就是具體的類型商店,裡面實現該商店的行為方法。創建A類型的商店
using SimpleFactory.App.IFactroy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleFactory.App.product
{
//A類型的商店的創建
public class Ashop:Ishop
{
/// <summary>
/// /// A類型商店租金額,天數*單價+績效*0.005
/// </summary>
/// <param name="days">天數</param>
/// <param name="dayprice">每天單價</param>
/// <param name="performance">日平均績效</param>
/// <returns></returns>
public double Getrent(int days, double dayprice, double performance)
{
Console.WriteLine("A商店的租金算法");
return days * dayprice + performance * 0.01;
}
}
}
3.創建B類型的商店:
using SimpleFactory.App.IFactroy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleFactory.App.product
{
/// <summary>
/// B類型的商店的創建
/// </summary>
public class Bshop:Ishop
{
/// <summary>
/// B類型商店的租金=月份*(每月價格+performance*0.001)
/// </summary>
/// <param name="month">月數</param>
/// <param name="monthprice">月單價</param>
/// <param name="performance">月平均績效</param>
/// <returns></returns>
public double Getrent(int month, double monthprice, double performance)
{
Console.WriteLine("B商店的租金算法");
return month * (monthprice + performance * 0.001);
}
}
}
4. 在創建號類型商店並實現方法後,思考在什麼情況下如何創建那種對象,於是簡單工廠模式中最核心的部分:工廠類出來了
using SimpleFactory.App.IFactroy;
using SimpleFactory.App.product;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleFactory.App.factoryMethod
{
public class factorymethod
{
public Ishop CreateShow(string show)
{
switch (show.Trim().ToLower())
{
case"ashop":
return new Ashop();
case "bshop":
return new Ashop();
default:
throw new Exception("該商店不存在");
}
}
}
}
5.然後就根據當前的商店類型進行判斷,該類型的商店應該進行哪一種算法:
using SimpleFactory.App.factoryMethod;
using SimpleFactory.App.IFactroy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleFactory.App
{
class Program
{
static void Main(string[] args)
{
Ishop As;
factorymethod afm = new factorymethod();
As = afm.CreateShow("ashop"); //a 類型的某商店
double total = As.Getrent(30, 300, 2000); //30 天/100元 日平均績效為2000
Console.WriteLine("該A類型商店的租金為:" + total);
Console.WriteLine("=============");
Ishop Bs;
factorymethod bfm = new factorymethod();
Bs = bfm.CreateShow("bshop"); //b 類型的某商店
total = Bs.Getrent(3, 3000, 60000); //3 月/4000元 月平均績效為60000
Console.WriteLine("該B類型商店的租金為:" + total);
Console.ReadKey();
}
}
}
到這裡我們實現了客戶要求的兩種類型商店的算法的需求,但是作為一種好的設計架構,還應該考慮到後面的需求變革,如果客戶現在又增加了C類型商店和D類型商店,它們的算法要求又不一樣,這個時候我們就需要進行C,D類型商店的創建,並繼承Ishop接口,實現裡面的方法,同時還得繼續修改工廠類在switc中增加case進行捕捉創建相應的商店對象,一旦出現這樣的情況,是不利於程序的擴展性和項目後期的維護性的。
優點:
缺點:
出現的上訴情況,應該如何解決,值得思考,將在下一個工廠方法模式中得到很好的解決。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持幫客之家。