• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Strategy Design Pattern In Java

August 21, 2007 //  by Krishna Srinivasan//  Leave a Comment

Strategy design pattern falls under the category of Behavioural patterns. Assume that we have an object and its behavior is largely dependent on the state of its internal variables.

Strategy pattern allows, object to behave in different ways depends on the internal state. Strategy pattern is defined in the book Gang of four is,

Allows an object to alter its behaviour when its internal behaviour changes. The object will appear to change its class.

Consider the following example,

Person.java

[code lang=”java”]package tips.pattern.strategy;
public class Person {
private String name;
private String character;

public Person(String name, String character) {
this.name = name;
this.character = character;
}

public void giveMeMoney() {
if (character.equals("GoodCharacter")) {
System.out.println("Yes, take all my money");
} else if (character.equals("BadCharacter")) {
System.out.println("No, I dont have anything with me");
} else if (character.equals("OtherCharacter")) {
System.out.println("I will give the money tmrw");
}
}
}[/code]

We have modeled a Person object passing his name and a string representing his character, which may be a Good Character or Bad Character or some Other Character. If someone comes to him and asks for money by calling the Person.giveMeMoney() method, his behavior is entirely dependant on his character. Technically, his behavior is tightly coupled with the internal variable character.

The above design has one great dis-advantage. Suppose we want to add further characters that represent some other different behavior of a Person, then we end up in adding if-else clauses, which certainly is not very good from a design perspective because of changes being done in the existing code.

  • Factory Pattern
  • Template Pattern
  • Abstract Factory Pattern
  • Observer Pattern

So, how to provide support for different characteristic behaviors for a person without making code changes? Here comes the Strategy pattern which just do that. The State pattern mandates to model behavior as interfaces rather than representing them as Strings or some other types. For example, since character is the changing behavior for a Person, let us encapsulate it by having something like the following,

Character.java

[code lang=”java”]package tips.pattern.strategy;

public interface Character {
public void giveMeMoney();
}[/code]

See, we have modeled a person’s Character as an interface. Now, let us see the implementation for different characteristic feature of a person, say GoodCharacter and BadCharacter.

GoodCharacter.java

[code lang=”java”]package tips.pattern.state;

public class GoodCharacter implements Character {
@Override
public void giveMeMoney() {
System.out.println("Yes, take all my money");
}
}[/code]

BadCharacter.java

[code lang=”java”]package tips.pattern.state;

public class BadCharacter implements Character {
@Override
public void giveMeMoney() {
System.out.println("No, I dont have anything with me");
}
}[/code]

Now, let us see the new Person class which accepts character in the form of an interface rather than as a String. Given below is the code snippet for the same.

Person2.java

[code lang=”java”]package tips.pattern.state;

public class Person2 {
private String name;
private Character character;

public Person2(String name, Character character) {
this.name = name;
this.character = character;
}

public void giveMeMoney() {
character.giveMeMoney();
}

public static void main(String[] args) {
Person2 object = new Person2("John", new GoodCharacter());
object.giveMeMoney();
object = new Person2("John", new BadCharacter());
object.giveMeMoney();
}
}[/code]

Since now the character is represented as an interface, there wont be any more code changes if there is a need to add new characteristic behavior. All we need to do is to define the new character class implementing the Character interface.

Category: JavaTag: Design Patterns

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Using the new Process Builder class
Next Post: Template method Pattern – Design Patterns in Java/J2EE »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact