factory method implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Going forward with our design patterns tutorials series, we will see another creational design pattern which is a flavour of factory design pattern. It comes in two flavours, factory method and abstract factory. We will see factory method implementation in this article.

Factory Method Implementation:

factory method design pattern delegates the task of creation of objects to another type. i.e. the calling class does not have to know about the way objects are created. Gang of Four book defines factory method as

 “Defines an interface for creating an object but lets subclasses decide which class to instantiate”

Head First Design Patterns also adds that

“Factory method lets a class defer instantiation to subclass”

We will create factory method implementation referring to above definition.

Generic Class Diagram for factory method implementation:

Generic Factory Design Pattern

Generic Factory Design Pattern

  • Product is an abstract class which represents all object those will be created
  • Factory is an abstract class which represents all object factories. Factory class will have an abstract factory method which needs to be implemented by all extending subclasses. This factory method is invoked from another public helper method in same class.
  • Client is the actual class which invokes factory and delegates object creation to factory method implementation.

 Factory Method Implementation:

Consider our scenario. We have a Dealer which can ask for different types of vehicles to a vehicle factory. A Dealer asks the factory to assemble vehicles, for assembling a vehicle, factory needs to create specific vehicle type objects, which is done by subclasses of VehicleFactory. 

  • Vehicle is abstract class which represents the type of Object to be returned to factory method.
  • Bike and Car classes extend Vehicle.
  • VehicleFactory is abstract class which represents types of factories. They will have factory method which needs to be implemented by subclasses.
  • VehicleFactory has createVehicle() as factory method which is invoked from assembleVehicle()
  • BikeFactory and CarFactory extends VehicleFactory and for factory method implementation, they override createVehicle() method to return specific objects.
Vehicle and VehicleFactory Hierarchy

Vehicle and VehicleFactory Hierarchy

 

Factory Method Implementation Class Diagram:

Now let us create factory method implementation for our example.

Complete Example

Complete Example

  • VehicleFactory declares abstract createVehicle() method which is invoked from public assembleVehicle()
  • BikeFactory and CarFactory extends VehicleFactory and implement createVehicle()
  • Dealer instantiates BikeFactory and CarFactory.
  • Dealer invokes assembleVehicle() which in turn invokes createVehicle(). At runtime, actual factory method implementation will be invoked because of polymorphism.

Factory Method Implementation in code:

Vehicle.java

Bike.java

Car.java

VehicleFactory.java

BikeFactory.java

CarFactory.java

Dealer.java

If you run Dealer code, you will see console output.

From above code, we come to know that our factory method pattern implementation is successful. In next article we will understand how to implement abstract factory pattern.

Share Button

1 comment for “factory method implementation

Leave a Reply

Your email address will not be published. Required fields are marked *