c # design patterns - commissioned factory

1. Here is a simple and typical factory method:

public static Employee CreateEmployee(string name)
    {
        if (name.StartWith("VIP"))
        {
            return new Employee() { Name = name.Replace("VIP", "") };
        }
        return new Employee() { Name = name };
    }

Factory code above logic is that if there are words in the name VIP, VIP will be removed after the creation of an employee.

Change point here is that if statement, when it is determined logic created with the ever-changing needs of the time, will continue to change the factory method could be to modify the original, possibly adding new.

But by the C # language technology, isolation can change point does not change an external point, to ensure the stability of the plant, commissioned by injection logic C # would come.

   public static Employee CreateEmployee(string name,Func<string,bool> logic,Func<string,string> filter)
    {
        if (logic(name))
        {
            return new Employee() { Name = filter(name) };
        }
        return new Employee() { Name = name };
    }

 

Here are just commissioned in the application factory mode into full force commissioned Hui, Hui can send a greater role in the project.

 

Quote ".net Framework Design"

 

Guess you like

Origin www.cnblogs.com/coder-fang/p/11236039.html