0% found this document useful (0 votes)
11 views32 pages

Factory, Abstract Factory Design Pattern

The Factory Method Pattern defines an interface for creating objects, allowing subclasses to decide which class to instantiate, promoting loose coupling and extensibility. It is useful when a class cannot predict the type of objects it needs to create or when subclasses should specify the objects they create. The document also discusses the advantages and disadvantages of the Factory Method, as well as the Abstract Factory Pattern, which allows for creating families of related objects without specifying their concrete subclasses.

Uploaded by

Jay Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views32 pages

Factory, Abstract Factory Design Pattern

The Factory Method Pattern defines an interface for creating objects, allowing subclasses to decide which class to instantiate, promoting loose coupling and extensibility. It is useful when a class cannot predict the type of objects it needs to create or when subclasses should specify the objects they create. The document also discusses the advantages and disadvantages of the Factory Method, as well as the Abstract Factory Pattern, which allows for creating families of related objects without specifying their concrete subclasses.

Uploaded by

Jay Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Factory method

design pattern
Factory Method Pattern
• A Factory Pattern or Factory Method Pattern says that just define an
interface or abstract class for creating an object but let the subclasses
decide which class to instantiate. In other words, subclasses are
responsible to create the instance of the class.
• The Factory Method Pattern is also known as Virtual Constructor.
Advantage of Factory Design
Pattern
• Factory Method Pattern allows the sub-classes to choose the type of
objects to create.
• It promotes the loose-coupling by eliminating the need to bind
application-specific classes into the code. That means the code interacts
solely with the resultant interface or abstract class, so that it will work
with any classes that implement that interface or that extends that
abstract class.
When to use Factory Method
Design Pattern?
• 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.
UML for Factory Method Pattern
• We are going to create a Plan abstract class and concrete classes that
extends the Plan abstract class. A factory class GetPlanFactory is defined
as a next step.
• GenerateBill class will use GetPlanFactory to get a Plan object. It will pass
information (Domesticplan/commercialplan /institutionalplan) to
GetPlanFactory to get the type of object it needs.
• Why do we need to use the Factory Method Design Pattern with an
Example
• Factory Method Design use abstract classes to define and maintain
relationship between objects. A framework is often responsible for
creating these objects as well.
Key Component of Factory
Method Design Pattern
Product
• It’s an abstract class or interface that defines the common operations for
the objects that the factory will create.
• Concrete Products are the actual classes that implement the Product
interface, each representing a specific type of object to be created.
• Creator:
• It’s an abstract class or interface that declares the factory method.
• This method is responsible for creating Product objects, but it delegates the
actual creation to subclasses.
• Concrete Creators:
• These are subclasses of the Creator that implement the factory method.
• They decide which specific Concrete Product to create, often based on input
parameters or configuration.
• Factory Method
• It’s a method defined in the Creator class that is responsible for creating Product
objects.
• It’s typically declared as abstract in the Creator and implemented in the Concrete
Creators.
Use Cases of the Factory
Method Design Pattern
• Creational Frameworks:
• JDBC (Java Database Connectivity) uses factories extensively for creating
connections, statements, and result sets. Dependency injection frameworks
like Spring and Guice rely heavily on factories to create and manage beans.
• GUI Toolkits:
• Swing and JavaFX use factories to create UI components like buttons, text
fields, and labels, allowing for customization and flexibility in UI design.
• Logging Frameworks:
• Logging frameworks like Log4j and Logback use factories to create loggers
with different configurations, enabling control over logging levels and
output destinations.
• Serialization and Deserialization:
• Object serialization frameworks often use factories to create objects from serialized
data, supporting different serialization formats and versioning.

• Plugin Systems:
• Plugin-based systems often use factories to load and create plugin instances
dynamically, allowing for extensibility and customization.

• Game Development:
• Game engines often use factories to create different types of game objects, characters, and
levels, promoting code organization and flexibility.

• Web Development:
• Web frameworks sometimes use factories to create view components, controllers, and
services, enabling modularity and testability in web applications.
Advantages of Factory Method
Design Pattern
• Decoupling: It separates object creation logic from the client code
that uses those objects.
• Extensibility: It’s easy to introduce new product types without
changing the client code.
• Testability: It simplifies unit testing by allowing you to mock or stub
out product creation during tests.
• Code Reusability: The factory method can be reused in different
parts of the application where object creation is needed.
• Encapsulation: It hides the concrete product classes from the client
code, making the code less dependent on specific implementations.
Disadvantages of Factory
Method Design Pattern
• Increased Complexity: It introduces additional classes and
interfaces, adding a layer of abstraction that can make the code
more complex to understand and maintain.
• Overhead: The use of polymorphism and dynamic binding can
slightly impact performance.
• Tight Coupling Within Product Hierarchies: Concrete Creators
are still tightly coupled to their corresponding Concrete Products.
• Dependency on Concrete Subclasses: The client code still
depends on the abstract Creator class, requiring knowledge of its
concrete subclasses to make correct factory method calls.
• Potential for Overuse: It’s important to use the Factory
Method pattern judiciously to avoid over-engineering the
application.
• Testing Challenges: Testing the factory logic itself can be
more complex.
Abstract Factory Pattern
Abstract Factory Pattern
• Abstract Factory Pattern says that just define an interface or
abstract class for creating families of related (or dependent)
objects but without specifying their concrete sub-classes.
• That means Abstract Factory lets a class returns a factory of
classes. So, this is the reason that Abstract Factory Pattern is
one level higher than the Factory Pattern.
Usage of Abstract Factory Pattern
• When the system needs to be independent of how its object are
created, composed, and represented.
• When the family of related objects has to be used together, then
this constraint needs to be enforced.
• When you want to provide a library of objects that does not
show implementations and only reveals interfaces.
• When the system needs to be configured with one of a multiple
family of objects.
UML for Abstract Factory Pattern
• We are going to create a Bank interface and a Loan abstract
class as well as their sub-classes.
• Then we will create AbstractFactory class as next step.
• Then after we will create concrete
classes, BankFactory, and LoanFactory that will
extends AbstractFactory class
• After that, AbstractFactoryPatternExample class uses
the FactoryCreator to get an object of AbstractFactory class.

You might also like