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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Execution Type Models in Node.js
  • Reducing Network Latency and Improving Read Performance With CockroachDB and PolyScale.ai
  • JavaFX Goes Mobile
  • Building and Deploying Microservices With Spring Boot and Docker

Factory Method vs Abstract Factory (again?)

Vishal Jain user avatar by
Vishal Jain
·
Mar. 17, 13 · Interview
Like (9)
Save
Tweet
Share
110.26K Views

Join the DZone community and get the full member experience.

Join For Free

hello friends,

recently i was asked by one of my friends what is the difference between factory method and abstract factory design patterns, but i didn't seem to convince him easily. of course i’d already read head first, gof, pattern hatching, refactoring to patterns, but not deeply all of them and it was not recent. so i decided to explore more and tried to simply the differences, and here ‘s my attempt.

i assume that you are already familiar with both the patterns, so i’ll focus here on where the most people have confusion with the differences. let’s revisit the definitions and their structure first:

factory method : define an interface for creating an object, but let subclasses decide which class to instantiate. factory method lets a class defer instantiation to subclasses.


abstract factory : provide an interface for creating families of related or dependent objects without specifying their concrete classes.

af_dp

differences

one product vs set of many

picture1

this is perhaps the easiest one but important certainly. factory method is used to create one product only but abstract factory is about creating families of related or dependent products .

inheritance vs composition

picture2

this is perhaps the most confusing one (as both seems to be using inheritance). factory method depends on inheritance to decide which product to be created. in creator class (in structure diagram), it has other methods also (implemented, to manipulate the product) which use the only abstract method factorymethod() to create the product and it can only be implemented/changed by subclasses .

here the creator class is also acting like client which depends only on a single method to create a product. we have to create a subclass of whole creator/client class to create a new different product. there’s no separate and dedicated class for creation of a product, had it been the case we could have used it with composition where we can pass an object of factory to client and client can use it without getting into inheritance hierarchy.

on the other side, in abstract factory, there’s a separate class dedicated to create a family of related/dependent products and its (any concrete subclass factory) object can be passed to the client which uses it ( composition ). here the client gets a different object (concrete factory) to create the products, instead of creating itself (e.g. using factorymethod() and forcing inheritance), and thus uses composition .

if we think of just a product creation facility and the client that uses it, it is clear that in factory method, we are restricted to use inheritance (class based) and in abstract factory we have the flexibility of composition (object based) to create specific products.

//factory method
 class client {
   public void anoperation() {
     product p = factorymethod();
     p.dosomething();
   }
   protected product factorymethod() {//or it can be abstract as well
     return new defaultproduct();
   }
 }
 class newclient extends client {
   protected product factorymethod() {//overriding
     return new specificproduct();
   }
 }
 //abstract factory
 class client {
   private factory factory;
   public client (factory factory) {
     this.factory = factory;
   } 
   public void anoperation() {
     producta p = factory.createproducta();
     p.dosomething();//other products and operations as well
   }
 }
 interface factory {
   producta createproducta();
   productb createproductb();
 }
 //concrete factories also, implementing factory interface
 

method vs full class (object)

picture3

factory method is just a method while abstract factory is an object . the purpose of a class having factory method is not just create objects, it does other work also, only a method is responsible for creating object. in abstract factory, the whole purpose of the class is to create family of objects .

level of abstraction

level-of-abstraction-640x390

abstract factory is one level higher in abstraction than factory method. factory method abstracts the way objects are created , while abstract factory also abstracts the way factories are created which in turn abstracts the way objects are created.

one inside another

picture4

as abstract factory is at higher level in abstraction, it often uses factory method to create the products in factories.

i also believe that we should not be obsessed with design patterns, these are all built on good basic design principles, and often mixed while using in real world.

i hope it helps somebody else also. let me also know if it can be improved.

happy patterns, bye.

Factory (object-oriented programming)

Opinions expressed by DZone contributors are their own.

Trending

  • Execution Type Models in Node.js
  • Reducing Network Latency and Improving Read Performance With CockroachDB and PolyScale.ai
  • JavaFX Goes Mobile
  • Building and Deploying Microservices With Spring Boot and Docker

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

Let's be friends: