This article explains strategy design pattern in Java with class diagrams and example code.
Introduction – Strategy Design Pattern is a behavioral design pattern among the Gang Of Four(GOF) Design PatternsArticle on GOF Patterns & their types. I.e. the strategy design pattern deals with how the classes interact with each other.
What is Strategy Design Pattern
Strategy Pattern is used when there is a family of interchangeable algorithms of which one algorithm is to be used depending on the program execution context.
Strategy Pattern achieves this by clearly defining the family of algorithms, encapsulating each one and finally making them interchangeable. Each of the algorithms in the family can vary independently from the clients that use it.
Interchangeability is achieved by making a parent interface/base strategy class and keeping individual algorithms as children of this base class. The client class holds a reference to the base strategy and at runtime receives the correct algorithm child instance depending on the execution context.
Lets have a look at the class diagram of the strategy pattern –
Strategy Design Pattern Class Diagram
Explanation of Class Diagram
BaseStrategyclass is an abstract class which represents the algorithm.
executeAlgorithm()is the abstract method in
BaseStrategywhich is responsible for executing the algorithm, but is puposefully kept abstract so that each of the child strategies/algorithms can implement this method with their own logic.
ConcreteStrategy2are two subclasses of
BaseStrategy. These 2 are infact two different variants of the algorithm represented by the
BaseStrategyto provide the implementation of the algorithm variant as per their own needs.
- Lastly, the
Contextclass invokes this algorithm. Based on the need of the execution context specific variant of the
BaseStrategy– either of
ConcreteStrategy2is assigned to the
executeAlgorithm()method, but the actual algorithm which is executed is based on which instance of
ConcreteStrategyis held by
Note – I have shown 2 concrete instances named ConcreteStrategy1 & ConcreteStrategy2 just for explanation. There can be more than 2 concrete instances as per needs of the design.
Strategy Design Pattern – Example Use Case in Java
The Java Example’s Class Diagram is for a family of algorithms for file parsers where –
BaseFileParserclass is an abstract base class which represents family of
FileParserinstances in the design. It has a single abstract method
parseFile()which needs to be invoked to parse a file. This method will be overridden by individual parsers next.
BaseFileParser. Both also override
parseFile()method as per their own logic for processing information in XML and CSV formats.
Clientclass needs to parse files and hence is a consumer of
Clientclass holds an instance of
BaseFileParserwhich is initialized with the correct subtype of
CSVFileParser. This happens at the time of instantiation of the
Clientusing a parameterized constructor which assigns the correct file parser to the
parseFile()method is invoked, it in turn invokes
parseFile()method of the child of
CSVFileParser) which is contained in
baseFileParservariable at that moment.
- Thus, appropriate CSV or XML file parsing happens based on the context.
- As you might have noticed in the
main()method that the information of which child parser(CSV or XML) is to be assigned to the
baseFileParservariable will be passed at runtime. This information can also be passed through the command line arguments
Clientis able to parse the incoming file by selecting at runtime one of the strategies of CSV or XML based.
- If another file format, say PDF, needs to be implemented in future then all we need is to create a new subclass of
PDFFileParserand implement PDF file handling logic in its
This concludes the article on strategy design pattern where we looked at what is the pattern, pattern class diagram and its explanation and finally an example of the pattern in java with class diagram and sample code.