重构工厂而不违反开放式关闭原则

时间:2014-03-11 12:47:35

标签: c# design-patterns refactoring factory-pattern

我有两个版本的工厂类,基于文章

设计

http://www.oodesign.com/factory-pattern.html

public abstract class Employee
{
    public string Name { get; set; }
    protected string Role { get; set; }
    public abstract string GetRole();
}

public class Manager : Employee
{
    public Manager()
    {
        Role = "MGR";
    }
    public override string GetRole()
    {
        return this.Role;
    }
}

版本1:简单,违反开放式关闭原则

每次添加新的具体类时都需要更改SimpleEmployeeFactory

public class SimpleEmployeeFactory
{
    public static Employee GetEmployee(int typeId)
    {
        switch (typeId)
        {
            case 1:
                return new Manager();
            case 2:
                return new TechnicalLead();
            default:
                return null; //if the id doesn't have any 
        }
    }
}

版本2:

Refactored Factory,在我们使用工厂调用之前仍然需要创建一个Concrete类

public abstract class Employee
{
    public string Name { get; set; }
    protected string Role { get; set; }
    public abstract string GetRole();      
    public abstract Employee createEmployee();
} 

public class ChiefTechnologyOfficer : Employee
{
    public ChiefTechnologyOfficer()
    {
        this.Role = "CTO";
    }
    static ChiefTechnologyOfficer()
    {            
        RefactoredFactory.Instance.registerEmployee(5, new ChiefTechnologyOfficer());
    }
    public override string GetRole()
    {            
        return this.Role;
    }        
    public override Employee createEmployee()
    {
        return new ChiefTechnologyOfficer();
    }
}

class RefactoredFactory
{
    private static readonly RefactoredFactory instance = new RefactoredFactory();

    static RefactoredFactory()
    {
    }

    private RefactoredFactory()
    {
    }

    public static RefactoredFactory Instance
    {
        get
        {
            return instance;
        }
    }

    private Dictionary<int, Employee> registeredEmployees = new Dictionary<int, Employee>();

    public void registerEmployee(int typeId, Employee employeeInst)
    {
        registeredEmployees.Add(typeId, employeeInst);
    }

    public Employee createEmployee(int typeId)
    { 
        return ((Employee)registeredEmployees[typeId]).createEmployee();
    }
}

客户端

 Employee emp = SimpleEmployeeFactory.GetEmployee(1);
 Activator.CreateInstance(typeof(ChiefTechnologyOfficer)); //Avoid
 Employee empFNoR = RefactoredFactory.Instance.createEmployee(5);

您可以看到Activator.CreateInstance(typeof(ChiefTechnologyOfficer))调用以使具体类在Factory中注册。否则我们无法检索对象

有没有办法创建一个没有违反OCP原则的工厂类&amp;没有创建像我在RefactoredFactory类中使用的对象?

2 个答案:

答案 0 :(得分:3)

看起来typeId受到功能羡慕的影响。相反,定义一个多态类型来捕获类型;例如界面:

public interface IEmployeeType
{
    Employee Create()
}

现在您可以定义,例如ManagerTypeTechnicalLeadType等。示例:

public class ManagerType : IEmployeeType
{
    public Employee Create()
    {
        return new Manager();
    }
}

这基本上是Abstract Factory,它的优势在于,当您需要创建新的子类型时,您始终可以创建新的实现。

如果您位于系统的边界,并且必须将原始值(如整数)转换为多态值,则可以使用Role Hint patterns之一 - 特别是{{ 3}},Metadata或(我最喜欢的)Role Interface

<强>客户端

给定IEmployeeType实例employeeType,客户端只需:

Employee emp = employeeType.Create();

答案 1 :(得分:0)

塔莱, 而不是使用静态构造函数,register方法来填充类型的字典。 您可以使用基于配置的解决方案,如.net DbProviderFactory,来注册所有类型。

<EmployeeFactories>
  <add name="manger"  type="Manager, EmployeeAssmbly" />
..
</EmployeeFactories>