DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Intro to Design Patterns: Factory Method Pattern

Intro to Design Patterns: Factory Method Pattern

Andre Mare user avatar by
Andre Mare
·
Apr. 21, 08 · News
Like (0)
Save
Tweet
Share
26.01K Views

Join the DZone community and get the full member experience.

Join For Free

Last week, in part 1, Andre Mare introduced us to the Builder pattern. Today he continues his series on the "Gang of Four" design patterns. -- Geertjan Wielenga, JavaLobby Zone Leader

 

Intent

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. - Gof

Type

Class Creational

Solution

The Factory Method Pattern is a well known Creational Pattern that creates an abstraction through which one of several classes is returned. The pattern enables us to encapsulate the instantiation of concrete types. The pattern is classified as a Class Creational Pattern which means the pattern makes use of inheritance to decide what object to instantiate.

The Factory Method Pattern consists of a Product, ConcreteProduct, Creator, ConcreteCreator and Client.

  • The Product defines the type or interface for the concrete objects that are created by the Factory Method.
  • The ConcreteProduct provides the implementation of the different Product types created by the Factory Method. The ConcreteProduct is instantiated by the factory method of the different ConcreteCreator objects.
  • The Creator class specify the creator method or factory method that returns objects of type Product.
  • The ConcreteCreator provides the concrete factory methods. These methods override the factory method in the Creator class to return the ConcreteProduct instance.
  • The Client object is decoupled from the ConcreteProduct objects, but uses the factory method to return the appropriate ConcreteProduct through the Product type.

The pattern makes use of polymorphism to decouple the client from the class created by the Factory Method. The Factory Method returns an instance of a class (ConcreteProduct) that is defined through an interface or abstract parent (Product) class. The method where the class is created returns the object through its interface or abstract parent class, so that the client is decoupled from the actual ConcreteProduct class. All the returned classes through the factory methods have the same type (interface) or abstract parent class.

Structure

Design Pattern, Factory Method Pattern, GOF

Java Sample Code

Download : Bank Account System

The following example will illustrates the use of the Factory Method pattern. The Bank Account System example illustrates the creation of different bank accounts for different banks. The first diagram illustrates the different bank account types that are available.

Product & ConcreteProducts

The Bank Account System contains an abstract type for all the bank accounts available called the BacnkAccountProduct. The other classes are ConcreteProduct classes that is created by the Factory Method, depending on the type and the ConcreteCreator class.

Design Pattern, Factory Method Pattern, GOF

Creator & ConcreteCreator

The BankAccountCreator or Creator class defines the factory method as abstract so that the implementation is delegated to the subclasses. The factory method is defined as follows:

protected abstract BankAccountProduct createBankAccount(String accountType);

This factory method is then implemented in all the different subclasses of the BankAccountCreator class. Each different ConcreteCreator knows how to instantiate the different ConcreteProduct classes.

Design Pattern, Factory Method Pattern, GOF

Factory Method Class Diagram

The class diagram below show the dependencies between the different classes as used in the Bank Account System example.

Design Pattern, Factory Method Pattern, GOF

 

The BankSystemClient class has a gets a reference to an object of type BankAccountProduct. The client does not know what the implementing class is, but rather works through the abstract BankAccountProduct class.

Sequence Diagram

Sequence Diagram by "Yanic Inghelbrecht" with Trace Modeler

 

References

  • Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995
Factory (object-oriented programming) Factory method pattern Design Object (computer science)

Published at DZone with permission of Andre Mare. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Avoid “Schema Drift”
  • Problems of Cloud Cost Management: A Socio-Technical Analysis
  • Learning by Doing: An HTTP API With Rust
  • Securing VMs, Hosts, Kubernetes, and Cloud Services

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: