Abstract Factory pattern, another GoF pattern which falls under the category of creational design patterns, deals with a factory of factories (sometimes called a "super factory"). Main intent of this pattern is to instantiate families of related objects without exposing their concrete classes to the client and thereby reducing the complexity when dealing with large systems.
Factory Method pattern may seem quite similar to this except Abstract Factory deals with multiple families of related products where as in Factory Method pattern we have only one main product.
Factory Method pattern may seem quite similar to this except Abstract Factory deals with multiple families of related products where as in Factory Method pattern we have only one main product.
Basic Structure of Abstract Factory pattern
An Implementation with an added extension
The example follows a process of building up a look and feel theme. Here the abstract factory is ThemeFactory. For each distinct theme there's a separate concrete factory, MetalThemeFactory and NimbusThemeFactory which are subclasses of ThemeFactory.
To keep it minimal I've only used two product families, Window and Button. So bear in mind that we can add other elements such as ScrollBars, TextBoxes and so on as product families which build up a theme. For each product family added, a corresponding method must also be added in ThemeFactory class, for instance createScrollBar() and createTextBox().
The added extension in the below implementation (ie. ThemeProducer and Theme classes) which differs from the basic structure of Abstract Factory pattern adds more convenience for the Client in terms of creating Themes, but at the same time it restricts the system from being open to extension. So it's up to the designer to choose the appropriate method to use according to the situation.
The added extension in the below implementation (ie. ThemeProducer and Theme classes) which differs from the basic structure of Abstract Factory pattern adds more convenience for the Client in terms of creating Themes, but at the same time it restricts the system from being open to extension. So it's up to the designer to choose the appropriate method to use according to the situation.
Product classes (Highlighted in green in the class diagram)
Window product family - Window.java
MetalWindow.java
NimbusWindow.java
MetalWindow.java
NimbusWindow.java
Button product family - Button.java
MetalButton.java
NimbusButton.java
MetalButton.java
NimbusButton.java
Creator classes (Highlighted in yellow in the class diagram)
Abstract Factory - ThemeFactory.java
Concrete Factory - MetalThemeFactory.java
Concrete Factory - NimbusThemeFactory.java
Concrete Factory - NimbusThemeFactory.java
Theme.java
ThemeProducer.java
Client.java
Output:
Output:
Thank you for reading.
No comments:
Post a Comment