Abstract Factory Method Demo

package AbstractFactoryMethodDemo; // Abstract product A public interface Button { void render(); }
package AbstractFactoryMethodDemo; // Abstract product B public interface Checkbox { void render(); }
package AbstractFactoryMethodDemo; // Abstract Factory public interface GUIFactory { Button createButton(); Checkbox createCheckbox(); }

 

Let's consider a real-world example with a software application that supports multiple operating systems (OS) like Windows and MacOS. Depending on the OS, the application should create appropriate controls like buttons, checkboxes, and menus. Here, we can use the Abstract Factory pattern to create a separate factory for each OS.

In this example, Button and Checkbox are the abstract products. WindowsButton, WindowsCheckbox, MacOSButton, and MacOSCheckbox are the concrete products. GUIFactory is the abstract factory that declares a set of methods for creating each abstract product. WindowsGUIFactory and MacOSGUIFactory are the concrete factories that implement these creation methods to produce the concrete products. The Client uses only interfaces declared by the abstract factory and abstract product classes. This allows you to create a new factory that creates controls for a different OS without changing the client code.