Home » Design Patterns

Tag Archives: Design Patterns

Static Factories are Great!

Every now and then I jump on classes with multiple constructors or classes that are rigorous to work with. Let alone not being able to mock part of their components and at the end being forced to use reflection for testing (mockito based, old school, you choose). Imagine a Producer class that you use for Kafka. A class that provides ...

Read More »

What are Design Patterns?

Design patterns are solutions to known problems. The pattern represents an abstracted solution to a commonly experienced problem. As the solution is abstracted it is language agnostic. A non-programmatic analogy is to think about how to solve the problem of getting wet when it rains. A common problem in rainy countries like the UK. There are several solutions: wear a ...

Read More »

State Pattern

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. Participants Context: defines the interface of interest to clients Maintains an instance of a ConcreteState subclass that defines the current state. State: defines an interface for encapsulating the behavior associated with a particular state of the Context. Concrete State: each ...

Read More »

Memento Pattern

Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later. Participants Memento: stores internal state of the Originator object. The memento may store as much or as little of the originator’s internal state as necessary at its originator’s discretion. Protect against access by objects of other than the originator. ...

Read More »

Strategy Design Pattern In Java

The strategy design pattern is a behavioral pattern in which we have multiple algorithms/strategies for achieving a task and which algorithm/strategy to use is left for the client to choose. The various algorithm options are encapsulated in individual classes. In this tutorial, we’ll learn to implement the strategy design pattern in Java. UML Representation: Let’s first start by looking at ...

Read More »

Mediator Design Pattern In Java

In this tutorial, we’ll learn about a behavioral pattern that promotes loose coupling between several objects communicating with one another. The idea behind the Mediator design pattern is to have a central object that encapsulates how a set of objects interact. In the mediator pattern, we extract all the relationships between different classes in a separate class known as a ...

Read More »

Chain Of Responsibility Design Pattern In Java

In this tutorial, we’ll learn how to implement the Chain Of Responsibility Pattern in Java. The Chain Of Responsibility design pattern involves having a chain of objects that are together responsible for handling a request. When a client sends a request, the first handler will try to process it. If it can process it, then the request processing ends here. ...

Read More »

Command Design Pattern In Java

In this tutorial, we’ll learn about the command pattern which is an important behavioral design pattern. It has some important applications like implementing undo/redo functionality in text editors. In the command design pattern, there’s a command object that sits between the sender and the receiver objects. The sender object can create a command object. The command object then calls the ...

Read More »

State Design Pattern In Java

In this tutorial, we’ll explore another popular behavioral design pattern – the State Design Pattern. The knowledge of state design pattern comes handy when we’re working with an object which can exist in multiple states. We should use it primarily when the behavior of an object depends on its current state. This pattern helps us avoid long conditional checks of ...

Read More »