出自:http://www.cnblogs.com/libingql/p/3887577.html

1.1 定义

  简单工厂模式定义一个Factory类,可以根据参数的不同返回不同类的实例,被创建的实例通常有共同的父类。

  简单工厂模式只需要一个Factory类。

  简单工厂模式又称为静态工厂模式,Factory类为静态类或包含静态方法。

1.2 使用频率

  C#设计模式系列:简单工厂模式(Simple Factory) 中

2. 简单工厂模式结构

2.1 结构图

C#设计模式系列:简单工厂模式(Simple Factory)

2.2 参与者

  简单工厂模式参与者:

  ◊ Product:抽象产品类,将具体产品类公共的代码进行抽象和提取后封装在一个抽象产品类中。

  ◊ ConcreteProduct:具体产品类,将需要创建的各种不同产品对象的相关代码封装到具体产品类中。

  ◊ Factory:工厂类,提供一个工厂类用于创建各种产品,在工厂类中提供一个创建产品的工厂方法,该方法可以根据所传入参数的不同创建不同的具体产品对象。

  ◊ Client:客户端类,只需调用工厂类的工厂方法并传入相应的参数即可得到一个产品对象。

3. 简单工厂模式结构实现

3.1 Product类抽象实现

  Product.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Text;

namespace DesignPatterns.SimpleFactoryPattern.Structural
{
    public abstract class Product
    {
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

  ConcreteProduct.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Text;

namespace DesignPatterns.SimpleFactoryPattern.Structural
{
    public class ConcreteProduct : Product
    {
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

  Factory.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Text;

namespace DesignPatterns.SimpleFactoryPattern.Structural
{
    public class Factory
    {
        /// <summary>
        /// 静态方法创建Product实例
        /// </summary>
        public static Product CreateProduct()
        {
            return new ConcreteProduct();
        }
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

  Program.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Text;

using DesignPatterns.SimpleFactoryPattern.Structural;

namespace DesignPatterns.SimpleFactoryPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Product product = Factory.CreateProduct();
            Console.WriteLine("Created {0}", product.GetType().Name);
        }
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

  运行结果:

Created ConcreteProduct
请按任意键继续. . .

3.2 Product接口类实现

  IProduct.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Text;

namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation
{
    public interface IProduct
    {
        void Display();
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

  Product.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Text;

namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation
{
    public class Product : IProduct
    {
        public void Display()
        {
            Console.WriteLine("DesignPatterns.SimpleFactoryPattern.Structural.Product");
        }
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

  Factory.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;

namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation
{
    public class Factory
    {
        /// <summary>
        /// Factory返回IProduct的静态方法
        /// </summary>
        /// <returns></returns>
        public static IProduct Create()
        {
            // 使用new直接创建接口的具体类
            //return new DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation.Product();

            // 通过映射创建接口的具体类
            return (IProduct)Assembly.Load("DesignPatterns.SimpleFactoryPattern").CreateInstance("DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation.Product");
        }
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

  Program.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation;

namespace DesignPatterns.SimpleFactoryPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            IProduct product = Factory.Create();
            product.Display();
        }
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

4. 简单工厂模式实践应用

4.1 实践应用——运算操作

  Operation.cs

C#设计模式系列:简单工厂模式(Simple Factory)
using System;
using System.Collections.Generic;
using System.Text;

namespace DesignPatterns.SimpleFactoryPattern.Practical
{
    /// <summary>
    /// 运算类
    /// </summary>
    public abstract class Operation
    {
        public double NumberA { get; set; }
        public double NumberB { get; set; }

        public virtual double GetResult()
        {
            const double result = 0;
            return result;
        }
    }
}
C#设计模式系列:简单工厂模式(Simple Factory)

相关文章:

  • 2022-01-11
  • 2021-07-19
  • 2021-07-29
  • 2021-08-05
  • 2022-12-23
  • 2022-01-20
  • 2021-08-15
猜你喜欢
  • 2021-09-14
  • 2021-06-27
  • 2021-09-16
  • 2021-10-30
  • 2021-06-17
  • 2021-06-26
相关资源
相似解决方案