From Passion to Profession
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use

Abstract Factory Design Pattern Example

6/25/2013

4 Comments

 

overview

What is Abstract Factory pattern and what is it for? According to Design Patterns by GoF, the intent of Abstract Factory is to,
Provide an interface for creating families of related or dependent objects without specifying their concrete classes. 
Picture
source: GoF
Abstract Factory conforms to the principle of "program to an interface, not an implementation." In Abstract Factory pattern, the class that is responsible for creating concrete objects (Products) is said to be a Factory class. In order to create different types of concrete Products, different type of concrete Factory classes will be used. Each Factory is just a collection of factory methods (e.g. CreateProductA(), CreateProductB()). Each factory method creates a concrete Product of the same family. 

The client code's factory method creates the final product based on a given factory. This extra layer of abstraction is provided to the client code by introducing the concept of factory. This is the most common way to implement the Abstract Factory pattern.

example

AbstractFactory / AbstractProduct / Client

    
[1] A factory method that creates an empty maze to begin with, not a final end product.
[2] Because each Factory knows how to make the product of their own family. The client code (MazeGame) simply has a factory method which depends on an abstract factory to create the final end Product - a final version of Maze. As demonstrated above, the Factory class adds an layer of abstraction to the client without exposing the implementation details of the products.
Another good example of an Abstract Factory in use could be UI toolkits across Windows, Mac and Linux, UI composites such as windows, buttons as described on DZone.

scenarios

Use Abstract Factory patter when you want to:
  • create multiple families of products,
  • provide a library of products without exposing the implementation details of the products.

comparison

Abstract Factory vs Builder: 

They are quite similar with the following differences: 
  • Abstract Factory emphasizes a family of product objects (either simple or complex). Builder focuses on constructing a complex product object step by step. 
  • Abstract Factory focuses on what is made. Builder focuses on how product is made. 
  • In Abstract Factory, every method call creates and returns different objects. In Builder pattern, only the last method call (build() or getResult()) returns the object, while other calls partially build/config the end product. 


Abstract Factory vs. Factory Method
  • Differ in their intent. In Abstract Factory pattern, a client class delegates the responsibility of object instantiation to another object (an Factory object) via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.
  • An Abstract Factory class builds on top of Factory Method pattern as seen in its derived Factory classes. An Abstract Factory is used to be passed to (or referenced by) other client code to create the final product using a specific Concrete Factory.
4 Comments
DP
10/16/2016 05:02:52 pm

Can we have an example of implementing abstract factory + factory method + builder pattern all together.

Reply
Hsufeng Ko
10/16/2016 11:27:37 pm

An 'Abstract Factory' and a 'Factory Method' patterns differ in their intention. Therefore, it won't make much sense to have an example with mixed intention, will it?

Reply
Erich Gamma
4/3/2017 11:25:58 pm

From GoF:
"AbstractFactory only declares an interface for creating products. It's up to ConcreteProduct subclasses to actually create them. The most common way to do this is to define a factory method"

If you want to write better, then you need to read more

Hsufeng Ko
5/25/2020 03:05:46 pm

I'd agree some said the factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it. Therefore, I had modified my note to reflect that an Abstract Factory may build upon factory methods.




Leave a Reply.

    Categories

    All
    Algorithm
    Concurrency
    CQ
    Data Structure
    Design Pattern
    Developer Tool
    Dynamic Programming
    Entrepreneur
    Functional Programming
    IDE
    Java
    JMX
    Marketing
    Marklogic
    Memory
    OSGI
    Performance
    Product
    Product Management
    Security
    Services
    Sling
    Social Media Programming
    Software Development

    Feed Widget

    Archives

    May 2020
    March 2020
    April 2018
    March 2018
    February 2018
    December 2017
    March 2017
    November 2016
    June 2016
    May 2016
    April 2016
    October 2015
    September 2015
    August 2015
    September 2014
    July 2014
    June 2014
    May 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013

    RSS Feed

in loving memory of my mother  and my 4th aunt
Photos used under Creative Commons from net_efekt, schani, visnup, Dan Zen, gadl, bobbigmac, Susana López-Urrutia, jwalsh, Philippe Put, michael pollak, oskay, Creative Tools, Violentz, Kyknoord, mobilyazilar