Abstract Factory Pattern in Java
Well, if had read my previous article on Factory Design Pattern in Java, this would be much simpler. I felt that abstract factory is a factory of factories.
When Abstract Factory ?
I found a good application in android widget library. Widget library has components like Dialogue, Toast etc. But the internal implementation might be different for different Android Versions. We can see that, for Lollipop and Nougat, the entire widget output is different. So in that case, we can create a factory for Lollipop and Nougat. Then we create a GUIFactory to handle the widget creation based on Android Version.
Similar things can be seen in the case of Ionic. Ionic had UI components like Button, Dialogue etc. Based on the OS (Android, iOS, Windows), the correct component will be instantiated.
What is the difference between Factory & Abstract Factory?
With the Factory pattern, you produce instances of implementations (Apple
, Banana
, Cherry
, etc.) of a particular interface — say, IFruit
.
With the Abstract Factory pattern, you provide a way for anyone to provide their own factory. This allows your warehouse to be either an IFruitFactory
or an IJuiceFactory
, without requiring your warehouse to know anything about fruits or juices.
Project Structure:
Android Example :
Suppose I am creating a Material UI Library for Android. For this current discussion, we limit ourselves to Material Dialogue Box.
For older versions of android (Lollipop), Dialogue Box’s might not support GIF’s and Animations. While for latest version (say Nougat) it supports GIF’s and Animations but may require an explicit permission to be provided(For may be File Upload).
We create two Factories.
- LollipopFactory
- NougatFactory
LollipopFactory is factory to instantiate different kinds of dialogues for Lollipop : Info Dialogue, Error Dialogue, File Dialogue etc.
Similar NougatFactory is factory to instantiate the above mentioned, for Nougat.
Finally we have WidgetFactory
which takes input as ANDROID.VERSION and instantiates & returns the respective Factory at Run-time.
Implementation in Java
- Lets start with the fundamental Factories.
IFactory.java
Concretions of the above Factory.
LollipopFactory.java
NougatFactory.java
2. Create the fundamental products of every factory. In our case Dialogue Box, Input Box etc.
IDialogueBox.java
3. Create products of sub-factory. ie LollipopFactory and NougatFactory.
Lollipop: 🍭
LollipopErrorDialogueBox.java
LollipopInfoDialogueBox.java
Nougat 🍫
NougatErrorDialogueBox.java
NougatInfoDialogueBox.java
4. Create Parent Factory which delegates the method calls to respective sub-factories based on ANDROID.VERSION.
WidgetFactory.java
5. Finally a Driver to test this out.
Similar Examples:
- UI Component class of Electron. For Mac OS / Windows it should instantiate different Button class based on OS.
Found it Interesting?
Please show your support by 👏. To read the complete series, click below.
Reference:
- https://dzone.com/articles/design-patterns-abstract-factory
- https://refactoring.guru/design-patterns/abstract-factory/java/example
- https://www.oodesign.com/abstract-factory-pattern.html
Disclaimer:
I myself, has just started learning, design patterns. If you find any issues please feel free to post them in the comments section below. Thank you for reading so far 😄