dimanche 10 décembre 2017

Abstract Factory or Factory Method for types with different constructor arguments

I have a factory method as given below. Is there a better way to design this so I do not have to use switch statement and achieve open closed principle

public IPolicy CreatePolicy(Context context)
{
    IPolicy policy = default(IPolicy);
    ISettings settings = _settings.Get(context);
    Policy policyType = (Policy) Enum.Parse(typeof(Policy), settings.Policy);
    switch (policyType)
    {
        case Policy.Policy1:
            policy = new Policy1(_policy1Service, _logHandler);
            break;
        case Policy.Policy2:
            policy = new Policy2(_policy2Service, _logHandler);
            break;
        case Policy.Policy3:
            policy = new Policy3(_policy1Service, _policy2Service, _logHandler);
            break;
    }
    return policy;
}

Aucun commentaire:

Enregistrer un commentaire