It is a creational pattern that uses a special type of object to create other objects. Factory methods encapsulate the creation of objects. This can be useful if the creation process is very complex, for example if it depends on settings in configuration files or on user input.
Concrete object’s (product’ s) constructor is often made private to force clients to use the factory methods.
Factory Method Pattern
It deals with the problem of creating objects without specifying the exact class of object that will be created. The pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.
- A family of objects needs to be separated by using shared interface.
- The code needs to deal with interface, not implemented classes.
- Hide concrete classes from the client.
- Factory methods can be parameterized.
- The returned object may be either abstract or concrete object.
- Providing hooks for subclasses is more flexible than creating objects directly.
Participants:
Product defines the interface of objects the factory method creates
ConcreteProduct implements the Product interface
Creator declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.May call the factory method to create a Product object.
ConcreteCreator overrides the factory method to return an instance of a ConcreteProduct.
Sample code:
//Product public interface IBooking { } // A ConcreteProduct public class FlightBooking : IBooking { } // A ConcreteProduct public class HotelBooking: IBooking { }
The abstract class may provide a default object, but each subclass can instantiate an extended version of the object.
An abstract class that has only abstract methods is the same as an interface. The benefit of using an abstract class comes when you want to implement functionality in the base class so each derived class doesn’t need to repeat it.
// A Creator public abstract class BookingFactory { public abstract IBooking Book(); } // A ConcreteCreator public class FlightBookingFactory : BookingFactory { public override IProduct Book() { return new FlightBooking(); } } // A ConcreteCreator public class HotelBookingFactory : BookingFactory { public override IProduct Book() { return new HotelBooking(); } }
Usage:
BookingFactory _flightBooking= new FlightBookingFactory(); BookingFactory _hotelBooking= new HotelBookingFactory(); IBooking fb = _flightBooking.Book(); IBooking hb = _hotelBooking.Book();
Use it when:
- A class can’t anticipate which kind of class of objects it must create.
- A class uses its subclasses to specify which objects it creates.
- You want to localize the knowledge of which class gets created.
There are several similar variations on the factory pattern to recognize
- The base class is abstract and the pattern must return a complete working class.
- The base class contains default methods and is only subclassed for cases where the default methods are insufficient.
- Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different.
Similar Topics:
- Implementing Abstract Factory Pattern in .net
- Implementing Unit of Work Pattern in .net
- Repository Pattern in .net
- Dependency Injection (DI) Pattern in asp.net
- Introduction To Model-View-ViewModel (MVVM) Pattern
- Test Driven Development guide