• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Strategy Design Pattern Implementation using Java

July 20, 2017 By j2eereference Leave a Comment

What is Strategy Design Pattern?

Strategy design pattern, also called as policy design pattern is utilized for managing (i) algorithms, (ii) relationships and (iii) responsibilities among the objects. Strategy design pattern is a behaviour pattern. In this pattern, a class algorithm or its behaviour can be altered during run time.

Few Strategy design pattern examples include

  • Documents being saved in various file formats like .docx, .html, .rtf and excel documents saved as .xls, .csv.
  • Files being compressed by .zip, .rar and image files being compressed as .jpeg, .png, .bmp, .tif
  • Using various formats like bar chart, points etc, for plotting the same statistics.

 Purpose of Strategy Design Pattern

Strategy design pattern is intended to define a group of algorithms, then encapsulate each of the algorithms and permit them to become interchangeable. Strategy aims at letting the algorithm differ independently from Clients who are using it.

 Strategy Design Pattern Implementation using Java

Strategy design pattern is now demonstrated with an example. It is illustrated step by step.

Step 1: First a Strategy interface is created which describes an operation.

OperationStrategy.java

 

Java
1
2
3
public interface OperationStrategy {
   public int arithmeticOperation(int m, int n);
}

Step 2: In the next step, concrete classes are created. Here three implementations are made, one each for addition, subtraction and multiplication.

Addition.java

Java
1
2
3
4
5
6
public class Addition implements OperationStrategy{
   @Override
   public int arithmeticOperation(int m, int n) {
      return m + n;
   }
}

Subtraction.java

Java
1
2
3
4
5
6
public class Subtraction implements OperationStrategy{
@Override
   public int arithmeticOperation(int m, int n) {
      return m - n;
   }
}

Multiplication.java

Java
1
2
3
4
5
6
public class Multiplication implements OperationStrategy{
   @Override
   public int arithmeticOperation(int m, int n) {
      return m * n;
   }
}

Step 3: The Context Class is created in the next step. The context is used for observing the alteration in its behavior when the strategy is altered.

OperationContext.java

Java
1
2
3
4
5
6
7
8
9
10
public class OperationContext {
   private OperationStrategy strategy;
 
   public OperationContext(OperationStrategy strategy){
      this.strategy = strategy;
   }
   public int performStrategy(int m, int n){
      return strategy.arithmeticOperation(m, n);
   }
}

Step 4: As shown below, StrategyPatternTrial, the demo class, will utilize OperationContext and strategy objects for representing alteration in Context behaviour on the basis of the strategy it uses.

StrategyPatternTrial.java

Java
1
2
3
4
5
6
7
8
9
10
11
public class StrategyPatternTrial {
public static void main(String[] args) {
      OperationContext context = new OperationContext(new Addition());
      System.out.println("12 + 4 = " + context.performStrategy(12,4));
      context = new OperationContext(new Subtraction());
      System.out.println("12 - 4 = " + context.performStrategy(12, 4));
 
      context = new OperationContext(new Multiplication());
      System.out.println("12 * 4 = " + context.performStrategy(12, 4));
   }
}

Step 5: After execution, output of the above program would be:

12 + 4 = 16
12 – 4 = 8
12 * 4 = 48

Related Posts

  • Façade pattern
  • What is Design pattern ?
  • Design pattern – Service Locator
  • Singleton

Filed Under: Design Patterns

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.