Contents
• Creational designpatterns
• Factory Method
• When to use Factory Method Design Pattern in Java?
• Usage of Factory Design Pattern
3.
Creational design patterns
Insoftware 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 added complexity to the design.
4.
Factory Method
Is acreational design pattern that provides an interface for creating
objects in a superclass, but allows subclasses to alter the type of
objects that will be created.
5.
Continue…
• Factory patternis one of the most used design patterns in Java. This
type of design pattern comes under creational pattern as this pattern
provides one of the best ways to create an object.
• In Factory pattern, we create object without exposing the creation
logic to the client and refer to newly created object using a common
interface.
6.
Factory Method
• Thefactory method in the interface lets a class defer the instantiation
to one or more concrete subclasses.
• Since these design patterns talk about the instantiation of an object
they come under the category of creational design pattern.
7.
Factory Method
• Ifwe notice the name Factory method, that means there is a method
which is a factory, and in general, factories are involved with
creational stuff and here with this, an object is being created.
• It is one of the best ways to create an object where object creation
logic is hidden from the client. Now Let’s look at the implementation.
8.
When to useFactory Method Design Pattern in Java?
Factory method design pattern can be used in java in following cases:
• A class cannot predict the type of objects it needs to create.
• A class wants its subclasses to specify the objects it creates.
• Classes delegate responsibility to one of multiple helper subclasses,
and you aim to keep the information about which helper subclass is
the delegate within a specific scope or location.
9.
Usage of FactoryDesign Pattern
• When a class doesn't know what sub-classes will be required to create
• When a class wants that its sub-classes specify the objects to be
created.
• When the parent classes choose the creation of objects to its sub-
classes.