[Creative Design Pattern] The factory pattern of C# design pattern, and the realization of dynamic factory through reflection.

The topics are as follows:

假设你正在为一家汽车制造公司编写软件。公司生产多种类型的汽车,包括轿车、SUV和卡车。每种汽车都有不同的特点和功能。

请设计一个工厂模式,用于创建不同类型的汽车对象。该工厂模式应具有以下要求:

工厂类名为 CarFactory,包含一个静态方法 CreateCar,根据传入的参数类型,返回对应类型的汽车对象。
汽车类 Car 是一个抽象类,包含一个抽象方法 Drive,用于描述汽车的驾驶行为。
轿车类 Sedan 继承自 Car 类,实现了 Drive 方法,在 Drive 方法中输出“驾驶轿车”。
SUV 类继承自 Car 类,实现了 Drive 方法,在 Drive 方法中输出“驾驶SUV”。
卡车类 Truck 继承自 Car 类,实现了 Drive 方法,在 Drive 方法中输出“驾驶卡车”。
使用该工厂模式完成以下操作:

在程序入口处,向 CarFactory 的 CreateCar 方法传入参数 "sedan",并将返回的对象存储到 sedanCar 变量中。
在 sedanCar 上调用 Drive 方法,观察输出结果。
同样地,创建一个 SUV 对象,并调用其 Drive 方法。
创建一个 Truck 对象,并调用其 Drive 方法。
请根据以上要求实现该工厂模式,并编写相应的代码。

Simple factory implementation:

using System;
using System.Reflection;


namespace Factory
{
    
    
    internal class CarFactory
    {
    
    
        public static T CreateCar<T>() where T : Car, new()
        {
    
    
            if (typeof(T) == typeof(Sedan))
                return new Sedan() as T;
            if (typeof(T) == typeof(SUV))
                return new SUV() as T;
            if (typeof(T) == typeof(Truck))
                return new Truck() as T;

            throw new ArgumentException("无法创建该类型的汽车对象。");
        }

     

    }
}

namespace Factory
{
    
    
    public abstract class Car 
    {
    
    

        public abstract void Drive();      
    }
}

using System;


namespace Factory
{
    
    
    internal class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Car sedanCar = CarFactory.CreateCar<Sedan>();
            sedanCar.Drive();

            Car suvCar = CarFactory.CreateCar<SUV>();
            suvCar.Drive();

            Car truckCar = CarFactory.CreateCar<Truck>();
            truckCar.Drive();




            Console.ReadLine();
        }
    }
}

The above code gives the basic definition of the abstract base class, the implementation of the generic factory, and the calling method.
It is worth noting that where T : Car, new()this condition
where T : Car, new()is a constraint on generic type parameters T. This constraint states that generic type parameters Tmust satisfy two conditions:

TMust be Cara class or its derived class: means Tmust be Cara type or Cara subclass (derived class) of the class.

TMust have a no-argument constructor: Through new()the constraint, it means that Tmust have a no-argument constructor, that is, be able to use new T() 创建 Tan instance of .

The purpose of the above constraints is to ensure that generic type parameters Tmeet the requirements during use. The constraint Tmust be Carof type or its derived type to ensure that CreateCarthe object created in the method is Caran instance of the type or its subclass; at the same time, the constraint Tmust have a no-argument constructor to ensure that an instance of can be new T()created by T.

Through such constraints, it can be ensured that when calling CreateCarthe method, the type parameters passed in Tmeet the specified requirements, and Tthe corresponding object instance can be created based on the type inside the method.

工厂模式适用于以下场景:

对象创建复杂:当对象的创建涉及到复杂的逻辑判断、依赖关系或者大量的初始化操作时,可以使用工厂模式来封装对象的创建过程。这样可以简化客户端代码,并提供一个统一的入口来创建对象。

对象类型不确定:当需要根据不同的条件或者配置来创建不同类型的对象时,可以使用工厂模式。通过工厂模式,可以将对象的创建逻辑从客户端代码中分离出来,降低代码的耦合性。

扩展性要求高:当系统中需要添加新的产品或者变化频繁时,使用工厂模式可以方便地扩展和修改代码。通过增加新的具体工厂和产品类,而无需修改已有代码,符合开闭原则。

隐藏对象创建细节:当需要隐藏对象创建的细节时,可以使用工厂模式。客户端只需要与工厂接口进行交互,无需关心具体的产品如何创建或实现。

总的来说,工厂模式适用于对象创建复杂、对象类型不确定、扩展性要求高以及隐藏对象创建细节的情况。它能够提供灵活、可扩展和易于维护的代码结构。

From the code, we can see that CarFactorythis factory class has disadvantages. For example, every time we add a new car object, we need to modify CarFactorythe class CreateCarand add a new object. At this time we can use a dynamic factory to create objects through reflection.

using System;
using System.Reflection;


namespace Factory
{
    
    
    internal class CarFactory
    {
    
    

        public static T CreateCar<T>(string typeName) where T : Car, new()
        {
    
    
            Type type = GetTypeByName(typeName);
            if (type != null && typeof(T).IsAssignableFrom(type))  return Activator.CreateInstance(type) as T;

            throw new ArgumentException("无法创建该类型的汽车对象。");
        }

        private static Type GetTypeByName(string typeName)
        {
    
    
            return Assembly.GetAssembly(typeof(CarFactory)).GetType($"Factory.{
      
      typeName}");
        }

    }
}

using System;


namespace Factory
{
    
    
    internal class Program
    {
    
    
        static void Main(string[] args)
        {
    
    

            // 传递具体类的类型名称作为参数
            string typeName = "Sedan";
            Car sedan = CarFactory.CreateCar<Sedan>(typeName);
            sedan.Drive();
            // 可以根据需要创建不同类型的汽车对象
            typeName = "SUV";
            Car suv = CarFactory.CreateCar<SUV>(typeName);
            suv.Drive();
            typeName = "Truck";
            Car truck = CarFactory.CreateCar<Truck>(typeName);
            truck.Drive();

            Console.ReadLine();
        }
    }
}

In this way, we don't need to modify the core factory code. Dynamically generate an instance
by passing in the assembly where the current type is located. It also means whether the current type can be converted to another type, such as whether it is a base class or a derived class of T. Used to create instances.typeNameAssembly.GetAssembly(typeof(CarFactory))Factory.{typeName} 为 命名空间.类typeof(T).IsAssignableFrom(type)TTypetypeActivator.CreateInstance(type) as T

Guess you like

Origin blog.csdn.net/csdn2990/article/details/132082222