<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Strategy_Design_Pattern</id>
	<title>Strategy Design Pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Strategy_Design_Pattern"/>
	<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Strategy_Design_Pattern&amp;action=history"/>
	<updated>2026-04-22T15:28:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>http://rasimsen.com/index.php?title=Strategy_Design_Pattern&amp;diff=523&amp;oldid=prev</id>
		<title>Rasimsen: /* Strategy Design Pattern Important Points */</title>
		<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Strategy_Design_Pattern&amp;diff=523&amp;oldid=prev"/>
		<updated>2018-07-23T18:03:00Z</updated>

		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Strategy Design Pattern Important Points&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en-GB&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 18:03, 23 July 2018&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l192&quot; &gt;Line 192:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 192:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Strategy Design Pattern Important Points==&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Strategy Design Pattern Important Points==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*We could have used composition to create instance variable for strategies but we should avoid that as we want the specific strategy to be applied for a particular task. Same is followed in Collections.sort() and Arrays.sort() method that take comparator as argument.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*We could have used composition to create instance variable for strategies but we should avoid that as we want the specific strategy to be applied for a particular task. Same is followed in &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;Collections.sort()&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039; &lt;/ins&gt;and &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;Arrays.sort()&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039; &lt;/ins&gt;method that take comparator as argument.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*Strategy Pattern is very similar to State Pattern. One of the difference is that Context contains state as instance variable and there can be multiple tasks whose implementation can be dependent on the state whereas in strategy pattern strategy is passed as argument to the method and context object doesn’t have any variable to store it.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*Strategy Pattern is very similar to State Pattern. One of the difference is that Context contains state as instance variable and there can be multiple tasks whose implementation can be dependent on the state whereas in strategy pattern strategy is passed as argument to the method and context object doesn’t have any variable to store it.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*Strategy pattern is useful when we have multiple algorithms for specific task and we want our application to be flexible to chose any of the algorithm at runtime for specific task.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*Strategy pattern is useful when we have multiple algorithms for specific task and we want our application to be flexible to chose any of the algorithm at runtime for specific task.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Rasimsen</name></author>
		
	</entry>
	<entry>
		<id>http://rasimsen.com/index.php?title=Strategy_Design_Pattern&amp;diff=522&amp;oldid=prev</id>
		<title>Rasimsen: Created page with &quot;Report Ad Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the...&quot;</title>
		<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Strategy_Design_Pattern&amp;diff=522&amp;oldid=prev"/>
		<updated>2018-07-23T18:00:39Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Report Ad Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Report Ad&lt;br /&gt;
Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
Strategy pattern is also known as [[Policy Pattern]]. We define multiple algorithms and let client application pass the algorithm to be used as a parameter.&lt;br /&gt;
&lt;br /&gt;
One of the best example of strategy pattern is Collections.sort() method that takes Comparator parameter. Based on the different implementations of Comparator interfaces, the Objects are getting sorted in different ways.&lt;br /&gt;
&lt;br /&gt;
==Example - Shopping Card==&lt;br /&gt;
For our example, we will try to implement a simple Shopping Cart where we have two payment strategies – using Credit Card or using PayPal.&lt;br /&gt;
&lt;br /&gt;
First of all we will create the interface for our strategy pattern example, in our case to pay the amount passed as argument.&lt;br /&gt;
&lt;br /&gt;
===PaymentStrategy.java===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
package com.journaldev.design.strategy;&lt;br /&gt;
&lt;br /&gt;
public interface PaymentStrategy {&lt;br /&gt;
&lt;br /&gt;
	public void pay(int amount);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we will have to create concrete implementation of algorithms for payment using credit/debit card or through paypal.&lt;br /&gt;
&lt;br /&gt;
===CreditCardStrategy.java===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.journaldev.design.strategy;&lt;br /&gt;
&lt;br /&gt;
public class CreditCardStrategy implements PaymentStrategy {&lt;br /&gt;
&lt;br /&gt;
	private String name;&lt;br /&gt;
	private String cardNumber;&lt;br /&gt;
	private String cvv;&lt;br /&gt;
	private String dateOfExpiry;&lt;br /&gt;
	&lt;br /&gt;
	public CreditCardStrategy(String nm, String ccNum, String cvv, String expiryDate){&lt;br /&gt;
		this.name=nm;&lt;br /&gt;
		this.cardNumber=ccNum;&lt;br /&gt;
		this.cvv=cvv;&lt;br /&gt;
		this.dateOfExpiry=expiryDate;&lt;br /&gt;
	}&lt;br /&gt;
	@Override&lt;br /&gt;
	public void pay(int amount) {&lt;br /&gt;
		System.out.println(amount +&amp;quot; paid with credit/debit card&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===PaypalStrategy.java===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.journaldev.design.strategy;&lt;br /&gt;
&lt;br /&gt;
public class PaypalStrategy implements PaymentStrategy {&lt;br /&gt;
&lt;br /&gt;
	private String emailId;&lt;br /&gt;
	private String password;&lt;br /&gt;
	&lt;br /&gt;
	public PaypalStrategy(String email, String pwd){&lt;br /&gt;
		this.emailId=email;&lt;br /&gt;
		this.password=pwd;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	@Override&lt;br /&gt;
	public void pay(int amount) {&lt;br /&gt;
		System.out.println(amount + &amp;quot; paid using Paypal.&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now our strategy pattern example algorithms are ready. We can implement Shopping Cart and payment method will require input as Payment strategy.&lt;br /&gt;
&lt;br /&gt;
===Item.java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.journaldev.design.strategy;&lt;br /&gt;
&lt;br /&gt;
public class Item {&lt;br /&gt;
&lt;br /&gt;
	private String upcCode;&lt;br /&gt;
	private int price;&lt;br /&gt;
	&lt;br /&gt;
	public Item(String upc, int cost){&lt;br /&gt;
		this.upcCode=upc;&lt;br /&gt;
		this.price=cost;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public String getUpcCode() {&lt;br /&gt;
		return upcCode;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public int getPrice() {&lt;br /&gt;
		return price;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ShoppingCart.java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.journaldev.design.strategy;&lt;br /&gt;
&lt;br /&gt;
import java.text.DecimalFormat;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class ShoppingCart {&lt;br /&gt;
&lt;br /&gt;
	//List of items&lt;br /&gt;
	List&amp;lt;Item&amp;gt; items;&lt;br /&gt;
	&lt;br /&gt;
	public ShoppingCart(){&lt;br /&gt;
		this.items=new ArrayList&amp;lt;Item&amp;gt;();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void addItem(Item item){&lt;br /&gt;
		this.items.add(item);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void removeItem(Item item){&lt;br /&gt;
		this.items.remove(item);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public int calculateTotal(){&lt;br /&gt;
		int sum = 0;&lt;br /&gt;
		for(Item item : items){&lt;br /&gt;
			sum += item.getPrice();&lt;br /&gt;
		}&lt;br /&gt;
		return sum;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void pay(PaymentStrategy paymentMethod){&lt;br /&gt;
		int amount = calculateTotal();&lt;br /&gt;
		paymentMethod.pay(amount);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that payment method of shopping cart requires payment algorithm as argument and doesn’t store it anywhere as instance variable.&lt;br /&gt;
&lt;br /&gt;
Let’s test our strategy pattern example setup with a simple program.&lt;br /&gt;
&lt;br /&gt;
===ShoppingCartTest.java===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.journaldev.design.strategy;&lt;br /&gt;
&lt;br /&gt;
public class ShoppingCartTest {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		ShoppingCart cart = new ShoppingCart();&lt;br /&gt;
		&lt;br /&gt;
		Item item1 = new Item(&amp;quot;1234&amp;quot;,10);&lt;br /&gt;
		Item item2 = new Item(&amp;quot;5678&amp;quot;,40);&lt;br /&gt;
		&lt;br /&gt;
		cart.addItem(item1);&lt;br /&gt;
		cart.addItem(item2);&lt;br /&gt;
		&lt;br /&gt;
		//pay by paypal&lt;br /&gt;
		cart.pay(new PaypalStrategy(&amp;quot;myemail@example.com&amp;quot;, &amp;quot;mypwd&amp;quot;));&lt;br /&gt;
		&lt;br /&gt;
		//pay by credit card&lt;br /&gt;
		cart.pay(new CreditCardStrategy(&amp;quot;Rasim SEN&amp;quot;, &amp;quot;1234567890123456&amp;quot;, &amp;quot;321&amp;quot;, &amp;quot;12/18&amp;quot;));&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of above program is:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
50 paid using Paypal.&lt;br /&gt;
50 paid with credit/debit card&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Design Pattern Class Diagram==&lt;br /&gt;
https://cdn.journaldev.com/wp-content/uploads/2013/07/Strategy-Pattern-450x261.png&lt;br /&gt;
&lt;br /&gt;
==Strategy Design Pattern Important Points==&lt;br /&gt;
*We could have used composition to create instance variable for strategies but we should avoid that as we want the specific strategy to be applied for a particular task. Same is followed in Collections.sort() and Arrays.sort() method that take comparator as argument.&lt;br /&gt;
*Strategy Pattern is very similar to State Pattern. One of the difference is that Context contains state as instance variable and there can be multiple tasks whose implementation can be dependent on the state whereas in strategy pattern strategy is passed as argument to the method and context object doesn’t have any variable to store it.&lt;br /&gt;
*Strategy pattern is useful when we have multiple algorithms for specific task and we want our application to be flexible to chose any of the algorithm at runtime for specific task.&lt;/div&gt;</summary>
		<author><name>Rasimsen</name></author>
		
	</entry>
</feed>