Thursday, September 30, 2021

23 Design Patterns Java Developers Should know

Recently I come across a nice Java design pattern cheatsheet which






1. Abstract Factory Design Pattern
Abstract Factory is a creational Design Pattern, which is used to control the creation of Objects.

2. Adapter design pattern
The Adapter is a structural design pattern; it is used to maintain the structure of classes in object-oriented programs like Java applications.

The adapter pattern converts interface of a class into expected interface, allowing classes with incompatible interfaces to work together.
Adapters are useful for integrating existing components.

Java implementation creates a wrapper class, that is explicitly used in the code:
public interface Log {
    void warning(String message);
    void error(String message);
}
 
public final class Logger {
    void log(Level level, String message) { /* ... */ }
}
 
public class LoggerToLogAdapter implements Log {
    private final Logger logger;
 
    public LoggerToLogAdapter(Logger logger) { this.logger = logger; }
 
    public void warning(String message) {
        logger.log(WARNING, message);
    }
   
    public void error(String message) {
        logger.log(ERROR, message);
    }
}
 
Log log = new LoggerToLogAdapter(new Logger());
In Scala, we have a built-in concept of interface adapters, expressed as implicit classes:


3. Bridge Design Pattern
The bridge is also a structural design pattern

4. Builder Design Pattern
The Builder is another creational design pattern in Java.

5. Chain of Responsibility Pattern
Chain of Responsibility is a behavioral pattern from Gang of Four. It avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. You actually have a chain of receiving objects, starting from lower level to higher level, request move along the chain until an object handles it.

6. Command Design Pattern
The Command Pattern is also a behavioral design pattern and uses to control the behavior of an object or set of objects in a program. The command object is used to encapsulate a request, thus allows you to parameterize clients with different requests, queue, or log requests, and also support undo sort of functionality. As the name suggests, it is used to execute commands e.g., buttons on the remote control.

7. Composite Design Pattern
Composite pattern is another structural design pattern

8. Decorator Design Pattern
Decorator is also a structural pattern

9. Facade Design Pattern
The Facade is another structure design pattern for Java developers.

10. Factory Method Design Pattern
Like Abstract Factory, this is also a creational design pattern, provides a better way to create an instance of objects in Java or any other object-oriented programming language.

The factory method pattern provides an interface for creating an object that encapsulates the actual class instantiation in a method, and lets subclasses decide which class to instantiate.
Factory method allows to:
merge complex object creation code,
select which class to instantiate,
cache objects,
coordinate access to shared resources.
We will consider the static factory method, which is slightly different from the classical version of the pattern “ static factory method avoids sub-classing, so there's no option to override the method.
In Java, we use a new operator to instantiate a class, by invoking its constructor. To implement the pattern, we rely on ordinary methods. Also, because we canĂ¢€™t define static methods in interface, we have to use a separate factory class:
public interface Animal {}
 
private class Dog implements Animal {}
 
private class Cat implements Animal {}
 
public class AnimalFactory {
    public static Animal createAnimal(String kind) {
        if ("cat".equals(kind)) return new Cat();
        if ("dog".equals(kind)) return new Dog();
        throw new IllegalArgumentException();
    }
}
 
AnimalFactory.createAnimal("dog");

11. Flyweight Design Pattern
It's a structural pattern

12. Interpreter Design Pattern

13. Iterator Design Pattern

14. Mediator Design Pattern

15. Memento Design Pattern

16. Prototype Design Pattern

17. Proxy Design Pattern

18. Observer Design Pattern

19. Singleton Design Pattern

20. State Design Pattern

21. Strategy Design Pattern

The Strategy Pattern defines a group of polymorphic (interchangeable) and encapsulated Algorithms in a Context. If the algorithm changes then the client that uses it is not affected. Strategy Pattern can be thought as a very simple form of State Pattern where Algorithms does not access or alter the encapsulated Algorithm of the Context.
 
What Problem strategy pattern solves
 
 
When to use Strategy pattern
The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.
 
It is also an alternative of passing behaviors to methods in Java in pre Java 8 world. Now you have a better option in terms of lambda expression for passing a function as parameter. Before that you can use Strategy pattern to pass behavior. It's the nearest substitue for a function of pointer in Java.
 
To add to the already magnificient answers: The strategy pattern has a strong similarity to passing a function (or functions) to another function. In the strategy this is done by wrapping said function in an object followed by passing the object. Some languages can pass functions directly, so they don't need the pattern at all. But other languages can't pass functions, but can pass objects; the pattern then applies.
 
When we have a set of similar algorithms and its need to switch between them in different parts of the application. With Strategy Pattern is possible to avoid ifs and ease maintenance;
When we want to add new methods to superclass that donĂ¢€™t necessarily make sense to every subclass. Instead of using an interface in a traditional way, adding the new method, we use an instance variable that is a subclass of the new Functionality interface. This is known as Composition : Instead of inheriting an ability through inheritance the class is composed with Objects with the right ability;
 
1. Encoding
2. Decoding
3. Compression
4. Splitting
 
 
Advantages
 
 
Drawbacks
 
 
Problem
 Chess is a two-player strategy board game played on a chessboard, a checkered gameboard with 64 squares arranged in an eight-by-eight grid.
Each player begins the game with 16 pieces: one king, one queen, two rooks, two knights, two bishops, and eight pawns. Each of the six piece types moves differently.
Each chess piece has its own style of moving. In the diagrams
 
The king moves one square in any direction. The king has also a special move which is called castling and involves also moving a rook.
The rook can move any number of squares along any rank or file, but may not leap over other pieces. Along with the king, the rook is involved during the king's castling move.
The bishop can move any number of squares diagonally, but may not leap over other pieces.
The queen combines the power of the rook and bishop and can move any number of squares along rank, file, or diagonal, but it may not leap over other pieces.
The knight moves to any of the closest squares that are not on the same rank, file, or diagonal, thus the move forms an "L"-shape: two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The knight is the only piece that can leap over other pieces.
 
Chess is played on a square board of eight rows (called ranks and denoted with numbers 1 to 8) and eight columns (called files and denoted with letters a to h) of squares

 
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
* Problem with switch statement and solution using strategy pattern.
*
* @author
*/
public class Testing {
 
    public static void main(String args[]) {
        ChessPiece night = new ChessPiece("Night", ChessPiece.PieceType.NIGHT);
        ChessPiece rook = new ChessPiece("Rook", ChessPiece.PieceType.ROOK);
        ChessPiece bishop = new ChessPiece("Bishop", ChessPiece.PieceType.BISHOP);
        ChessPiece pawn = new ChessPiece("Pawn", ChessPiece.PieceType.PAWN);
 
        List<ChessPiece> pieces = new ArrayList<>();
        pieces.add(night);
        pieces.add(rook);
        pieces.add(bishop);
        pieces.add(pawn);
 
        for (ChessPiece p : pieces) {
            p.movePiece();
        }
    }
 
}
 
class ChessPiece {
 
    private String name;
 
    public enum PieceType {
 
        PAWN, NIGHT, ROOK, BISHOP, QUEEN, KING;
    }
 
    private PieceType type;
 
    public ChessPiece(String name, PieceType type) {
        this.name = name;
        this.type = type;
    }
 
    public void movePiece() {
        System.out.print(name + ", ");
        switch (type) {
            case ROOK:
                System.out.println("Moving horizontally");
                break;
            case BISHOP:
                System.out.println("Moving diagonally");
                break;
            case NIGHT:
                System.out.println("Moving two and half square");
                break;
            case PAWN:
                System.out.println("Moving vertically");
                break;
        }
 
    }
 
    public String name() {
        return name;
    }
}
 
Output:
Night, Moving two and half square
Rook, Moving horizontally
Bishop, Moving diagonally
Pawn, Moving vertically
 
 
Example
 
 
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
*
*
* @author
*/
public class Testing {
 
    public static void main(String args[]) {
        ChessPiece night = new ChessPiece("Night", new NightMove());
        ChessPiece rook = new ChessPiece("Rook", new RankMove());
        ChessPiece bishop = new ChessPiece("Bishop", new DiagonalMove());
        ChessPiece pawn = new ChessPiece("Pawn", new FileMove());
 
        List<ChessPiece> pieces = new ArrayList<>();
        pieces.add(night);
        pieces.add(rook);
        pieces.add(bishop);
        pieces.add(pawn);
 
        for (ChessPiece p : pieces) {
            p.movePiece();
        }
    }
 
}
 
interface MoveStrategy {
 
    public void move();
}
 
class DiagonalMove implements MoveStrategy {
 
    @Override
    public void move() {
        System.out.println("Moving diagonally");
    }
 
}
 
class FileMove implements MoveStrategy {
 
    @Override
    public void move() {
        System.out.println("Moving horinzontally");
    }
 
}
 
class RankMove implements MoveStrategy {
 
    @Override
    public void move() {
        System.out.println("Moving vertically");
    }
 
}
 
class NightMove implements MoveStrategy {
 
    @Override
    public void move() {
        System.out.println("Moving two and half squares");
    }
 
}
 
class ChessPiece {
 
    private String name;
    private MoveStrategy moveStrategy;
 
    public ChessPiece(String name, MoveStrategy strategy) {
        this.name = name;
        this.moveStrategy = strategy;
    }
 
    public void movePiece() {
        System.out.print(name + ", ");
        moveStrategy.move();
    }
 
    public String name() {
        return name;
    }
}
 
Output :
Night, Moving two and half squares
Rook, Moving vertically
Bishop, Moving diagonally
Pawn, Moving horinzontally
 
 
 
Examples from JDK
Collections.sort() and Comparable method.
 
Important points


22. Template Method Design Pattern

23. Visitor Design Pattern


And here is the big diagram which shows the relationship among different object-oriented design patterns:





That's all on this list of object-oriented design Patterns, Java developers should know. In fact, this list of patterns is equally useful for any object-oriented programmer working on object-oriented languages like C++, Scala, or any other language.

These are also known as Gang Of Four (GOF) design pattern because it was first published in the GOF design pattern book. Just keep one thing in mind, design patterns are tried and tested solutions to some particular problem, but it doesn't mean you always need them.

In fact, if you are familiar with these patterns and know their intent and when to use them, you will automatically alert by your mind about places where you can use these patterns.

I always keep these patterns back of my mind, and while coding, I somehow know that, Ok, this is the place where I should use this pattern. I never use them pre-planned; in fact, they came at random while doing coding, testing, refactoring, and code review.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.