DEV Community

Pablo Cavalcante
Pablo Cavalcante

Posted on

Strategy Design Pattern in Java with Enum

If you are interested in this article it’s very likely you have already heard about design patterns, but if you haven’t heard or never had contact and just fell off here by parachute, let’s go to a timid introduction about what it’s.

Design Patterns

Design Patterns are reusable and customizable solutions to recurring problems in software development. Overall, a design pattern is made up of four elements:

  • Pattern name: the reference used to describe a design problem, its solutions and its consequences;

  • Problem: the context in which design pattern is applicable;

  • Solution: the elementary composition of the design pattern, its relationships, its responsibilities etc.

  • Consequences: the advantages and disadvantages of applying the design pattern.

Nowadays, there are 23 design patterns distributed categorically in: creation pattern; structural pattern and behavioral pattern. The pattern in question in this article is the Strategy, it belongs to the category of behavioral patterns.

Strategy

Strategy is a behavioral design pattern whose intention is to define a family of algorithms, encapsulate each one of them and make them interchangeable, in other words, Strategy allows the algorithm to vary regardless of the clients use it.

There are many scenarios where applying this design pattern is very useful. For example, in a backing application there may be several means by which we can transfer money from one account to another: PIX, TED, DOC, TEF.

However, the intention is always the same: to transfer the money; and what changes is just how it will be done. In the context of software programming, what changes is the used algorithm.

Generic structure

That is Strategy’s generic structure:

Strategy’s generic structure

Let’s talk about each of the components (or participants):

  • Strategy: defines the generic interface for all supported algorithms. The Context (client’s role) uses that interface to invoke the defined algorithm by a ConcreteStrategy.
  • ConcreteStrategy: it’s the algorithm itself that is implemented using the Strategy interface.
  • Context: referentially keeps up a Strategy instance.

Strategy with Enum

Previously, it has been said that one of the characteristics of design patterns besides reuse is customization. Well, implementing Strategy with Enum is one of the custom ways to implement this Design Pattern (and my favorite). To exemplify, I will use the Java programming language.

That’s Enum Strategy groups the execution strategies (algorithms) and also already defines the common interface that the strategies must implement.

Enum Strategy groups the execution strategies

Here comes an intermediate and encapsulating class which the client can instantiate it already passing which strategy he wants to use.

Intermediate and encapsulating class

Finally, we have the client (or context) which keeps up one indirect reference to the chosen strategy using the UseStrategy class.

Client

Furthermore, it’s worth remembering that design patterns can be creatively implemented in different ways and are very useful for common problems in software development. In other words, it’s worth getting to know each one of them. Well, that 's it! See you guys. 👋👨‍💻

Top comments (0)