Consider this example for easy understanding.
What does telecommunication companies provide? Broadband, phone line and mobile for instance and you're asked to create an application to offer their products to their customers.
Generally what you'd do here is, creating the products i.e broadband, phone line and mobile are through your Factory Method where you know what properties you have for those products and it's pretty straightforward.
Now, the company wants to offer their customer a bundle of their products i.e broadband, phone line, and mobile altogether, and here comes the Abstract Factory to play.
Abstract Factory is, in other words, are the composition of other factories who are responsible for creating their own products and Abstract Factory knows how to place these products in more meaningful in respect of its own responsibilities.
In this case, the BundleFactory
is the Abstract Factory, BroadbandFactory
, PhonelineFactory
and MobileFactory
are the Factory
. To simplify more, these Factories will have Factory Method to initialise the individual products.
Se the code sample below:
public class BroadbandFactory : IFactory { public static Broadband CreateStandardInstance() { // broadband product creation logic goes here }}public class PhonelineFactory : IFactory { public static Phoneline CreateStandardInstance() { // phoneline product creation logic goes here }}public class MobileFactory : IFactory { public static Mobile CreateStandardInstance() { // mobile product creation logic goes here }}public class BundleFactory : IAbstractFactory { public static Bundle CreateBundle() { broadband = BroadbandFactory.CreateStandardInstance(); phoneline = PhonelineFactory.CreateStandardInstance(); mobile = MobileFactory.CreateStandardInstance(); applySomeDiscountOrWhatever(broadband, phoneline, mobile); } private static void applySomeDiscountOrWhatever(Broadband bb, Phoneline pl, Mobile m) { // some logic here // maybe manange some variables and invoke some other methods/services/etc. }}
Hope this helps.