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.
With the Factory pattern, you produce instances of implementations (
Cherry, etc.) of a particular interface — say,
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.
Android Example :
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 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.
Concretions of the above Factory.
2. Create the fundamental products of every factory. In our case Dialogue Box, Input Box etc.
3. Create products of sub-factory. ie LollipopFactory and NougatFactory.
4. Create Parent Factory which delegates the method calls to respective sub-factories based on ANDROID.VERSION.
5. Finally a Driver to test this out.
- 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.
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 😄