Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract Factory, Factory Method, Builder

It may seem as if this is question is a dupe, but please bear with me - I promise I've read the related posts (and the GOF book).

After everything I've read, I still don't have it clear when to use an Abstract Factory, a Factory Method, or a Builder. I believe it will finally sink in after I see a simple example of a problem which is best approached by, say, a builder and it would be clearly silly to use, say, an abstract factory.

Can you provide a simple example where you would clearly use one pattern and not the others?

I understand it may boil down to a matter of opinion if the example is too simple, but I'm hopeful that if anybody can, that person is in SO.

Thanks.

like image 335
Escualo Avatar asked Sep 10 '10 18:09

Escualo


People also ask

What is difference between factory method and Abstract Factory?

The main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object. 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.

Is Abstract Factory a factory of factories?

Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

Can we use abstract class in factory design pattern?

Yes, as also a java 8 interface with an implemented default method.


2 Answers

A builder helps you construct a complex object. An example is the StringBuilder class (Java, C#), which builds the final string piece by piece. A better example is the UriComponentsBuilder in Spring, which helps you build a URI.

A factory method gives you a complete object in one shot (as opposed to the builder). A base class defines a single abstract method that returns an interface (or super class) reference, and defers the concrete creation of the object to subclasses.

An abstract factory is an interface (or abstract class) to create many different related objects. A good example (in .NET) is the DbProviderFactory class, that serves to create related objects (connections, commands, ...) to a given database provider (oracle, sql server, ...), depending on its concrete implementation.

like image 118
Jordão Avatar answered Sep 19 '22 18:09

Jordão


Builder

// Builder encapsulates construction of other object. Building of the object can be done in multiple steps (methods) public class ConfigurationBuilder {   // Each method adds some configuration part to internally created Configuration object   void AddDbConfiguration(...);   void AddSmtpConfiguration(...);   void AddWebServicesConfiguration(...);   void AddWebServerConfiguration(...);    // Returns built configuration   Configuration GetConfiguration(); } 

Factory method

// Factory method is declared in base class or interface. Subclass defines what type is created by factory method. public interface ICacheProvider {   ISession CreateCache(); // Don't have to return new instance each time - such decission is part of implementation in derived class. }  public class InMemoryCacheProvider : ICacheProvider { ... }  public class DbStoredCacheProvider : ICacheProvider { ... }  // Client code ICacheProvider provider = new InMemoryCacheProvider ICache cache = provider.CreateCache();  

Abstract Factory

// Abstract factory defines families of platform classes - you don't need to specify each platform class on the client. public interface IDbPlatform {   // It basically defines many factory methods for related classes   IDbConnection CreateConnection();   IDbCommand CreateCommand();   ... }  // Abstract factory implementation - single class defines whole platform public class OraclePlatfrom : IDbPlatform { ... }  public class MySqlPlatform : IDbPlatform { ... }  // Client code: IDbPlatform platform = new OraclePlatform(); IConnection connection = platform.CreateConnection(); // Automatically Oracle related ... 
like image 36
Ladislav Mrnka Avatar answered Sep 21 '22 18:09

Ladislav Mrnka