Alexey Zvolinskiy

About Alexey Zvolinskiy

Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

Design Patterns: Strategy

This time I want to talk about Strategy design pattern. In this way I start articles about behavioral design patterns. These kind of patterns represent some schemas of interaction between objects to make a code more flexible and well organized.The most essential point of this approach is loose coupling between objects.

The Strategy should be used when you have several implementations for one purpose in your application. In this case you create strategy-interface, concrete realizations of the interface, and finally a context class which will encapsulate all logic in some methods. In order to understand this approach, lets see an example.

The example will be based on football. Let’s imagine that any football team can play in two manners: attacking and defending. These two tactics are particular realisations of a football strategy.

Strategy-Design-Pattern

Strategy interface:

public interface FootballStrategy {

	public void adhereTactic(String team);

}

Concrete realizations:

public class AttackTactic implements FootballStrategy {

	@Override
	public void adhereTactic(String team) {
		System.out.println(team+" will play in attacking football!");
	}

}

And

public class DefenceTactic implements FootballStrategy {

	@Override
	public void adhereTactic(String team) {
		System.out.println(team+" will make emphasis on defence!");
	}

}

Context class:

public class TacticContext {

	private FootballStrategy strategy = null;

	public void selectTactic(String team) {
		strategy.adhereTactic(team);
	}

	public FootballStrategy getStrategy() {
		return strategy;
	}

	public void setStrategy(FootballStrategy strategy) {
		this.strategy = strategy;
	}

}

Demonstration of the Strategy usage:

...
	public static void main(String[] args) {

		String team1 = "Barcelona";
		String team2 = "Real Madrid";

		TacticContext context = new TacticContext();

		context.setStrategy(new AttackTactic());
		context.selectTactic(team1);

		context.setStrategy(new DefenceTactic());
		context.selectTactic(team2);

	}
...

The result of the code execution:

Barcelona will play in attacking football!
Real Madrid will make emphasis on defence!

When to use the Strategy design pattern? Definitely when a client doesn’t need to know about implementation of concrete strategies or about data which is used there. When you want to use one class from the set dynamically. I don’t know which situations I also need to mention now. But I’m sure that my example was verbose and you can make your own conclusion about cons and pros of the Strategy design pattern.
 

Reference: Design Patterns: Strategy from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


− two = 4



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close