Software Modelling and Design

Gang of Four Patterns

Gang of Four Patterns

Content and images from Larman: Applying UML and Refactoring Guru

Taxonomy

Behavioural

Creational

Structural

Adapter

Problem:

Solution: Convert the original interface of a component into another interface through an intermediate adapter object

Implementation:

Object adapter

Example: airline flights

// flight adapter interface
public interface IFlightAdapter {
    String from();
    String to();
    String departureTime();
    String arrivalTime();
}

public VirginFlightAdapter implements IFlightAdapter {
    // context maintained within the adapter
    VirginFlight flight;

    VirginFlightAdapter(VirginFlight flight) {
        this.flight = flight;
    }

    // implement methods of interface by calling services of VirginFlight
    public String from() {
        return flight.getDeparture();
    }
}

public QantasFlightAdapter implements IFlightAdapter { ... }

public JetstarFlightAdapter implements IFlightAdapter { ... }

// singleton factory to create adapters
public AirlineAdapterFactory {
     
    public static AirlineAdapterFactory getInstance() { ... }

    public IFlightAdapter getFlightAdapter(Object Flight) {
        // determine correct adapter to create
        if (flight instanceof VirginFlight) {
            return new VirginFlightAdapter(flight);
        } else if (...) {
            // ...
        }
    }
}

Related:

Pros:

Cons:

Factory

Problem:

Solution: create a Factory, a Pure Fabrication, that handles object creation

Implementation:

Factory for an adapter

Related:

Pros:

Cons:

Singleton

Problem:

Solution: use a Singleton class which has only one instance, providing a global point of access through a static method

Implementation:

public class Singleton {
    private static instance = null;

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

Related:

Pros:

Cons:

Strategy

Problem:

Solution: define each algorithm in a separate class, implementing a common interface

Implementation:

Strategy Pattern

public interface RouteStrategy {
    Route getRoute(Location A, Location B);
}

public class CycleStrategy implements RouteStrategy {
    public Route getRoute(Location A, Location B) {
        // get cycling route
        // ...
        return cycleRoute;
    }
}

public class BusStrategy implements RouteStrategy { ... }

public class TrainStrategy implements RouteStrategy { ... }

Related:

Pros:

Cons:

Composite

Problem: how to treat a group/composition structure of objects the same way (polymorphically) as a non-composite (atomic) object?

Solution: define classes for both atomic and composite objects, both implementing the same interface

Implementation:

Composite

Notes:

Related:

Pros:

Cons:

Facade

Problem: need a common, unified interface to a disparate set of implementations/interfaces (e.g. a subsystem) to prevent undesirable coupling to the subsystem whose implementation may change.

Solution: define a single point of contact to the subsystem via a facade object. The facade object then wraps the subsystem. This provides a unified interface and is responsible for collaborating with subsystem components.

Implementation:

ATM with a facade

Note ATM is a controller, while BankAccountFacade is the Facade.

Related:

Pros:

Cons:

Observer

Problem: different kinds of subscriber objects are interested in state changes/events of a publisher object, and want to react in their own unique way when the publisher generates an event. The publisher wants to maintain low coupling to the subscribers

Solution: Define a subscriber or listener interface, implemented by subscribers. The publisher can dynamically register interested subscribers and notify them when an event occurs.

Implementation:

Observer pattern

Related:

Pros:

Cons:

Decorator

Problem: how to dynamically (at run-time) add behaviour/state to individual objects without changing the interface presented to the client?

Solution:

Decorator acting as a wrapper

Implementation:

Decorator Pattern

Related:

Pros:

Cons:


Edit this page.