mardi 22 septembre 2015

Extendable factory design pattern

Following are my import factory classes. If I want to add new ImportTypes then just add new type in ImportTypes enums and add a case in factory class and it works fine.

My question is how can I make it more independent and easy to extend. Lets say a user wants to add a new ImportType then instead of changing code, he writes his own DLL and implements interface... any good suggestions/ideas?

Import types

enum ImportTypes
{
   DefaultImport,
   C2CImport
}

Import interface

public interface IImportService
{
   void Import(Argument arguments, ImportDefinition config);
}

Import factory

class ImportFactory
    {
    public static IImportService GetService(ImportTypes type)
    {
       switch (type)
       {
           case ImportTypes.DefaultImport:
               return new DefaultImportService();

           case ImportTypes.C2CImport:
               return new C2CImportService();
       }

       return null;
    }
}

Test

IImportService docImportService = ImportFactory.GetService(ImportTypes.DefaultImport);

Aucun commentaire:

Enregistrer un commentaire