C#设计模式之工厂模式

Rhea ·
更新时间:2024-09-20
· 1013 次阅读

这是我们用得比较多的一种设计模式,也是23种标准设计模式之一,使用前面讲的简单工厂设计模式,遇到具体产品经常变换时就不太适合了,违反了开闭设计原则;怎么才能避免修改工厂类呢?工厂方法模式可以做到。
工厂方法模式要求我们应该有一个抽象的工厂类,我们知道尽量使用抽象类或接口来定义就可以达到一个开闭原则的效果,这样我们在抽象的工厂类定义一个生产产品的方法,这个方法就是工厂方法,这也是工厂方法模式的由来,他具体的行为会有他的子类或实现类来实现。如果想生产某种产品,就定义一个新的产品,新的产品工厂类,这样就实现了不同的产品进行一个不同的创建,这样如果有信的产品,只需要添加新的工厂类,原来写好的代码不会发生变化,这种方式符合开闭原则,可扩展比较好。 

添加一个具体产品,只需要在添加一个具体产品的工厂类实现抽象工厂类,不需要修改原来的代码

示例代码:

抽象产品类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /* 动物抽象类 * 抽象产品 */ public abstract class Animal { public abstract void Eat(); } }

抽象工厂类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /* 动物抽象工厂类 */ public abstract class AnimalFactory { public abstract Animal GetAnimal(); } }

生产狗的具体工厂类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /// <summary> /// 具体工厂:生成狗 /// </summary> public class DogFactory :AnimalFactory { public override Animal GetAnimal() { return new Dog(); } } }

生产企鹅的具体工厂类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /// <summary> /// 具体工厂:生成企鹅 /// </summary> public class PenguinFactory :AnimalFactory { public override Animal GetAnimal() { return new Penguin(); } } }

具体产品狗类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /* 具体的产品类,实现抽象产品类 */ public class Dog:Animal { // 实现抽象方法 public override void Eat() { Console.WriteLine("狗在吃饭!"); } } }

具体产品企鹅类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /* 具体产品类,实现抽象产品类 */ public class Penguin : Animal { // 实现抽象方法 public override void Eat() { Console.WriteLine("企鹅在吃饭!"); } } }

客户端调用:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { class Program { static void Main(string[] args) { AnimalEat(new DogFactory()); Console.ReadKey(); } static void AnimalEat(AnimalFactory af) { Animal am = af.GetAnimal(); am.Eat(); } } }

类图:

如果想在增加一个Cat类,只需要增加一个具体的Cat类实现Animal类的方法,增加一个具体的Cat工厂类实现抽象工厂类即可,不需要在修改已经写好的代码,符合开闭原则。

使用接口的方式实现工厂模式

需求:使用面向对象的方式设计一个系统,描述使用卡车从事货运,使用公共汽车从事客运。使用工厂模式实现。

1、汽车接口:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /// <summary> /// 汽车接口 /// </summary> public interface ICar { /// <summary> /// 描述汽车从事的活动 /// </summary> void Work(); } }

2、分别定义卡车(Truck)和公共汽车(Bus)类实现汽车接口(ICar)里面的Work()方法

Truck类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /// <summary> /// 卡车类 /// </summary> public class Truck : ICar { /// <summary> /// 卡车从事的活动 /// </summary> public void Work() { Console.WriteLine("卡车从事货运"); } } }

Bus类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /// <summary> /// 公共汽车类 /// </summary> public class Bus:ICar { /// <summary> /// 公共汽车从事的活动 /// </summary> public void Work() { Console.WriteLine("公共汽车从事客运"); } } }

3、定义汽车的工厂接口(ICarFactory):

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /// <summary> /// 汽车工厂接口 /// </summary> public interface ICarFactory { ICar GetCar(); } }

4、分别定义卡车工厂(TruckFactory)和公共汽车工厂(BusFactory)实现ICarFactory接口

TruckFactory工厂:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /// <summary> /// 卡车工厂接口 /// </summary> public class TruckFactory:ICarFactory { /// <summary> /// 返回卡车类 /// </summary> /// <returns></returns> public ICar GetCar() { return new Truck(); } } }

BusFactory工厂:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { /// <summary> /// 公共汽车工厂 /// </summary> public class BusFactory:ICarFactory { /// <summary> /// 返回公共汽车类 /// </summary> /// <returns></returns> public ICar GetCar() { return new Bus(); } } }

5、主程序调用:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 工厂模式 { class Program { static void Main(string[] args) { CarWork(new TruckFactory()); Console.ReadKey(); } /// <summary> /// 根据汽车工厂返回具体的汽车类 /// </summary> /// <param name="cf"></param> static void CarWork(ICarFactory cf) { ICar c = cf.GetCar(); c.Work(); } } }

6、程序运行结果

代码下载地址:点击下载

到此这篇关于C#设计模式之工厂模式的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持软件开发网。



C# 设计模式 工厂模式

需要 登录 后方可回复, 如果你还没有账号请 注册新账号