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.

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:

If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.

Go to your working directory and then follow below steps:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout factory-method

If you don’t have git installed, you can either directly download zip or copy paste from below.

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

2 comments for “factory method implementation

  1. Nyng
    April 4, 2017 at 2:27 pm

    Is that the Vehicle: vehicle inside the VehicleFactory is a necessary element of Factory Pattern?

Leave a Reply

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