Factory Method vs Abstract Factory (again?)
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.
differences
one product vs set of many
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
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)
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
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
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.
Opinions expressed by DZone contributors are their own.
Comments