How to Implement a Strategy Pattern using Enum in Java?
Last Updated :
12 Sep, 2022
The strategy design pattern is intended to provide a way to choose from a variety of interchangeable strategies. Classic implementation includes an architecture to be implemented by each technique and offers a concrete implementation for execution. The method is chosen from an interface reference, and the execution method is called.
The classic implementation of the Strategy design pattern:
The strategy interface must be implemented by all strategies.
public interface Strategy {
public void execute();
}
All strategies must implement the strategy interface two classes showing the implementation of the Strategy interface and created an execute method.
public class StrategyA implements Strategy {
@Override
public void execute(){
System.out.print("Executing strategy A");
}
}
public class StrategyB implements Strategy {
@Override
public void execute() {
System.out.print("Executing strategy B");
}
}
The main class will select the strategy and execute the execute method of the strategy we choose.
public class GFG {
private Strategy strategy;
public void setStrategy(Strategy strategy){
this.strategy = strategy;
}
public void executeStrategy(){
this.strategy.execute();
}
}
An example of using the Strategy.
public class UseStrategy {
public static void main(String[] args){
Context context = new Context();
context.setStrategy(new StrategyA());
context.executeStrategy();
context.setStrategy(new StrategyB());
context.executeStrategy();
}
}
Enum implementation of the Strategy design pattern:
It will have two classes: an enum class and a class that uses it. All the magic happens in the enum, where the concrete implementation of the strategy is done to define each enum constant.
Java
// Java program to Implement a Strategy Pattern using Enum
enum Strategy {
STRATEGY_A {
@Override
void execute() {
System.out.println("Executing strategy A");
}
},
STRATEGY_B {
@Override
void execute() {
System.out.println("Executing strategy B");
}
};
abstract void execute();
}
class UseStrategy {
public static void main(String[] args)
{
UseStrategy useStrategy = new UseStrategy();
useStrategy.perform(Strategy.STRATEGY_A);
useStrategy.perform(Strategy.STRATEGY_B);
}
private void perform(Strategy strategy) {
strategy.execute();
}
}
OutputExecuting strategy A
Executing strategy B
Similar Reads
EnumMap clear() Method in Java with Examples The Java.util.EnumMap.clear() method in Java is used to remove all the mappings from the map. The method does not delete the map instead it just clears the map of the mappings. Syntax: enum_map.clear() Parameters: This method does not accept any argument. Return Values: This method does not return a
2 min read
Java Program to Define Ordinal() Method Using Enum Concept Java enum, also called Java enumeration type, is a type whose fields consist of a fixed set of constants. The java.lang.Enum.ordinal() tells about the ordinal number(it is the position in its enum declaration, where the initial constant is assigned an ordinal of zero) for the particular enum. ordina
2 min read
Java Program to Access All the Constant Defined in the Enum An enum is a special class that represents a group of constants. To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; } values() method can be used to return all val
1 min read
EnumMap equals() Method in Java with Examples The Java.util.EnumMap.equals(obj) in Java is used to compare the passed object with this EnumMap for the equality. It must be kept in mind that the object passed must be a map of the same type as the EnumMap.Syntax: boolean equals(Object obj) Parameter: The method takes one parameter obj of Object t
2 min read
Java Program to Convert Enum to String Given an enum containing a group of constants, the task is to convert the enum to a String. Methods: We can solve this problem using two methods: Using name() MethodUsing toString() Method Let us discuss both of them in detail and implementing them to get a better understanding of the same. Method 1
2 min read