Skip to main content

The Factory Pattern

Lesson 1 - The Factory Pattern


In this series we will be going through the various design patterns that are available. I will be using many different sources to gather information and all these sources will be listed in the links below. I will also be heavily referencing from the Head First Design Patterns Book which is definitely worth a read. At the bottom of each article there will also be links to some code examples for you to go through and play around with. So lets begin.

 The Factory Pattern is a Creational Design Pattern.
Creational Design Patterns are design patterns that deal with object creation mechanisms.

In this tutorial we will look at the following :

  • Simple Factory
  • Factory Method.
  • Abstract Factory.

Definition

Factory Method

“The Factory Method Pattern defines an interface for creating an object but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to sub classes.” - Head First Design Patterns (Page 134)

Abstract Factory

“The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.” - Head First Design Patterns (Page 156)

When to use ?

When the object creation process is complex or multiple objects created that share the same properties. When business logic is required to determine what object is created.

Important to Note

All factories encapsulate object creation.
The new keyword is not needed as instantiation is done in the factory implementation.
When you think new, think concrete. (Thats an implementation not an interface).
All factory patterns promote loose coupling by reducing the dependency of your application on concrete classes Concrete classes are more fragile and less flexible.
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification (Open-Closed Priciple)
Instead of having multiple new MyObject() call, the initialization is kept in one place, satisfying the Don’t Repeat Yourself(DRY) principle.

Simple Factory

While not actually a pattern. It is a simple way to decouple your clients from the concrete classes.

Factory Method

Provides a generic interface to create objects.
Provides an interface for creating one product.
A factory method handles object creation and encapsulates it in a subclass, This decouples the client code in the subclass from the object creation code in the superclass.
Subclasses decide what needs to be created.
The factory method adheres to the Dependency Inversion Principle. (Depend on Abstracts and not concrete classes)
Creates objects via inheritance. ( Extend a class and overwrite the factory method )

Abstract Method

Creates objects via object inheritance. (Defines an abstract type for creating a family of products. Subclasses of this type define how those products are produced. To use this factory you instantiate one and pas sin some code that is written against the abstract type.)
Interface needs to change when new products are added.(Interfaces of subclasses will therefore also need to be updated)
Uses Factory Methods to implement its concrete classes.
Useful when you want to create a family of products.

Benefits

  1. Provides Decoupling
  2. Avoids duplication
  3. Single point of maintenance.
  4. By allowing Clients to code to a interface and not an
    implementation, you allow the code to be more extensible and
    flexible.

Code Examples

The examples below are taken from the Head First Design Patterns Book. Work through them in the given order.

The Pizza Problem
Simple Factory
Factory Method
Abstract Factory

More Reading

Next Up - The Strategy Pattern

Back to Tutorials

Comments

Popular posts from this blog

The Decorator Pattern

--- --- Lesson 3 - The Decorator Pattern The Decorator Pattern (also known as the wrapper) is a Structural Design Pattern. Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient. It allows us to dynamically add properties to objects at runtime. The goal is to combine a single concrete class with one or more Decorators. Definition Decorator is a structural design pattern that lets you attach new behaviours to objects by placing these objects inside special wrapper objects that contain the behaviours. The Decorator Pattern allows behaviour to be added to an individual object , either statically or dynamically without affecting the behaviour of other objects in the same class. - Wikipedia The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. - Head First Design Patterns Provides ...

The Strategy Pattern

Lesson 2 - The Strategy Pattern The Strategy Pattern is a Behavioural Design Pattern. Behavioural Design Patterns are design patterns that identify common communication patterns among objects and realise these patterns and in doing so increases flexibility, carrying out this communication. Definition The Strategy Pattern defines a family of algorithms, encapsulates each one and makes them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. When to use ? When you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime. When you have a lot of similar classes that only differ in the way they execute some behaviour. To isolate the business logic of a class from the implementation details of algorithms that may not be as important in the context of that logic. When your class has a massive conditional operator that switches between different variants of the same algo...