vendredi 22 avril 2016

Can a factory method pattern use different overloads

I have a use case where I need to build a factory class that returns different concrete types based on an enum being passed. However, each of those types require different constructor parameters. So, in that case does it make sense to have multiple methods with different signatures, or is that not a factory pattern at all?

I'm thinking something like

class CarFactory
{
  public ModelS TeslaMaker (List<Battery> batteries){/*return ModelS*/}
  public Mustang FordMaker (Engine engine) {/*return a Mustang*/}
}

instead of say

    class CarFactory
    {
      public Car GetCar(CarType carType) //where CarType is an enum (Ford=0,Tesla=1)
      { 
        switch(carType) 
        {  case CarType.Ford: return new Mustang(); }//just an example
      }
    }

Aucun commentaire:

Enregistrer un commentaire