designpatterns/creational booknotes
Creational Design Pattern that lets you produce families of related objects without specifying their concrete classes.
Factory Method and Abstract Factory
Factory Method focuses on one product (Button), letting subclasses decide which concrete one to return.
Abstract Factory handles multiple related products — keeping the family consistent (no mixing Windows button with Mac checkbox).
This pattern considered as another layer of abstraction over factory pattern.
Abstract Factory patterns work around a super-factory which creates other factories.

Java-Learn/src/com/DesignPatterns/AbstractFactory/Example3 at main · nmdra/Java-Learn · GitHub
Abstract Factory provides a high level blueprint that defines rules for creating families of related object without specifying their concrete classes.
Concrete Factories implement the rules specified by the abstract factory. It contain the logic for creating specific instances of objects within a family.
Client utilizes the abstract factory to create families of objects without specifying their concrete types and interacts with objects through abstract interfaces provided by abstract products.
SA03.3 - Factory Method Pattern > Abstract Factory and Abstract Method
Factory, Factory Method & Abstract Factory
// Step 1: Define Abstract Product Interfaces
interface Button {
void render();
}
interface Checkbox {
void render();
}
// Step 2: Implement Concrete Products for Windows
class WindowsButton implements Button {
public void render() {
System.out.println("Rendering a Windows Button.");
}
}
class WindowsCheckbox implements Checkbox {
public void render() {
System.out.println("Rendering a Windows Checkbox.");
}
}
// Step 3: Implement Concrete Products for MacOS
class MacButton implements Button {
public void render() {
System.out.println("Rendering a MacOS Button.");
}
}
class MacCheckbox implements Checkbox {
public void render() {
System.out.println("Rendering a MacOS Checkbox.");
}
}
// Step 4: Define Abstract Factory
interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}
// Step 5: Implement Concrete Factories
class WindowsFactory implements GUIFactory {
public Button createButton() {
return new WindowsButton();
}
public Checkbox createCheckbox() {
return new WindowsCheckbox();
}
}
class MacFactory implements GUIFactory {
public Button createButton() {
return new MacButton();
}
public Checkbox createCheckbox() {
return new MacCheckbox();
}
}
// Step 6: Client Code
public class AbstractFactoryExample {
public static void main(String[] args) {
// Get the appropriate factory
GUIFactory factory = getGUIFactory("MacOS");
// Create multiple related products using a single factory
Button button = factory.createButton();
Checkbox checkbox = factory.createCheckbox();
// Render UI elements
button.render(); // Output: Rendering a MacOS Button.
checkbox.render(); // Output: Rendering a MacOS Checkbox.
}
// Client
private static GUIFactory getGUIFactory(String osType) {
if ("Windows".equalsIgnoreCase(osType)) {
return new WindowsFactory();
} else if ("MacOS".equalsIgnoreCase(osType)) {
return new MacFactory();
} else {
throw new IllegalArgumentException("Unknown OS type");
}
}
}