Can this example serve as an example of the implementation of the FactoryMethod pattern, or is it erroneous in relation to this particular pattern?

 public class RentCar { public static Car getRentCar(Object object) { Car car = null; if (object instanceof BmwCar) { car = new BmwCar(); } else if (object instanceof PejouCar) { car = new PejouCar(); } return car; } } 

Class:

 public abstract class Car { public abstract int getMaxSpeed(); public abstract String getMark(); } 

Impl:

 public class BmwCar extends Car { @Override public int getMaxSpeed() { return 120; } @Override public String getMark() { return "BMW"; } } public class PejouCar extends Car { @Override public int getMaxSpeed() { return 80; } @Override public String getMark() { return "Pejou"; } } 
  • What you gave is not a Factory Method, but an Abstract Factory. - andreycha
  • @andreycha as far as I know, for an abstract factory, there must also be an abstraction of a concrete factory and its implementation, since the factory creates a whole family of objects, but in this case there is only one method that creates one heir. I do not think this is a factory. but I can not understand, or this is the correct factory method. - raviga
  • Yes, lured. All right, say it. And about the correctness of the below you correctly answered. - andreycha

1 answer 1

A strange method is obtained. In the current implementation, in order to get, for example, an instance of the BmwCar class, you must pass an instance of the BmwCar class to the getRentCar method! It makes no sense. So it would be better:

 public static Car getRentCar(String carName) { Car car = null; if ("BMW".equals(carName)) { car = new BmwCar(); } else if ("Peugeot".equals(carName)) { car = new PeugeotCar(); } return car; } 

Update after discussion in the comments: From a purely formal point of view, the implementation is correct, because It does not explicitly indicate which instance of the class should be returned from the method.

  • as I understand, changes in the transmission parameter does not change the logic of object initialization. in this case, I do not fully understand or this approach is generally correct. - raviga
  • en.wikipedia.org/wiki/Factory_method_pattern : " created. " In your opinion, you have violated an important condition - you purposefully inform the method which instances of which class to create, passing the instances of that particular class that you want to receive back. - fromSPb
  • You mine do not correctly understand the meaning of "without specifying the exact class". This means that it is not possible to use not instanceof, but that class initialization should proceed without explicit indication of the class itself. Example: BmwCar car = new BmwCar (); In my case, this is not happening. - raviga
  • Mm .. I agree. But not because "it is impossible to use instanceof" - I did not say that. And simply because, for example, a BmwCar may have a successor to OtherBmwCar, for which the object instanceof BmwCar returns true and you will get a BmwCar output ... In general, although a curve, it is a purely formal implementation. - fromSPb