Design Patterns – Facade, Builder and Strategy

  • Introduction: Why Design Patterns Matter

  • Facade Pattern (Structural)

  • Builder Pattern (Creational)

  • Strategy Pattern (Behavioral)

  • Comparative Matrix & Key Takeaways

Mastering Object-Oriented Design: Facade, Builder, and Strategy Patterns

Author: Wiktor Gindorowicz | Technical Scrum Master & Software Engineer

Published: October 2026

Software development is rarely just about writing code that works; it is about writing code that remains maintainable, scalable, and adaptable over time. Object-Oriented Design Patterns represent proven, industry-tested solutions to common architectural challenges.

 

Introduction: Why Design Patterns Matter

In this article, we will break down three essential patterns—Facade, Builder, and Strategy—exploring their real-world analogies, structural mechanics, advantages, pseudocode, and Java implementations.

Facade Pattern (Structural)

Overview & Core Concept

According to the principles of structural design, a system often grows complex as it scales, accumulating dozens of interconnected classes, subsystem initializations, and intricate execution flows. The Facade Pattern provides a simplified, high-level interface to a complex framework, library, or set of subsystem classes.

Instead of forcing client code to manage dependencies and execute steps in a strict order, the Facade offers a single entry point that orchestrates the underlying complexity.

Real-World Analogy

Think of starting a modern car. As a driver (client), you don’t want to manually manage fuel valves, electrical pumps, and spark plugs. Instead, you use a single interface: the ignition button (CarFacade). When you call startCar(), the facade automatically coordinates the internal subsystems behind the scenes: it first turns on the ignition (ignition.turnOn()), primes the fuel pump (fuelPump.inject()), and finally fires up the engine (engine.start()).

When to Apply

  • When you need a limited, straightforward interface to a complex subsystem.

  • When you want to structure a subsystem into layers (using Facades as entry points for each layer).

Pros & Cons

  • Pros: Isolates your code from the complexity of a subsystem; decouples client code from third-party frameworks.

  • Cons: A Facade risks becoming a “God Object” coupled to all classes of an app if not kept focused.


// Subsystem Components
class Engine {
    public void start() { 
        System.out.println("Engine started successfully."); 
    }
}

class Ignition {
    public void turnOn() { 
        System.out.println("Ignition switched ON."); 
    }
}

class FuelPump {
    public void inject() { 
        System.out.println("Fuel pump pressurized and injected."); 
    }
}

// Facade Class
public class CarFacade {
    private final Engine engine = new Engine();
    private final Ignition ignition = new Ignition();
    private final FuelPump fuelPump = new FuelPump();

    public void startCar() {
        ignition.turnOn();
        fuelPump.inject();
        engine.start();
    }
}

// Execution
public class Main {
    public static void main(String[] args) {
        CarFacade car = new CarFacade();
        car.startCar(); // Single entry point call
    }
}

// UML Diagram

PlantUML Syntax:
@startuml
skinparam backgroundColor transparent
skinparam DefaultFontName Arial
skinparam ArrowColor #3b82f6
skinparam ClassBackgroundColor #1e293b
skinparam ClassBorderColor #334155
skinparam ClassFontColor #f8fafc
skinparam NoteBackgroundColor #0f172a
skinparam NoteBorderColor #334155
skinparam NoteFontColor #e2e8f0

class Client

class CarFacade {
    - Engine engine
    - Ignition ignition
    - FuelPump fuelPump
    + startCar()
}

class Engine {
    + start()
}

class Ignition {
    + turnOn()
}

class FuelPump {
    + inject()
}

Client --> CarFacade : calls
CarFacade --> Ignition : controls
CarFacade --> FuelPump : controls
CarFacade --> Engine : run
@enduml

// Pseudocode

class Engine:
method start(): print “Engine running”

class Ignition:
method turnOn(): print “Ignition ON”

class FuelPump:
method inject(): print “Fuel injected”

class CarFacade:
private engine = new Engine()
private ignition = new Ignition()
private fuelPump = new FuelPump()

method startCar():
ignition.turnOn()
fuelPump.inject()
engine.start()

Builder Pattern (Creational)

Overview & Core Concept

The Builder Pattern is a creational pattern designed to separate the construction of a complex object from its representation.

When a class constructor requires five, six, or more parameters—many of which are optional—you face the “Telescoping Constructor” anti-pattern (constructors calling constructors with varying parameters). The Builder pattern allows you to construct complex objects step-by-step using a readable, fluent interface.

Real-World Analogy

Think of custom-building a gravel bicycle in a specialized workshop. Instead of dealing with a massive constructor cluttered with parameters or modifying parts on a finished bike, you use a dedicated configurator (Builder). Step by step, you specify the required frame and optional handlebars using a fluent interface (return this). Before the bike leaves the workshop, a final quality check in build() ensures the mandatory frame is present. Once completed, you receive a fully assembled, immutable Bicycle whose specs cannot be altered anymore.

When to Apply

  • To get rid of a “telescoping constructor” with too many optional parameters.

  • When you want your code to create different representations of some product (e.g., carbon gravel bike vs. aluminum road bike).

Pros & Cons

  • Pros: Allows step-by-step object creation; defers execution steps; enforces the Single Responsibility Principle by separating construction from business logic.

  • Cons: Increases overall code complexity by requiring multiple new classes.



public final class Bicycle {
    private final String frame;
    private final String handlebars;

    private Bicycle(Builder builder) {
        this.frame = builder.frame;
        this.handlebars = builder.handlebars;
    }

    public void displayInfo() {
        System.out.println("Bicycle Specs -> Frame: " + frame + " | Handlebars: " + handlebars);
    }

    public static class Builder {
        private String frame;
        private String handlebars;

        public Builder frame(String frame) {
            this.frame = frame;
            return this;
        }

        public Builder handlebars(String handlebars) {
            this.handlebars = handlebars;
            return this;
        }

        public Bicycle build() {
            if (frame == null || frame.isBlank()) {
                throw new IllegalStateException("Frame is required to build a bicycle.");
            }
            return new Bicycle(this);
        }
    }
}

// UML Diagram

PlantUML Syntax:

@startuml
skinparam backgroundColor transparent
skinparam DefaultFontName Arial
skinparam ArrowColor #3b82f6
skinparam ClassBackgroundColor #1e293b
skinparam ClassBorderColor #334155
skinparam ClassFontColor #f8fafc

class Bicycle {
- String frame
- String handlebars
- Bicycle(Builder builder)
+ displayInfo()
}

class Builder {
- String frame
- String handlebars
+ frame(String frame): Builder
+ handlebars(String h): Builder
+ build(): Bicycle
}

Builder ..> Bicycle :creates (Fluent API)
@enduml

// Pseudocode

class Bicycle:
field frame: String
field handlebars: String
constructor(builder):
this.frame = builder.frame
this.handlebars = builder.handlebars

class Builder:
field frame: String
field handlebars: String

method frame(value):
this.frame = value
return this

method build():
if frame is null: error “Frame is mandatory”
return new Bicycle(this)

Strategy Pattern (Behavioral)

Overview & Core Concept

The Strategy Pattern is a behavioral pattern that defines a family of algorithms, encapsulates each one into a separate class, and makes their objects interchangeable.

Instead of embedding multiple algorithms directly into a class using monolithic conditional statements (if/else or switch), Strategy extracts the algorithms into separate classes (strategies) that adhere to a common interface.

Real-World Analogy

Imagine checking out in an e-commerce cart (ShoppingCart). At the final step, the system needs to charge a specific BigDecimal amount, but it doesn’t care whether you pay via BLIK or a Credit Card. The cart acts as a context holding a general PaymentStrategy. At runtime, you inject a specific implementation (e.g., BLIKPayment via setPaymentStrategy()). When checkout() is triggered, the cart simply delegates the payment to the chosen strategy without knowing its internal processing details.

When to Apply

  • When you want to use different variants of an algorithm within an object and switch from one algorithm to another during runtime.

     
  • When you have a lot of similar classes that only differ in the way they execute some behavior.

     
  • To isolate the business logic of a class from the implementation details of algorithms.

     

Pros & Cons

  • Pros: You can swap algorithms used inside an object at runtime; adheres to the Open/Closed Principle (add new strategies without modifying context code); eliminates bulky conditional logic.

  • Cons: Clients must be aware of the differences between strategies to select the right one.


import java.math.BigDecimal;

// Strategy Interface
interface PaymentStrategy {
    void pay(BigDecimal amount);
}

// Concrete Strategy: BLIK
class BLIKPayment implements PaymentStrategy {
    @Override
    public void pay(BigDecimal amount) {
        System.out.println("Processing " + amount + " PLN payment via BLIK code.");
    }
}

// Concrete Strategy: Credit Card
class CreditCardPayment implements PaymentStrategy {
    @Override
    public void pay(BigDecimal amount) {
        System.out.println("Processing " + amount + " PLN payment via Credit Card.");
    }
}

// Context Class
public class ShoppingCart {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void checkout(BigDecimal amount) {
        if (paymentStrategy == null) {
            throw new IllegalStateException("Payment strategy cannot be null. Please select a method.");
        }
        paymentStrategy.pay(amount);
    }
}

// UML Diagram

PlantUML Syntax:
@startuml
skinparam backgroundColor transparent
skinparam DefaultFontName Arial
skinparam ArrowColor #3b82f6
skinparam ClassBackgroundColor #1e293b
skinparam ClassBorderColor #334155
skinparam ClassFontColor #f8fafc
skinparam InterfaceBackgroundColor #0f172a
skinparam InterfaceBorderColor #3b82f6
skinparam InterfaceFontColor #f8fafc

class ShoppingCart {
    - PaymentStrategy paymentStrategy
    + setPaymentStrategy(strategy)
    + checkout(BigDecimal amount)
}

interface PaymentStrategy {
    + void pay(BigDecimal amount)
}

class BLIKPayment {
    + pay(BigDecimal amount)
}

class CreditCardPayment {
    + pay(BigDecimal amount)
}

ShoppingCart --> PaymentStrategy : uses
BLIKPayment ..|> PaymentStrategy
CreditCardPayment ..|> PaymentStrategy
@enduml

// Pseudocode

interface PaymentStrategy:
method pay(amount)

class BLIKPayment implements PaymentStrategy:
method pay(amount): print “Paid via BLIK: ” + amount

class ShoppingCart:
private strategy: PaymentStrategy

method setPaymentStrategy(newStrategy):
this.strategy = newStrategy

method checkout(amount):
strategy.pay(amount)

Comparative Matrix & Key Takeaways

Pattern
Category
Primary Intent
Key Benefit
Real-World E-Commerce Example
Facade
Structural
Provide a simplified interface to a complex set of classes.
Hides subsystem complexity from the client.
Single-click checkout triggering order, inventory, and shipping services.
Builder
Creational
Construct complex objects step-by-step.
Avoids telescoping constructors and separates construction logic.
Building custom product bundles or tailored bicycles.
Strategy
Behavioral
Encapsulate interchangeable algorithms behind a common interface.
Swaps behavior at runtime and eliminates huge switch/if blocks.
Selecting payment providers (BLIK, Credit Card, PayPal) or shipping methods.

Summary & Key Takeaways

Design patterns are tools, not silver bullets. Understanding when and why to apply them dictates clean architecture:

  • Use Facade to tame external library clutter or complex subsystems.

  • Use Builder to construct complex, immutable objects safely and readably.

  • Use Strategy to isolate interchangeable algorithms and purge legacy conditional spaghetti code.

Mastering these patterns bridges the gap between raw syntax proficiency and robust enterprise-grade engineering.

More Info

Looking for a professional who understands technology? Let's talk about how I can help your team deliver better products in an agile environment.

You have been successfully Subscribed! Ops! Something went wrong, please try again.

Quick Links

Services

About Me

Projects

Contact

Address

+48 660544777

Phone Number

wiktor.gindorowicz@gmail.com

Email Address

© 2026 dev @mr_cyclist