.NET简单工厂模式讲解

简单工厂模式介绍:

简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。

结构模式图:

.NET简单工厂模式讲解

角色分类:

工厂(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>天数</param> /// <param>每天单价</param> /// <param>日平均绩效</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>月数</param> /// <param>月单价</param> /// <param>月平均绩效</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(); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyzjx.html