jeudi 30 mai 2019

Should my classes have the same constructor using a factory pattern?

Actually I am using the factory pattern with classes that need different constructors.

Am I using this type of design pattern correctly or do I need to use a different design pattern for my problem?

Let's say that I have the following code:

public interface IProcessJob
{
   bool TryProcess(out StatusType status);
}

public class InitializationJob : IProcessJob
{ 
   public InitializationJob() { //Some code here... }
   public bool TryProcess(out StatusType status)
   {
     //Some code here...
   }
}

public class RelocationJob : IProcessJob
{ 
   public RelocationJob (IRepository<Item, Guid> _itemRepository) 
   { 
     //Some code here... 
   }
   public bool TryProcess(out StatusType status)
   {
     //Some code here...
   }
}

public class CleanUpJob : IProcessJob
{ 
   public CleanUpJob (IRepository<Processed, Guid> _processedRepository) 
   { 
     //Some code here... 
   }
   public bool TryProcess(out StatusType status)
   {
     //Some code here...
   }
}

Here I have my factory class that creates the instances with different constructors.

public static class ProcessJobFactory 
{
   public static IProcessJob GetProcessJob(
       ProcessJobType processJobType
       IRepository<Item, Guid> _itemRepository,
       IRepository<Processed, Guid> _processedRepository)
   {

       switch(processJobType)
       {
          case ProcessJobType.Initialization:
            return new InitializationJob();
          case ProcessJobType.Relocation:
            return new RelocationJob (_itemRepository);
          case ProcessJobType.CleanUp:
            return new CleanUpJob (_processedRepository);
       }
   }

}

Aucun commentaire:

Enregistrer un commentaire