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
  • Abstract factory
  • Prototype

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

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
  • State
  • Strategy
  • Template method

Strategy

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

Software Design Principles

Inversion of Control

Inversion of Control (IoC) is a design principle.

Depencendy Inversion

Dependency inversion is a design principle.

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);
    }
}

You might also be interested in…

External references

Leave a Reply

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