I have found a lot of literature on these two patterns (Wikipedia, MSDN, etc). Some very good book like “Head First Design Patterns” which is very well explained but can be a bit confusing to understand some basic concepts.

I have seen many schemes with many arrows that are very good didactically but not just integrated into a concrete example.

This was the reason for write this post.

The goal is to explain in a graphic and simple way how these two patterns work at a conceptual level. The two schemes that I’ve painted are a mix between the concept and the code of the examples. The sources are Wikipedia and MSDN. The examples are invented by myself. These images (taken from MSDN) are fantastic as they illustrate the relationship between these patterns and how Abstract Factory is implemented using Factory Method

fm01

In real life, the patterns are not segregated by absolute lines. Often, a class can use one or more patterns, making the boundary between both diffuse. Additionally, you can start using a simple pattern and evolve towards a more complex one depending on the needs of our application. 

Abstract Factory is usually implemented using Factory Method and therefore provides at least all the flexibility of it. The main difference between them is that the first deals with families of products, while the other refers to a single product.

Abstract Factory is at a higher level of abstraction than Factory Method. Designs using Abstract Factory are more flexible than those using Factory Method, but they are also more complex. Another notable difference is the scope of both patterns: Factory Method is a class pattern, while Abstract Factory is an object pattern. Class patterns refer to the relations between classes (static, at compile time) and their subclasses, while those of objects deal with relationships between instances (dynamic, at runtime). Object patterns are often preferable to class patterns, since they are based on the fundamental principle of using composition instead of inheritance and delegation.

Factory Method

Define an interface to create objects but let subclasses decide which classes to instantiate.

The main classes in this pattern are the creator and the product. The creator needs to create product instances, but the specific type of product must not be forced into the subclasses of the creator, because the possible subclasses of the creator must be able to specify subclasses of the product to use.

fm1

The solution for this is to make an abstract method (the factory method) that is defined in the creator. This abstract method is defined to return a product. Creator subclasses may overwrite this method to return appropriate subclasses of the product.

Factory Method diagram (car example)

FMe

Factory Method. Car example in Java.

The image below show the implementation of the previous diagram.

FMe2

here we can see the program code with calls to the factories

FMe3

as we can see the the lines of code are the same but different factories

and the result of the execution is the following:

FMe4

Abstract Factory

This pattern provides an interface to create families of related or dependent objects, without specifying their specific classes.

fm5

Use when

  • A system must be independent of how its products are created, composed and represented.
  • A system must be configured with a family of products among several.
  • A family of related product objects is designed to be used together and it is necessary to enforce that restriction.
  • You want to provide a library of product classes and you only want to reveal their interfaces, not their implementations.

Consequences

  • Benefits:
    • Isolates the concrete classes.
    • Facilitates the exchange of product families.
    • Promotes consistency between products
  • Disadvantage:
    • It is difficult to accommodate new types of products.

Abstract Factory diagram.

This is the diagram for two factories (car and motorcycle factories) and two products (tyres and brakes).

FMeScheme

Abstract Factory implementation (vehicles example)

This image shows the implementation of the concrete factories.

FMe5

And this other image shows the implementation of the concrete products.

FMe6

Main Program

FMe8

As we can see, the first line of code creates a concrete factory (car or motorcycle) and the rest of lines are the same.

And the final image after execution

FMe9

In summary Abstract Factory is more complex at a conceptual and implementation level than Factory Method. The important thing is to know if we are going to work with families of products, or on the contrary we have a single product.

I hope you have found my diagrams and post in general useful.

Juan Luis.

Below all the code:

public interface IVehicle {

//Abstract Factory

Tyre CreateTyre();

Brake CreateBrake();

}

//Concrete Factory

public class CarFactory implements IVehicle

{

public Tyre CreateTyre() {

return new CarTyre();

}

public Brake CreateBrake() {

return new CarBrake();

}

}

//Concrete Factory

public class MotorcycleFactory implements IVehicle

{

public Tyre CreateTyre() {

return new MotorcycleTyre();

}

public Brake CreateBrake() {

return new MotorcycleBrake();

}

}

public abstract class Brake {

//Abstract Product

public abstract void creating();

}

//Concrete Product (Brake for Car)

public class CarBrake extends Brake {

@Override

public void creating(){

System.out.println(“Creating Car Brake…”);

}

}

//Concrete Product (Tyre for Car)

public class CarTyre extends Tyre {

@Override

public void creating(){

System.out.println(“Creating Car Tyre…”);

}

}

// Concrete Product (Brake for Motorcycle)

public class MotorcycleBrake extends Brake {

@Override

public void creating(){

System.out.println(“Creating Motorcycle Brake…”);

}

}

//Concrete Product (Tyre for Motorcycle)

public class MotorcycleTyre extends Tyre

{

@Override

public void creating() {

System.out.println(“Creating Motorcycle Tyre…”);

}

}

public abstract class Tyre {

//Abstract Product

public abstract void creating();

}

public class Program {

public static void main(String[] args) {

// Objects for Car

IVehicle factory = new CarFactory();

Brake myBrake = factory.CreateBrake();

Tyre myTyre = factory.CreateTyre();

myTyre.creating();

myBrake.creating();

// Objects for Motorcycle

factory = new MotorcycleFactory();

myBrake = factory.CreateBrake();

myTyre = factory.CreateTyre();

myTyre.creating();

myBrake.creating();

}

}