Provide an interface for creating families of related or dependent objects without specifying their concrete classes. The pattern is very similar to the previous pattern. It is one level of abstraction higher than the other. The Abstract Factory is a factory object that returns one of several factories.
- Creates families of related or dependent objects like Kit.
- Provides a class library of products, exposing interface not implementation.
- Needs to isolate concrete classes from their super classes.
One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation. Actually, the delegated object frequently uses factory methods to perform the instantiation.
Participants:
AbstractFactory declares an interface for operations that create abstract products.
ConcreteFactory implements operations to create concrete products.
AbstractProduct declares an interface for a type of product objects.
Product defines a product to be created by the corresponding ConcreteFactory; it implements the AbstractProduct interface.
Client uses the interfaces declared by the AbstractFactory and AbstractProduct classes.
Sample code:
// An AbstractProduct public interface IFlightBooking { } // A Product public class FlightBooking: IFlightBooking { } // An AbstractProduct public interface IHotelBooking { } // A Product public class HotelBooking: IHotelBooking { } public interface IMoveBooking { } // A Product public class MoveBooking: IMoveBooking { } public interface IDramaBooking { } // A Product public class DramaBooking: IDramaBooking { } //Abstract Factory for PackageTour public abstract class TourBookingFactory { public abstract IFlightBooking BookFlight(); public abstract IHotelBooking BookHotel(); } //Abstract Factory for Entertainment public abstract class EntertainmentFactory { public abstract IMoveBooking BookMove(); public abstract IDramaBooking BookDrama(); } //A ConcreteFactory for package tour booking public class PackageBookingFactory : TourBookingFactory { public PackageBookingFactory() { } public override IFlightBooking BookFlight() { return new FlightBooking(); } public override IHotelBooking BookHotel() { return new HotelBooking(); } } //A ConcreteFactory for Entertainment booking public class EntertainmentBookingFactory : EntertainmentFactory { public EntertainmentBookingFactory() { } public override IMoveBooking BookMove() { return new MoveBooking(); } public override IDramaBooking BookDrama() { return new DramaBooking(); } } // The Client public class BookingClient { private IFlightBooking flightBooking; private IHotelBooking hotelBooking; private IMoveBooking MoveBooking; private IDramaBooking DramaBooking; public BookingClient(PackageBookingFactory factory) { flightBooking = factory.BookFlight(); hotelBooking = factory.BookHotel(); } public BookingClient(EntertainmentBookingFactory factory) { MoveBooking = factory.BookMove(); DramaBooking = factory.BookDrama(); } public void CreatePackage() { // Implementation for Package tour feature } public void CreateEntertainmentPackage() { //Implementation for entertainment booking feature } }
In above example you can find the abstract factory implementation. In above implementation you can find the package tour and entertainment booking feature implementation. As per above implementation package trip and Move booking both are different model. Abstract factory taking care of the dependency between different entity and loosely coupled.
Usage:
//Booking Package trip PackageBookingFactory factory = new PackageBookingFactory(); BookingClient client = new BookingClient(factory ); client.CreatePackage(); //Booking for entertainment package EntertainmentBookingFactory factory = new EntertainmentBookingFactory(); BookingClient client = new BookingClient(factory ); client.CreateEntertainmentPackage();
Use it when:
- A system should be independent of how its products are created, composed and represented.
- A system should be configured with one of multiple families of products.
- A family of related products are designed to be used together, and you need to enforce this constraint.
- You want to provide a class library of products and you want only to present them with their interfaces, not their implimentations.
When to use which ?
- Use the Factory Method pattern when there is a need to decouple a client from a particular product that it uses. Use the Factory Method to relieve a client of responsibility for creating and configuring instances of a product.
- Use the Abstract Factory pattern when clients must be decoupled from product classes. Especially useful for program configuration and modification The Abstract Factory pattern can also enforce constraints about which classes must be used with others. It may be a lot of work to make new concrete factories.
Similar Topics:
- Implementing Factory Pattern in .net
- Dependency Injection (DI) Pattern in asp.net
- Repository Pattern in .net
- Implementing Unit of Work Pattern in .net
- Introduction To Model-View-ViewModel (MVVM) Pattern
- Test Driven Development guide
- Outlook Web Access corrupts HTML attachments