mardi 26 avril 2016

How to add different set of parameters to factory method design pattern to achieve proper design

I have a scenario where two different classes have many properties and one different property. How do I use factory design pattern or should I not use it? Internal private methods of these sub classes make use of these properties.

public class RunsValidator  
{  
     //Few common properties  
     public int DataSegmentID { get; set; }  
     public int AttributeOffset { get; set; }  
}

public class ProductAttributeRunValidator : RunsValidator  
{

    public ProductAttributeRunValidator(string productNames)
    {
       this.ProdNames = productNames;
    }
}

public class CategoryAttributeRunValidator : RunsValidator  
{  

     public CategoryAttributeRunValidator(int orgIDs) : base()  
          {  
            this.totalOrgIDs = orgIDs;  
          }  

}  

//Factory implementation

public class RunAttributeFactory  
{     

    public static RunsValidator GetRunValidator(string type, string productNames, int orgIds)  
     {  
      RunsValidator runValidator = null;  
      if(type == "Product")  
      {   
           runValidator = new ProductAttributeRunValidator(productNames);  
      }  
      else if (type == "Category")  
      {  
           runValidator = new CategoryAttributeRunValidator(orgIds);  
      }  
      else  
      {  
          runValidator = null;  
      }  
      return runValidator;  
    }  
}

In the client code, I have access to variables that should be sent as parameters (productNames, orgIds and type) to the Factory method. I would like to access the returned runValidator in many places in the client code as it needs to be set only once based on the type.

Which is the best way to achieve this?

Aucun commentaire:

Enregistrer un commentaire