- Class constructors exist so that clients can create an instance of a class.
- There are situations where the client does not, or should not, know which one of several candidate classes to instantiate.
- The Factory Method defines an interface for creating an object (IFactory), but lets subclasses decide which class to instantiate.
- It allows the client to use an interface for creating an object while still retaining control over which class to instantiate.
- The key objective of the Factory Method is extensibility.
- Factory Methods are frequently used in “manager” type components, such as, document managers, account managers, permission managers.
- Not all methods return “new object” are Factory methods. The Factory rules are:
- The method creates a new object.
- The method returns an abstract class or interface.
- The abstract class or interface is implemented by several classes.
- In .NET, the factory method is typically implemented as a static method which creates an instance of a particular type determined at compile time.
System.Convert
class is one example of factory method which exposes many static methods that, given an instance of a type, returns another new type.
- These methods don’t return base classes or interface types of which the true type is only known at runtime.
- This is exactly where Abstract Factory and Factory Method differ:
- "Factory Methods" are abstract and return class types.
- "Abstract Factory" methods are virtual or abstract and return abstract classes or interfaces.
Participants
Product
(IAuto)
- Defines the interface of objects the factory method creates.
ConcreteProduct
(Bmw)
- Implements the
Product
interface.
Creator
(IAutoFactory)
- It declares the factory method, which returns an object of type
Product
.
- It may also define a default implementation of the factory method that returns a default
ConcreteProduct
.
- It may call the factory method to create a
Product
object.
ConcreteCreator
(BmwFactory)
- It overrides the factory method to return an instance of a
ConcreteProduct
.
IAutoFactory autoFactory = new BmwFactory();
IAuto auto = autoFactory.GetInstance();
interface IAuto
{
string GetName();
}
class Bmw : IAuto
{
public string GetName() => "Bmw";
}
interface IAutoFactory
{
IAuto GetInstance();
}
class BmwFactory : IAutoFactory
{
public IAuto GetInstance() => new Bmw();
}
Rules of Thumb
- Factory Methods are usually called within Template Methods.
- Designs start using Factory Method (less complicated, more customizable) and evolve toward Abstract Factory, Prototype, or Builder (more flexible and complex).
- The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.
- Some Factory Method advocates recommend that absolutely all constructors should be private or protected.
- The
new
operator considered harmful.
- There is a difference between requesting an object and creating one.
- The
new
operator always creates an object, and fails to encapsulate object creation.
- A Factory Method enforces that encapsulation, and allows an object to be requested without inextricable coupling to the act of creation.