You are on page 1of 5

Lab # 4

LAB # 4

Creational Patterns: Factory Method Pattern

OBJECTIVE:

Implementation of Creational Design Patterns: Factory Method Pattern.

THEORY:

Creational Design Pattern:

In software engineering, creational design patterns are design patterns that deal with object
creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form
of object creation could result in design problems or in added complexity to the design. Creational
design patterns solve this problem by somehow controlling this object creation.
Creational design patterns are composed of two dominant ideas. One is encapsulating knowledge
about which concrete classes the system uses. Another is hiding how instances of these concrete
classes are created and combined.
Creational design patterns are further categorized into Object-creational patterns and Class-
creational patterns, where Object-creational patterns deal with Object creation and Class-creational
patterns deal with Class-instantiation. In greater details, Object-creational patterns defer part of its
object creation to another object, while Class-creational patterns defer its object creation to
subclasses.
Five well-known design patterns that are parts of creational patterns are:

• Builder pattern, which separates the construction of a complex object from its representation
so that the same construction process can create different representations.
• Factory method pattern, which allows a class to defer instantiation to subclasses.
• Singleton pattern, which ensures that a class only has one instance, and provides a global point
of access to it.
• Abstract factory pattern, which provides an interface for creating related or dependent objects
without specifying the objects' concrete classes.
• Prototype pattern, which specifies the kind of object to create using a prototypical instance,
and creates new objects by cloning this prototype.

1. Factory Method Pattern:

According to Gang of Four,

SWE-208: Software Design and Architecture


Lab # 4

“Define an interface for creating an object, but let subclasses decide which class to instantiate.
Factory method lets a class defer instantiation to subclasses.”

The Factory Method design pattern is one of the twenty-three well-known "Gang of Four" design
patterns that describe how to solve recurring design problems to design flexible and reusable
object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

The Factory Method design pattern solves problems like:

• How can an object be created so that subclasses can redefine which class to instantiate?
• How can a class defer instantiation to subclasses?

The Factory Method design pattern describes how to solve such problems:

• Define a separate operation (factory method) for creating an object.


• Create an object by calling a factory method.

Let us consider an example. This example has been taken from the book “Java Design Patterns,
2nd edition, by Vaskaran Sarcar”.

SWE-208: Software Design and Architecture


Lab # 4

Java Code:

interface Animal
{
void speak();
void preferredAction();
}

class Dog implements Animal


{
public void speak()
{
System.out.println("Dog says: Bow-Wow.");
}
public void preferredAction()
{
System.out.println("Dogs prefer barking...\n");
}
}

class Tiger implements Animal


{
public void speak()
{
System.out.println("Tiger says: Roar.");
}
public void preferredAction()
{
System.out.println("Tigers prefer hunting...\n");
}
}

abstract class AnimalFactory


{
/*Remember that the GoF definition says "....Factory method lets a class defer
instantiation to subclasses."
In our case, the following method will create a Tiger or Dog but at this point it
does not know whether it will get a Dog or a Tiger. This decision will be taken by
the subclasses i.e. DogFactory or TigerFactory.
So, in this implementation, the following method is playing the role of a factory (of
creation)*/
public abstract Animal createAnimal();
}

class DogFactory extends AnimalFactory


{
public Animal createAnimal()
{
//Creating a Dog
return new Dog();
}
}

class TigerFactory extends AnimalFactory


SWE-208: Software Design and Architecture
Lab # 4

{
public Animal createAnimal()
{
//Creating a Tiger
return new Tiger();
}
}

class FactoryMethodPatternExample {
public static void main(String[] args) {
System.out.println("***Factory Pattern Demo***\n");
// Creating a Tiger Factory
AnimalFactory tigerFactory =new TigerFactory();
// Creating a tiger using the Factory Method
Animal aTiger = tigerFactory.createAnimal();
aTiger.speak();
aTiger.preferredAction();

// Creating a DogFactory
AnimalFactory dogFactory = new DogFactory();
// Creating a dog using the Factory Method
Animal aDog = dogFactory.createAnimal();
aDog.speak();
aDog.preferredAction();
}
}

Output

Exercise

Imagine that you’re creating a management application for a courier company. The initial version
of your app that you created only handled transportation by road i.e. by trucks. Now the company
is expanding its business and will be incorporating sea and air logistics into the app.
So, now you are thinking to apply factory method design pattern so that your code will not be
tightly coupled and you can add new classes for sea and air logistics easily.

SWE-208: Software Design and Architecture


Lab # 4

Suppose you have an interface named Transport, having method load(). The concrete classes
implementing the interface are Truck, Ship and Airplane. The abstract class TransportFactory will
have a factory method bookTransport(). This method will book a truck or a ship or an airplane
and the decision will be taken by the subclasses i.e. TruckFactory, ShipFactory etc.

Make a class diagram first and then implement this whole scenario.

SWE-208: Software Design and Architecture

You might also like