1 2 Previous Next 17 Replies Latest reply: Aug 30, 2010 2:47 PM by 843853 Go to original post RSS
      • 15. Re: Need help with Factory Pattern.
        791266
        Blocked a post, and locked an account due to spamming.

        Kaj
        • 16. Re: Need help with Factory Pattern.
          843853
          One possibilty is to have empty constructors and initialise the properties after the factory has returned the objects.
          Car car = TransportFactory.createCar();
          
          car.setColor(Color.RED);
          
          etc etc
          The problem with this is if you add another property, to say Car it will not get set throughout your code base unless you modify all occurences of the above code.

          This suggests it is better to have
          Car car = TransportFactory.createCar( color, numberofwheels ) 
          etc

          because when you add another property, you will need to change the signature of you creation method and a subsequent rebuild will highlight all the places that you need to think about initialising the new property.

          Of course the alternative is to go with suggestion 1 and initialise the propty to a default value in the contructor of the Car class.

          Rich
          • 17. Re: Need help with Factory Pattern.
            843853
            You could use a constructor with varargs for your child clasess. This way you can have classes with the same constructor signature, but of course, you'll still have to write the constructors.

            Little dirty but your domain seem a little messy.
            1 2 Previous Next