Design Patterns Overview

In this post, we will take a look into What are design patterns, the use of design patterns, how to use design patterns, and categories of design patterns as per GOF design patterns.

What are Design Patterns?

In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

Uses of Design Patterns

  • Design patterns can speed up the development process by providing tested, proven development paradigms. 
  • Often, people only understand how to apply certain software design techniques to certain problems. These techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that doesn't require specifics tied to a particular problem.
  • In addition, patterns allow developers to communicate using well-known, well-understood names for software interactions. Common design patterns can be improved over time, making them more robust than ad-hoc designs.

Object-oriented design principles

Before reading design patterns, I suggest reading some useful oops design principles:
  • Encapsulate what varies.
  • Code to an interface rather than to an implementation
  • Delegation principle
  • The open-closed principle (OCP)
  • DRY- Don't Repeat Yourself 
  • Single Responsibility Principle (SRP)
  • Liskov Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Injection or Inversion principle
Read more detail about object-oriented design principles in my post at https://www.javaguides.net/2018/08/object-oriented-design-principles-in-java.html

How to Use a Design Pattern

Let's discuss a step-by-step approach to applying a design pattern effectively:

1. Read the pattern once through for an overview. Pay particular attention to the Applicability and Consequences sections to ensure the pattern is right for your problem.

2. Go back and study the Structure, Participants, and Collaborations sections. Make sure you understand the classes and objects in the pattern and how they relate to one another.

3. Look at the Source Code section to see a concrete example of the pattern code. Studying the code helps you learn how to implement the pattern.

4. Choose names for pattern participants that are meaningful in the application context. The names of participants in design patterns are usually too abstract to appear directly in an application. Nevertheless, it's useful to incorporate the participant's name into the name that appears in the application. That helps make the pattern more explicit in the implementation.
For example, if you use the Strategy pattern for a text compositing algorithm, then you might have classes SimpleLayoutStrategy or TeXLayoutStrategy.

5. Define the classes. Declare their interfaces, establish their inheritance relationships, and define the instance variables that represent data and object references. Identify existing classes in your application that the pattern will affect, and modify them accordingly.

6. Define application-specific names for operations in the pattern. Here again, the names generally depend on the application. Use the responsibilities and collaborations associated with each operation as a guide. Also, be consistent in your naming conventions. For example, you might use the"Create-" prefix consistently to denote a factory method.
7. Implement the operations to carry out the responsibilities and collaborations in the pattern. The source code section offers hints to guide you in the implementation. 

Organizing the Catalog

Design patterns are organized into Creational, Structural, or Behavioral purposes.
  • Creational patterns concern the process of object creation. 
  • Structural patterns deal with the composition of classes or objects. 
  • Behavioral patterns characterize the ways in which classes or objects interact and distribute responsibility. 

1. Creational Patterns

In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.

2. Structural Patterns

In Software Engineering, Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize relationships between entities.

3. Behavioral Patterns

In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.
Download source code from our Github Repository :
https://github.com/RameshMF/gof-java-design-patterns