服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#设计模式之工厂模式

C#设计模式之工厂模式

2023-02-17 11:55.NET开发菜鸟 C#

本文详细讲解了C#设计模式之工厂模式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

C#设计模式之工厂模式

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

C#设计模式之工厂模式

示例代码:

抽象产品类:

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();
        }
    }
}

类图:

C#设计模式之工厂模式

如果想在增加一个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#设计模式之工厂模式的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文地址:https://www.cnblogs.com/dotnet261010/p/7352541.html

延伸 · 阅读

精彩推荐
  • C#C#/VB.NET 自定义PPT动画路径的步骤

    C#/VB.NET 自定义PPT动画路径的步骤

    这篇文章主要介绍了C#/VB.NET 自定义PPT动画路径的步骤,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...

    E-iceblue6562022-11-16
  • C#C#组件FormDragger窗体拖拽器详解

    C#组件FormDragger窗体拖拽器详解

    这篇文章主要为大家详细介绍了C#组件FormDragger窗体拖拽器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    AhDung11882021-12-30
  • C#winform实现限制及解除鼠标移动范围的方法

    winform实现限制及解除鼠标移动范围的方法

    这篇文章主要介绍了winform实现限制及解除鼠标移动范围的方法,涉及C#控制WinForm鼠标事件属性的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    C#教程网9132021-10-28
  • C#c# RSA非对称加解密及XML&PEM格式互换方案

    c# RSA非对称加解密及XML&PEM格式互换方案

    这篇文章主要介绍了c# RSA非对称加解密及XML&PEM格式互换方案,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    梦在旅途8772022-10-26
  • C#C#使用NOPI库实现导入Excel文档

    C#使用NOPI库实现导入Excel文档

    NPOI中N指代的是.Net,POI是一个完全开源的Java写成的库,能够在没有安装微软Office或者相应环境的情况下读写Excel、Word等微软OLE2组件文档,几乎支持所有的...

    易墨5442022-01-04
  • C#C#循环与循环控制的表达式树实现

    C#循环与循环控制的表达式树实现

    这篇文章介绍了C#循环与循环控制的表达式树实现,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参...

    痴者工良3352022-12-20
  • C#Unity3D Shader实现扫描显示效果

    Unity3D Shader实现扫描显示效果

    这篇文章主要为大家详细介绍了Unity3D Shader实现扫描显示效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    星空不语5822022-07-08
  • C#Unity3D创建圆柱体的方法

    Unity3D创建圆柱体的方法

    这篇文章主要为大家详细介绍了Unity3D创建圆柱体的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    JayW就是我吖11952022-03-10