3 Design Patterns Every Developer Should Know

16 juni 2020 om 14:00 by ParTech Media - Post a comment

What is a Design Pattern?

A problem that occurs over and over again in a development environment is described by a pattern. It further describes the core of the solution to that problem in a way that the solution is reused a million times. The pattern consists of four essential elements:

  • Pattern name: It is a design vocabulary to a design pattern, it's solutions and consequences.
  • Problem: It describes when the pattern is to be applied considering the problem and its context.
  • Solution: It describes the elements that constitute the design, relationships, responsibilities and collaborations.
  • Consequences: These are the results and trade-offs of applying the pattern.

How to use a Design Pattern?

  • Read and take an overview of the pattern.
  • Study the structure, participants, and collaboration sections again.
  • Take help from the Sample Code section to see a concrete example of the pattern in code.
  • Choose names for pattern participants meaningful to the application context.
  • Define classes.
  • Define application-specific names for operations in the pattern.
  • Implement the operations.

Design Pattern Catalog

Design patterns are classified based on purpose and scope. Purpose reflects what the pattern does, and can have a creational, structural, or behavioral purpose. The scope specifies whether the pattern applies primarily to classes or objects. Creational patterns deal with the process of object creation. Structural patterns are concerned with the composition of classes or objects. Behavioral patterns do characterization of how classes or objects interact and distribute responsibly.

Scope Creational Structural Behavioral
Class Factory Method Adapter Interpreter
Template Method
Object Abstract Factory Adapter Chain of responsibility
Builder Bridge Command
Prototype Composite Iterator
Singleton Decorator Mediator
Facade Memento
Flyweight Observer
Proxy State
Strategy
Visitor
Let us see below some of the most popular design patterns categorized based on the purposes:

Creational Design Pattern

Singleton Pattern

Singleton pattern is a creational design pattern that ensures a class has only one instance which can be accessed globally. The class ensures that no other instance is created from outside the class by declaring a private constructor. A static field of the type of singleton also needs to be declared. The method getInstance() assures that only one instance of this class is created at runtime. Frameworks like Spring, CDI, or EJBs implement this pattern. Java also uses this one typical example being Calendar which doesn’t allow to make an instance of that class and getInstance() method to get the object to be used.

Structure of Singleton Pattern

img

Applicability

You may use the Singleton pattern when:

  • the class must have exactly one instance, and it must be accessible from a well-known access point.
  • when the sole instance is extensible by subclassing, and clients are able to use an extended instance without modifying their code.

Consequences

The Singleton pattern has several benefits:

  • Controlled access to the sole instance
  • Reduced namespace.
  • It permits refinement of operations and representation.
  • It permits a variable number of instances.
  • More flexible than class operations.

Structural Design Pattern

Decorator Pattern

Consider an example, we have a TextView object that displays text in a window. By default, the object does not have any scroll bars, as we may not always need them. But we add them using a ScrollDecorator. Further, we also want to add a thick black border around the TextView. This addition can be done using BorderDecorator. We compose the decorators with the TextView to produce the desired result. This adding on additional responsibilities to the TextView object is where the Decorator pattern comes into play.

The goal of the Decorator design is to modify an objects' functionality at runtime. The decorator conforms to the interface of the component it decorates so that its presence is transparent to the component's clients. The decorator forwards requests to the component and may perform additional actions (such as drawing a border) before or after forwarding. Transparency lets you nest decorators, recursively, thereby allowing an unlimited number of added responsibilities.

Structure of Decorator Pattern

Structure

alt_text Example

Applicability

Use Decorator

  • to add additional responsibilities to individual objects dynamically and transparently, without affecting other objects.
  • to withdraw responsibilities from an object.
  • when it's impractical to extend a subclass.
Consequences

The Decorator pattern has at least two key benefits and two liabilities:

  • More flexibility than static inheritance.
  • Avoids feature-laden classes high up in the hierarchy.
  • A decorator and its components aren't identical.
  • Lots of little objects.

Behavioral Design Pattern

Strategy Design Pattern

Strategy pattern intends to define a family of algorithms under encapsulation and abstraction, making them interchangeable and thus allowing switching out one algorithm for another without modifying the client. Hence, the code receives runtime instructions specifying which of the group of algorithms to run instead of directly implementing a single algorithm. This pattern is thought of as an enhanced version of the if-else statement where a method from a base class has its interface made that can be used to figure out the appropriate implementation of the method from one of the derived classes. Thus the pattern allows the algorithm to vary from client to client.

Structure of Strategy Pattern

alt_text

Applicability

Use the Strategy pattern when:

  • many related classes differ only in their behavior.
  • you need different variants of an algorithm.
  • an algorithm uses data that clients shouldn't know about.
  • a class defines many behaviors and these appear as multiple conditional statements in its operations.
Consequences

The Strategy pattern has the following benefits and drawbacks:

  • Families of related algorithms: Inheritance can help factor out the standard functionality of the algorithms.
  • An alternative to subclassing.
  • Strategies eliminate conditional statements.

Concluson

We hope the concept of design patterns has become much clearer to you after reading this article. As mentioned, these are some of the more popular design patterns used by developers in detail, but you can study other patterns as well when you decide to solve any problem for your project with the help of patterns. Although patterns are helpful for every programming language and project, generally patterns are designed keeping object-oriented theory in mind for programming languages such as Java and C#. Do you find patterns helpful while developing your project? Which patterns do you prefer in your development? Share with us!

Nieuwste