Software Design Patterns

A software design pattern, in the context of software engineering, is a general, reusable solution to a commonly occurring problem within a given context in software design.

Design patterns became popular after the publication of the book “Design patterns” (1995), written by four authors known as the Gang of Four (GoF).

List of Software Design Patterns

A full list of most common software design patterns can be found on this link.

Main categories for software design patterns:

  1. Creational
  2. Structural
  3. Behavioral

Creational controls how objects are created, composed, and represented.

Structural are concerned with how classes and objects are composed to form larger structures.

Behavioural are focused on communication between classes.

Creational

Creational patterns:

  • Singleton
  • Builder
  • Factory method
  • Abstract factory
  • Prototype
  • Dependency injection
  • Service locator

Singleton

Singleton ensure that an instance is called only once.

Singleton example in Java:

public class SingletonExample {

    public static class DatabaseConnection {
        private static DatabaseConnection instance;
        private String connectionString;

        private DatabaseConnection() {
            this.connectionString = "jdbc:h2:mem:testdb";
            System.out.println("DatabaseConnection instance created.");
        }

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

        public void executeQuery(String query) {
            System.out.println("Executing query: '" + query + "' using connection string: " + this.connectionString);
        }
    }

    public static void main(String[] args) {
        DatabaseConnection conn1 = DatabaseConnection.getInstance();
        conn1.executeQuery("SELECT * FROM users");

        DatabaseConnection conn2 = DatabaseConnection.getInstance();
        conn2.executeQuery("INSERT INTO products VALUES (1, 'Widget')");

        System.out.println("Are conn1 and conn2 the same instance? " + (conn1 == conn2));
    }
}

Some developers recommend to apply the dependency inversion principle and dependency injection programming technique instead of the Singleton pattern. That means that there should be an outer class that constructs the inner providing an unique instance as a parameter, instead of making a class that calls from the inside a Singleton class to get the outer instance. The justification behind this is achieving loose coupling, because Singleton makes the inner class dependent of the outer class.

Builder

Builder pattern at Wikipedia

Abstract factory

Abstract factory is sometimes refered just as factory. It creates objects without specifying the exact class.

It was one of the original design patterns of the Gang of Four.

Abstract factory at Wikipedia

Dependency injection

Dependency injection (DI) decouples object creation from usage.

It means that the object or function that uses an object is different from the object or function that creates the object.

It did not appear among the original design patterns of the GoF.

It is part of a broader set of Inversion of Controls (IoC) patterns.

Dependency injection at Wikipedia

public class DependencyInjectionExample {

    interface MessageService {
        void sendMessage(String message);
    }

    static class EmailService implements MessageService {
        @Override
        public void sendMessage(String message) {
            System.out.println("Sending email with message: " + message);
        }
    }

    static class SmsService implements MessageService {
        @Override
        public void sendMessage(String message) {
            System.out.println("Sending SMS with message: " + message);
        }
    }

    static class NotificationService {
        private final MessageService messageService;

        public NotificationService(MessageService messageService) {
            this.messageService = messageService;
        }

        public void notifyUser(String message) {
            this.messageService.sendMessage(message);
        }
    }

    public static void main(String[] args) {
        MessageService emailService = new EmailService();
        NotificationService notificationByEmail = new NotificationService(emailService);

        MessageService smsService = new SmsService();
        NotificationService notificationBySms = new NotificationService(smsService);
    }
}

Service locator

Service locator is a design pattern not included among the GoF’s ones.

It implements the IoC software design principle.

It is generally considered less favorable that DI due to hidden dependencies.

Structural

Structural patterns

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Proxy
  • Dependency injection

Behavioral

Behavioral patterns:

  • Visitor
  • Chain of responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • Pub-Sub
  • State
  • Strategy
  • Template method
  • Event loop

Strategy

Strategy pattern allows to have different methods or algorithms and decide it on execution time.

Observer

Observer is a design pattern.

Pub-Sub

Publish-Subscribe (Pub-Sub) is a design pattern, not included among the original 23 GoF’s dessign patterns.

It is commonly compared with the Observer design pattern.

Event loop

Event loop is a design pattern not included among the GoF’s ones.

It implements the IoC software design principle.

Software Design Pattern Groupings

GoF’s Software Design Patterns

GoF’s software design patterns is a historical category.

All these patterns have in common that they were all published within the book “Design patterns” (1995), written by the authors Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, known collectively as the Gang of Four (GoF).

GoF’s software design patterns:

  1. Creational
    • Singleton
    • Factory Method
    • Abstract Factory
    • Builder
    • Prototype
  2. Structural
    • Adapter
    • Bridge
    • Composite
    • Decorator
    • Facade
    • Flyweight
    • Proxy
  3. Behavioral
    • Chain of Responsibility
    • Command
    • Interpreter
    • Iterator
    • Mediator
    • Memento
    • Observer
    • State
    • Strategy
    • Template Method
    • Visitor

You might also be interested in…

External references

Leave a Reply

Your email address will not be published. Required fields are marked *