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
Please visit the following url for more details.
http://xeon2k.wordpress.com
Abstract Factory is particularly helpful for test driven development and reducing coupling.
For example, in C#:
public class Worker
{
public IConsumerFactory Factory { get; set; }
private IResource resource;
public DoWork()
{
IConsumer consumer = Factory.CreateConsumer();
consumer.Consume(resource);
}
}
public interface IConsumerFactory
{
IConsumer CreateConsumer();
}
public interface IConsumer
{
void Consume(IResource resource);
}
public class DefaultConsumerFactory : IConsumerFactory
{
public IConsumer CreateConsumer()
{
return new DefaultConsumer();
}
}
public class DefaultConsumer : IConsumer
{
public void Consume(IResource resource)
{
... Do Work ...
}
}
This way, you can use dependency injection to inject the default implementations for production code, and then you can easily mock the factory and the objects it creates.
The Abstract Factory pattern uses subclassing (of factories) to produce other objects (non-factories). Abstract Factory also envisions that the objects produced belong to parallel hierarchies (e.g. to handle platform independance, one hierarchy for each platform).
The Builder pattern uses subclassing to produce "output" - which is not necessarily objects at all. The GOF example has the Builder producing text output (markup or otherwise).
The Factory Method pattern, unlike the other two, divides the "creator" into an abstract and concrete implementation (thus the emphasis on it belonging to a framework implementation). Like Abstract Factory, it deals with making actual objects.
All three are highly similar, because they all use subclassing. It is the subclassing that is the outstanding quality of them all, which hides the subtle differences (outlined above) and thus many people have difficulty seeing the differences.
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
...
Abstract Factory, Factory Method, Builder : All these patterns are creational patterns,which are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
Factory method:
It may use inheritance or sub classing to achieve the purpose
Key note: You will create an interface & specific implementation of these interfaces. In Factory method, depending on condition, you will get concrete implementation of common interface.
Abstract Factory:
Builder:
Guidelines for Builder design pattern in Java
Related posts:
Design Patterns: Factory vs Factory method vs Abstract Factory
Keeping builder in separate class (fluent interface)
Useful links:
sourcemaking design-patterns
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.