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.
One of the best example of strategy pattern is
- Programming to interface.
- Strategy pattern strategy is passed as argument to the method.
We define multiple algorithms and let client application pass the algorithm to be used as a parameter.
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.- Programming to interface.
- Strategy pattern strategy is passed as argument to the method.
We define multiple algorithms and let client application pass the algorithm to be used as a parameter.
Intent:
- Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
- Capture the abstraction in an interface, bury implementation details in derived classes
based on the argument which will be using inheritance/ implementation of interface.
Check list:
- Identify an algorithm (i.e. a behavior) that the client would prefer to access through a "flex point".
- Specify the signature for that algorithm in an interface.
- Bury the alternative implementation details in derived classes.
- Clients of the algorithm couple themselves to the interface.
Thumb Rules:
- Strategy is like Template Method except in its granularity.
- State is like Strategy except in its intent. State uses dynamic binding whereas strategy using one-time binding.
- State, Strategy, Bridge have some structure but they differ in intent which is, they solve different problems.
- Strategy objects often make good Flyweights.
Practical Derivation:
- Define the interface of an interchangeable family of algorithms
- Bury algorithm implementation details in derived classes
- Derived classes could be implemented using the Template Method pattern
- Clients of the algorithm couple themselves strictly to the interface
Example 1
package Strategy;
interface Strategy{
public void state();
}
abstract class Template1 implements Strategy{
int state = 1;
public void state(){
start();
System.out.println("template1 "+ state);
end();
}
abstract void start();
abstract void end();
}
class impl1 extends Template1{
void start() {
System.out.println("start"+ state);
}
void end() {
System.out.println("end"+ state);
}
}
abstract class Template2 implements Strategy{
int state = 2;
public void state(){
process();
System.out.println("template1 "+ state);
end();
}
abstract void process();
abstract void end();
}
class impl2 extends Template2{
void process() {
System.out.println("process" + state);
}
void end() {
System.out.println("end" + state);
}
}
public class AppTest {
public static void clientTest(Strategy strategy){
strategy.state();
}
public static void main(String[] args) {
Strategy imp1 = new impl1();
Strategy imp2 = new impl2();
clientTest(imp1);
clientTest(imp2);
}
}
Example 2
import java.util.LinkedList;
interface PaymentStrategy{ void pay(int amount); }
class CreditcardPayment implements PaymentStrategy{
public CreditcardPayment() {}
public void pay(int amount){ System.out.println("amount Paid by credit card"+ amount); }
}
class DebitcardPayment implements PaymentStrategy{
public DebitcardPayment() {}
public void pay(int amount){ System.out.println("amount Paid by debit card"+ amount); }
}
class Item{
int amount ;
String name;
public Item(int amount, String name) {
super();
this.amount = amount;
this.name = name;
}
public int getAmount() { return amount; }
public String getName() { return name; }
}
class shopingCart{
java.util.List<Item> item;
int sum = 0 ;
public shopingCart() {
this.item = new LinkedList<Item>();
}
public void additem(Item item){ this.item.add(item); }
public int getSum() {
for(Item i : item){
sum += i.getAmount();
}
return sum;
}
public void payment(PaymentStrategy strategy){
int amount = getSum();
strategy.pay(amount);
}
}
public class App2 {
static PaymentStrategy st1 = new CreditcardPayment();
public static void main(String[] args) {
shopingCart sc1 = new shopingCart();
sc1.additem(new Item(100, "tube"));
sc1.additem(new Item(32, "bulb"));
sc1.payment(st1);
shopingCart sc2 = new shopingCart();
sc2.additem(new Item(1221 , "s"));
sc2.payment(new DebitcardPayment());
}
}
interface PaymentStrategy{ void pay(int amount); }
class CreditcardPayment implements PaymentStrategy{
public CreditcardPayment() {}
public void pay(int amount){ System.out.println("amount Paid by credit card"+ amount); }
}
class DebitcardPayment implements PaymentStrategy{
public DebitcardPayment() {}
public void pay(int amount){ System.out.println("amount Paid by debit card"+ amount); }
}
class Item{
int amount ;
String name;
public Item(int amount, String name) {
super();
this.amount = amount;
this.name = name;
}
public int getAmount() { return amount; }
public String getName() { return name; }
}
class shopingCart{
java.util.List<Item> item;
int sum = 0 ;
public shopingCart() {
this.item = new LinkedList<Item>();
}
public void additem(Item item){ this.item.add(item); }
public int getSum() {
for(Item i : item){
sum += i.getAmount();
}
return sum;
}
public void payment(PaymentStrategy strategy){
int amount = getSum();
strategy.pay(amount);
}
}
public class App2 {
static PaymentStrategy st1 = new CreditcardPayment();
public static void main(String[] args) {
shopingCart sc1 = new shopingCart();
sc1.additem(new Item(100, "tube"));
sc1.additem(new Item(32, "bulb"));
sc1.payment(st1);
shopingCart sc2 = new shopingCart();
sc2.additem(new Item(1221 , "s"));
sc2.payment(new DebitcardPayment());
}
}
No comments:
Post a Comment