designpatterns/creational booknotes

Creational Design pattern that provide an interface for creating objects in a superclass, but allows sub-classes to alter the type of objects that will be created.

Creator is an abstract class or interface that declares the factory method.
This method is responsible for creating Product objects, but it delegates the actual creation to sub-classes.
Concrete creators are sub-classes of the Creator that implement the factory method.
Factory method is a method defined in the Creator class that is responsible for creating Product objects.
It’s typically declared as abstract in the Creator and implemented in the Concrete Creators.

// Step 1: Define Product Interface
interface Button {
    void render();
}
 
// Step 2: Implement Concrete Products
class WindowsButton implements Button {
    public void render() {
        System.out.println("Rendering a Windows Button.");
    }
}
 
class MacButton implements Button {
    public void render() {
        System.out.println("Rendering a MacOS Button.");
    }
}
 
// Step 3: Define Factory Interface
abstract class ButtonFactory {
    public abstract Button createButton();
}
 
// Step 4: Implement Concrete Factories
class WindowsButtonFactory extends ButtonFactory {
    public Button createButton() {
        return new WindowsButton();
    }
}
 
class MacButtonFactory extends ButtonFactory {
    public Button createButton() {
        return new MacButton();
    }
}
 
// Step 5: Client Code
public class FactoryMethodExample {
    public static void main(String[] args) {
        // Get a button factory based on OS
        ButtonFactory factory = getButtonFactory("MacOS");
        
        // Create button using factory method
        Button button = factory.createButton();
        button.render();  // Output: Rendering a MacOS Button.
    }
 
    private static ButtonFactory getButtonFactory(String osType) {
        if ("Windows".equalsIgnoreCase(osType)) {
            return new WindowsButtonFactory();
        } else if ("MacOS".equalsIgnoreCase(osType)) {
            return new MacButtonFactory();
        } else {
            throw new IllegalArgumentException("Unknown OS type");
        }
    }
}

Abstract Factory and Abstract Method

What are the differences between Abstract Factory and Factory design patterns? - Stack Overflow
Factory Comparison

  • Factory Method: Each product has its own factory.
  • Abstract Factory: One factory produces multiple related products.

The main difference between Abstract Factory and Factory method is that Abstract Factory is implemented by Composition; but Factory Method is implemented by inheritance
👉 SA03.2 - Design Principles > Favor Composition over Inheritance

  • Factory Method is used to create one product only but Abstract Factory is about creating families of related or dependent products.
  • Factory Method pattern exposes a method to the client for creating the object whereas in the case of Abstract Factory they expose a family of related objects which may consist of these Factory methods.
  • Factory Method pattern hides the construction of a single object whereas Abstract Factory hides the construction of a family of related objects. Abstract factories are usually implemented using (a set of) factory methods.
  • Abstract Factory pattern uses composition to delegate the responsibility of creating an object to another class while Factory Method design pattern uses inheritance and relies on a derived class or subclass to create an object.
  • The idea behind the Factory Method pattern is that it allows for the case where a client doesn’t know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job while Abstract Factory pattern is best utilized when your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details.!

Warning

Be aware that although the GoF define two different Factory patterns, these are not the only Factory patterns in existence. They are not even necessarily the most commonly used Factory patterns. A famous third example is Josh Bloch’s Static Factory Pattern from Effective Java. The Head First Design Patterns book includes yet another pattern they call Simple Factory.
Don’t fall into the trap of assuming every Factory pattern must match one from the GoF.

👉 Factory, Factory Method & Abstract Factory