Your SlideShare is downloading. ×
Strategy design pattern
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Strategy design pattern

153
views

Published on

Published in: Design

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
153
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Project Name
  • Project Name
  • Project Name
  • Transcript

    • 1. Slide 1Java Design PatternStrategy Pattern Swapnal Agrawal Module Lead Ganesh Kolhe Senior Team Lead
    • 2. Slide 2Outline Overview Analysis and Design Module and Client Implementation Strategy Variation in the Implementation Java API Usage Consequences
    • 3. Slide 3Overview Using different algorithms on some values such as sorting, filters, etc. Applying some logic or calculation to return a value to the client. Implementing the algorithms in such a way that the correct algorithm is provided from different available algorithms on the clients request and the required value to the client is returned.
    • 4. Slide 4Analysis and Design There are many solutions and alternatives which seem to be very simple and straightforward. Following are two such examples:  Separate implementation logic from client code.  Create a static class with a method and if-else or switch case ladder can be used to fetch different algorithms.
    • 5. Slide 5Module Implementationpublic final class Compute { public static final int COMPUTE_SUM= 1; public static final int COMPUTE_PRODUCT= 2; private Compute() {} public static double getComputedValue(int type, doublea , double b){ if (type == COMPUTE_SUM){ return a + b; }else if (type == COMPUTE_PRODUCT){ return a * b; } throw new IllegalArgumentException(); }}
    • 6. Slide 6Client Implementationpublic class ApplicationClient { public static void main(String[] args) { System.out.println("Sum = "+ Compute.getComputedValue( Compute.COMPUTE_SUM, 20, 25)); System.out.println("Product = "+ Compute.getComputedValue( Compute.COMPUTE_PRODUCT, 7, 3)); }}
    • 7. Slide 7The Design Easy Testing Easy Testing Code Reuse Code Reuse Easy Easy Maintenance Maintenance Easy Easy Expansion Expansion
    • 8. Slide 8The StrategyDesign Pattern Type: BehavioralIdea: AlgorithmAlias: PolicyA class defines many behaviors and these appear as multiple conditionalstatements in its operations. Instead of many conditionals, move relatedconditional branches into their own Strategy class.Strategy pattern defines family of algorithms, encapsulates each one, andmakes them interchangeable. Strategy lets algorithms vary independently from clients that use them. Calculations are based on the clients’ abstraction (not using the clients’ implementation or global data).
    • 9. Slide 9Strategy Implementation Define a Strategy Interface that is common to all supported algorithms. Strategy Interface defines your Strategy Object’s behavior. Implement the Concrete Strategy classes that share the common Strategy interface.
    • 10. Slide 10The Class Diagram Client Client <<interface>> <<interface>> IComputeStrategy IComputeStrategy ----------------------- ----------------------- Operation() Operation() Concrete Concrete Concrete Concrete Concrete Concrete Strategy1 Strategy1 Strategy2 Strategy2 Strategy…n Strategy…n
    • 11. Slide 11Module Implementation By Using Strategypublic interface IComputeStrategy01 { public double getValue(double a, double b);}public class ComputeSum01 implements IComputeStrategy01 { public double getValue(double a, double b) { return a + b; }}public class ComputeProduct01 implements IComputeStrategy01 { public double getValue(double a, double b) { return a * b; }}
    • 12. Slide 12Client Code Using the Implementationpublic class StrategyApplicationClient01 { public static void main(String[] args) { IComputeStrategy01 compute1 = new ComputeSum01(); System.out.println("Sum = "+ compute1.getValue(20, 25)); IComputeStrategy01 compute2 = new ComputeProduct02(); System.out.println("Product = "+ compute2.getValue(7, 3)); }}
    • 13. Slide 13The Strategy Design Easy Testing Easy Testing Code Reuse Code Reuse Easy Easy Maintenance Maintenance Easy Easy Expansion Expansion
    • 14. Slide 14Variation In The Implementation Singleton:  Concrete classes as singleton objects.  Define a static method to get a singleton instance.
    • 15. Slide 15Strategy Implementation By Using Singleton//singleton implementationpublic final class ComputeSum02 implements IComputeStrategy02{private static ComputeSum02 computeSum = new ComputeSum02();private ComputeSum02(){}public static IComputeStrategy02 getOnlyOneInstance(){return computeSum;}public double getValue(double a, double b) {return a + b;}}
    • 16. Slide 16Client - Strategy Implementation By Using Singletonpublic class StrategyApplicationClient02 { public static void main(String[] args) { IComputeStrategy02 compute1 = ComputeSum02.getOnlyOneInstance(); System.out.println("Sum = "+ compute1.getValue(20, 25)); IComputeStrategy02 compute2 = ComputeProduct02.getOnlyOneInstance(); System.out.println(“Product= "+ compute2.getValue(7, 3)); }}
    • 17. Slide 17Another Variation In the ImplementationContext:  is configured with a Concrete Strategy object.  maintains a private reference to a Strategy object.  may define an interface that lets Strategy access its data. By changing the Contexts Strategy, different behaviors can be obtained.
    • 18. Slide 18Strategy Implementation By Using Contextpublic interface IComputeStrategy03 { public double getValue(double a, double b);}//strategy context implementationpublic class StrategyContext03 { private IComputeStrategy03 computeStrategy; public StrategyContext03 (IComputeStrategy03 computeStrategy){ this.computeStrategy = computeStrategy; } public void setComputeStrategy(IComputeStrategy03computeStrategy){ this.computeStrategy = computeStrategy; } public double executeComputeStrategy(double a , double b){ return computeStrategy.getValue(a, b); }}
    • 19. Slide 19Client - Strategy Implementation By Using Contextpublic class StrategyApplicationClient03 { public static void main(String[] args) { StrategyContext03 ctx = new StrategyContext03( ComputeSum03.getOnlyOneInstance()); System.out.println("Sum = "+ ctx.executeComputeStrategy(20, 25)); ctx.setComputeStratey( ComputeProduct03.getOnlyOneInstance()); System.out.println("Product = "+ ctx.executeComputeStrategy(7, 3)); }}
    • 20. Slide 20The Class Diagram Client Client Context Context <<interface>> <<interface>> IComputeStrategy IComputeStrategy ----------------------- ----------------------- Operation() Operation() Concrete Concrete Concrete Concrete Concrete Concrete Strategy1 Strategy1 Strategy2 Strategy2 Strategy…n Strategy…n
    • 21. Slide 21Java API Usage checkInputStream and checkOutputStream uses theJava.util.zip strategy pattern to compute checksums on byte stream. compare(), executed by amongJava.util.comparator others Collections#sort(). the service() and all doXXX() methods take HttpServletRequest and HttpServletResponse and theJavax.servlet.http.HttpServlet implementor has to process them (and not to get hold of them as instance variables!).
    • 22. Slide 22Consequences Benefits  Provides an alternative to sub classing the Context class to get a variety of algorithms or behaviors.  Eliminates large conditional statements.  Provides a choice of implementations for the same behavior. Shortcomings  Increases the number of objects.  All algorithms must use the same Strategy interface. Think twice before implementing the Strategy pattern or any other design pattern to match your requirements.
    • 23. Slide 23About Cross Country InfotechCross Country Infotech (CCI) Pvt. Ltd. is a part of the Cross Country Healthcare (NYSE:CCRN) group of companies. CCI specializes in providing a gamut of IT/ITES services andis well equipped with technical expertise to provide smarter solutions to its customers.Some of our cutting-edge technology offerings include Mobile, Web and BI ApplicationDevelopment; ECM and Informix 4GL Solutions; and Technical Documentation, UIDesign and Testing services.
    • 24. Slide 24Thank You!