Strategy Design Pattern

yash dhingra
3 min readMar 11, 2023

The Strategy design pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. This pattern allows you to choose an algorithm at runtime and apply it to an object without knowing its implementation details.

In simpler terms, the Strategy pattern allows you to define a set of algorithms and use them interchangeably, depending on the situation or input provided.

How is the Strategy pattern different from other design patterns?

  • The Strategy pattern is different from other design patterns because it focuses on defining a family of interchangeable algorithms, while other patterns may focus on different aspects of software design, such as object creation, interaction between objects, or handling complex workflows.

How do you implement the Strategy pattern in code?

  • To implement the Strategy pattern, you need to define an interface (or abstract class) that specifies the contract for the algorithm family. Then, you can create concrete classes that implement the interface and encapsulate the algorithm logic. Finally, you need to create a context class that uses the interface to interact with the concrete classes and select the desired algorithm at runtime.

Scenario where you would use the Strategy pattern?

  • An example scenario where the Strategy pattern can be useful is a text editor that allows users to format text in different ways (e.g., bold, italic, underline). Instead of tightly coupling the formatting logic with the text editor code, you can define a set of formatting algorithms as separate classes (e.g., Bold Formatter, Italic Formatter, Underline Formatter), and allow the user to select the desired formatting at runtime.
#Basic example of strategy design pattern..
// Strategy interface
interface Formatter {
String format(String text);
}

// Concrete strategy classes
class BoldFormatter implements Formatter {
public String format(String text) {
return "<b>" + text + "</b>";
}
}

class ItalicFormatter implements Formatter {
public String format(String text) {
return "<i>" + text + "</i>";
}
}

class UnderlineFormatter implements Formatter {
public String format(String text) {
return "<u>" + text + "</u>";
}
}

// Context class
class TextEditor {
private Formatter formatter;

public void setFormatter(Formatter formatter) {
this.formatter = formatter;
}

public String formatText(String text) {
return formatter.format(text);
}
}

// Usage example
public class Main {
public static void main(String[] args) {
TextEditor editor = new TextEditor();
editor.setFormatter(new BoldFormatter());
String boldText = editor.formatText("This is bold text");
System.out.println(boldText); // Output: <b>This is bold text</b>

editor.setFormatter(new ItalicFormatter());
String italicText = editor.formatText("This is italic text");
System.out.println(italicText); // Output: <i>This is italic text</i>

editor.setFormatter(new UnderlineFormatter());
String underlineText = editor.formatText("This is underlined text");
System.out.println(underlineText); // Output: <u>This is underlined text</u>
}
}

In this example, the Formatter interface defines the contract for the formatting algorithm family, and the concrete strategy classes (Bold Formatter, Italic Formatter, Underline Formatter) encapsulate the specific formatting logic.

The Text Editor class is the context class that uses the formatter interface to interact with the concrete strategy classes. It contains a reference to a concrete formatter object and delegates the formatting logic to it.

In the usage example, the Text Editor is initialized with different concrete formatter objects at runtime, and the format Text () method is called to format a sample text input. The output shows that the formatting logic changes dynamically depending on the selected formatter.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

yash dhingra
yash dhingra

No responses yet

Write a response