Factory patterns
Overview
There is more to making objects than just using the new operator. Instantiation is an activity that shouldn’t always be done in public and can often lead to coupling problems. Factory Patterns can help save you from embarassing dependencies.
Tags
Similarities and Differences between Simple Factory and Factory Method pattern
Similarities
Similarities between Simple Factory and Factory Method: by placing all the creation code in one object or method, you avoid duplication in your code and provide one place to perform maintenance. That also means clients depend only upon interfaces rather than the concrete classes required to instantiate objects. This allows you to program to an interface, not an implementation, and that makes your code more flexible and extensible in the future.
Differences
Main difference between Simple Factory and Factory Method: Simple Factory gives you a way to encapsulate object creation, but doesn’t give you the flexibility of the Factory Method because there is no way to vary the products you are creating. With Factory Method, you are creating a framework that lets the subclasses decide which implementation will be used. For example, the orderPizza() method in the Factory Method provides a general framework for creating pizzas that relies on a factory method to actually create the concrete classes that go into making a pizza. By subclassing the PizzaStore class (NYPizzaStore, ChicagoPizzaStore), you decide what concrete products go into making the pizza that orderPizza() returns.
Similarities and Differences between Factory Method and Abstract Factory
Similarities
- Both are really good at decoupling applications from specific implementations; they just do it in different ways.
- Both encapsulate object creation to keep applications loosely coupled and less dependent on implementations.
- Both design patterns deal with object creation.
- Both the patterns advocates the Object Oriented Programming (OOP) principle “Program to an interface, not an implementation” to abstract how the objects are created.
- Both design patterns help in creating client code that is loosely-coupled with object creation code.
Differences
-
Abstract factory adds another level of abstraction to factory method. While factory method abstracts the way objects are created, abstract factory abstracts how the factories are created. The factories in turn abstracts the way objects are created. You will often hear the abstract factory design pattern referred to as a “factory of factories“.
-
Both Factory Method and Abstract Factory create objects. Factory Method uses (sub) classes to create where as Abstract Method use objects.
-
Factory Method does it through inheritance. We say that that factory method uses inheritance because this pattern relies on a subclass for the required object instantiation. Abstract Method does it through object composition.
-
From implementation point of view, the key difference between the factory method and abstract factory patterns is that factory method is just a method to create objects of a single type, while abstract factory is an object to create families of objects.
-
To create objects using Factory Method, you need to extend a class and provide an implementation for a factory method. And what does that factory method do? It creates objects. With Factory Method, we use subclasses to do our creation for us. Clients only need to know the abstract type they are using, the subclasses worry about the concrete type. Factory Method keeps the clients decoupled from the concrete types.
-
Abstract Factory does it through composition; It provides 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 pass it into some code that is written against the abstract type. So, the clients are decoupled from the actual concrete products they use.
-
The added advantage of Abstract Factory is that it groups together a set of related products.
-
The caveat is, if Abstract Factory needs to extend that set of related products, for example, to add another one, it requires changing the interface. This is a disadvantage. Changing the interface means, it has to go in and change the interface of every subclass. It could be a lot of work. But Abstract Factory needs a big interface because it is used to creating entire families of products. Factory Method is only creating one product, so Factory Method does really need a big interface - Factory Method just needs one method.
-
Use Abstract Factory whenever you have families of products you need to create and you want to make sure your clients create products that belong together.
-
Use Factory Method to decouple your client code from the concrete classes you need to instantiate, or if you don’t know ahead of time all the concrete classes you are going to need. To use Factory Method, just subclass it and implement it’s factory method.