Abstract factory pattern

Abstract factory gives you the ability to create a number of related classes. It provides an interface that encapsulates the capability to create a group of classes that are related in some way.

You are already aware of the factory method pattern. In other words, an abstract factory is an interface that groups together related factory methods.

For example, KTM bikes creates various types of bikes using a number of various factory methods, whereas the abstract factory for KTM brings all these factory methods together and gives you a generic interface so that you can similarly implement an Aprilia bikes abstract factory, and so on.

Let's take a look at the following UML diagram for the abstract factory pattern for our understanding:

Now let's look at a simple abstract factory pattern example in the code. Here, let's say we want to create a simple application with a user interface. The user interface includes graphical components menu, status bar, and a wizard screen interface.

While these components would be better if created by their own factory methods, for simplicity, we club them altogether in one abstract factory interface, IUIAbsFactory:

    public interface IUIAbsFactory 
{
IMenu GetMenu();
IWizard CreateWizard();
IStatusBar CreateStatusBar();
}

You can see that menu, screens, wizards, and status bar are UI components related to each other for a specific app. Based on this abstract factory interface, we can have, for example, DarkUIAbsFactory and LightUIAbsFactory concrete factory classes. We will take a look at the concrete factory implementation in a later section again, where we'll enhance our design further by utilizing the template method pattern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.178.211