Alex Rivera | Logout

Is a switch statement applicable in a factory method? c#

Asked 2009-07-28T23:37:45.290
17

I want to return an Interface and inside a switch statement I would like to set it. Is this a bad design?

private IResultEntity GetEntity(char? someType)
    {
        IResultEntity entity = null;

        switch (someType)
        {
            case 'L': //life
                entity = new LifeEntity();
                break;
            case 'P': //property
                entity = new PropertyEntity();
                break;
            case 'D': //disability
                entity = new DisabilityEntity();
                break;
            case 'C': //credit card
                entity = new CreditCardEntity();
                break;
        }

        return entity;
    }
Edit
Report

1 Answer

2

I wouldn't say its a bad design, although it is potentially fairly rigid. The only way to extend this would be via recompilation.

answered 2009-07-28T23:45:00.580

Your Answer