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.
Here is an example in Java:
...
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// Abstract product A
public interface Button {
void render();
} |
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// Abstract product B
public interface Checkbox {
void render();
}
|
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// ConcreteAbstract product A1Factory
public classinterface WindowsButton implementsGUIFactory Button {
publicButton void rendercreateButton();
{ System.out.println("Rendering Windows style button..."Checkbox createCheckbox();
}
}} |
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// Concrete product B1A2
public class WindowsCheckboxMacOSButton implements CheckboxButton {
public void render() {
System.out.println("Rendering WindowsMacOS style checkboxbutton...");
}
} |
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// Concrete product A2B2
public class MacOSButtonMacOSCheckbox implements ButtonCheckbox {
public void render() {
System.out.println("Rendering MacOS style buttoncheckbox...");
}
} |
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// Concrete productFactory B22
public class MacOSCheckboxMacOSGUIFactory implements CheckboxGUIFactory {
public voidButton rendercreateButton() {
System.out.println("Rendering MacOS style checkbox..."return new MacOSButton();
}
}
// Abstract Factory public interfaceCheckbox GUIFactorycreateCheckbox() {
Button createButton return new MacOSCheckbox();
Checkbox}
createCheckbox();
}
} |
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// Concrete Factoryproduct 1A1
public class WindowsGUIFactoryWindowsButton implements GUIFactoryButton {
public Buttonvoid createButtonrender() {
return new WindowsButton(System.out.println("Rendering Windows style button...");
}
} |
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// Concrete product B1
public class WindowsCheckbox implements Checkbox createCheckbox {
public void render() {
return new WindowsCheckbox(System.out.println("Rendering Windows style checkbox...");
}
} |
Code Block |
---|
|
package AbstractFactoryMethodDemo;
// Concrete Factory 21
public class MacOSGUIFactoryWindowsGUIFactory implements GUIFactory {
public Button createButton() {
return new MacOSButtonWindowsButton();
}
public Checkbox createCheckbox() {
return new MacOSCheckboxWindowsCheckbox();
}
}
// Client code
|
Code Block |
---|
|
package AbstractFactoryMethodDemo;
public class Client {
private Button button;
private Checkbox checkbox;
public Client(GUIFactory factory) {
button = factory.createButton();
checkbox = factory.createCheckbox();
}
public void renderGUI() {
button.render();
checkbox.render();
}
public static void main(String[] args) {
Client windowsClient = new Client(new WindowsGUIFactory());
windowsClient.renderGUI(); // Renders Windows style button and checkbox
Client macClient = new Client(new MacOSGUIFactory());
macClient.renderGUI(); // Renders MacOS style button and checkbox
}
} |
...
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.