SlideShare a Scribd company logo
1 of 24
Slide 1




Java Design Pattern
Strategy Pattern


                   Swapnal Agrawal
                   Module Lead

                   Ganesh Kolhe
                   Senior Team Lead
Slide 2




Outline
 Overview
 Analysis and Design
 Module and Client Implementation
 Strategy
 Variation in the Implementation
 Java API Usage
 Consequences
Slide 3




Overview
 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
  client's request and the required value to the client is returned.
Slide 4




Analysis 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.
Slide 5




Module Implementation
public 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, double
a , double b){
              if (type == COMPUTE_SUM){
                     return a + b;
              }else if (type == COMPUTE_PRODUCT){
                     return a * b;
              }
              throw new IllegalArgumentException();
       }
}
Slide 6




Client Implementation
public 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));
      }
}
Slide 7




The Design



             Easy Testing
              Easy Testing

                                        Code Reuse
                                         Code Reuse
                             Easy
                              Easy
                          Maintenance
                          Maintenance

                Easy
                 Easy
             Expansion
              Expansion
Slide 8




The Strategy
Design Pattern Type: Behavioral
Idea: Algorithm
Alias: Policy

A class defines many behaviors and these appear as multiple conditional
statements in its operations. Instead of many conditionals, move related
conditional branches into their own Strategy class.
Strategy pattern defines family of algorithms, encapsulates each one, and
makes 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).
Slide 9




Strategy 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.
Slide 10




The Class Diagram

    Client
     Client


                               <<interface>>
                                <<interface>>
                           IComputeStrategy
                             IComputeStrategy
                            -----------------------
                              -----------------------
                                 Operation()
                                  Operation()




              Concrete
               Concrete         Concrete
                                 Concrete                Concrete
                                                          Concrete
              Strategy1
               Strategy1        Strategy2
                                 Strategy2              Strategy…n
                                                         Strategy…n
Slide 11




Module Implementation By Using Strategy
public 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;
       }
}
Slide 12




Client Code Using the Implementation
public 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));
      }
}
Slide 13




The Strategy Design



            Easy Testing
             Easy Testing

                                       Code Reuse
                                        Code Reuse
                            Easy
                             Easy
                         Maintenance
                         Maintenance

               Easy
                Easy
            Expansion
             Expansion
Slide 14




Variation In The Implementation
 Singleton:

    Concrete classes as singleton objects.


    Define a static method to get a singleton instance.
Slide 15




Strategy Implementation By Using Singleton
//singleton implementation
public 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;
}
}
Slide 16




Client - Strategy Implementation By Using Singleton
public 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));
      }
}
Slide 17




Another Variation In the Implementation
Context:
   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 Context's Strategy, different behaviors can be
     obtained.
Slide 18




Strategy Implementation By Using Context
public interface IComputeStrategy03 {
        public double getValue(double a, double b);
}
//strategy context implementation
public class StrategyContext03 {
        private IComputeStrategy03 computeStrategy;
        public StrategyContext03 (IComputeStrategy03 computeStrategy){
                this.computeStrategy = computeStrategy;
        }
        public void setComputeStrategy(IComputeStrategy03
computeStrategy){
                this.computeStrategy = computeStrategy;
        }
        public double executeComputeStrategy(double a , double b){
                return computeStrategy.getValue(a, b);
        }
}
Slide 19




Client - Strategy Implementation By Using Context
public 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));
      }
}
Slide 20




The 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
Slide 21




Java API Usage

                                 checkInputStream and checkOutputStream uses the
Java.util.zip
                                 strategy pattern to compute checksums on byte stream.


                                 compare(), executed by among
Java.util.comparator
                                 others Collections#sort().


                                 the service() and all doXXX() methods take
                                 HttpServletRequest and HttpServletResponse and the
Javax.servlet.http.HttpServlet
                                 implementor has to process them (and not to get hold
                                 of them as instance variables!).
Slide 22




Consequences
 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.
Slide 23




About Cross Country Infotech
Cross 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 and
is well equipped with technical expertise to provide smarter solutions to its customers.
Some of our cutting-edge technology offerings include Mobile, Web and BI Application
Development; ECM and Informix 4GL Solutions; and Technical Documentation, UI
Design and Testing services.
Slide 24




Thank You!

More Related Content

Similar to Strategy design pattern

Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design PatternGanesh Kolhe
 
Guice tutorial
Guice tutorialGuice tutorial
Guice tutorialAnh Quân
 
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014First Tuesday Bergen
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsClare Macrae
 
Java programming concept
Java programming conceptJava programming concept
Java programming conceptSanjay Gunjal
 
Guide to Generate Extent Report in Kotlin
Guide to Generate Extent Report in KotlinGuide to Generate Extent Report in Kotlin
Guide to Generate Extent Report in KotlinRapidValue
 
Continuous Integration and Continuous Deployment (CI/CD) with WSO2 Enterprise...
Continuous Integration and Continuous Deployment (CI/CD) with WSO2 Enterprise...Continuous Integration and Continuous Deployment (CI/CD) with WSO2 Enterprise...
Continuous Integration and Continuous Deployment (CI/CD) with WSO2 Enterprise...WSO2
 
Exploring CameraX from JetPack
Exploring CameraX from JetPackExploring CameraX from JetPack
Exploring CameraX from JetPackHassan Abid
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesSami Mut
 
Unit Testing Using Mockito in Android (1).pdf
Unit Testing Using Mockito in Android (1).pdfUnit Testing Using Mockito in Android (1).pdf
Unit Testing Using Mockito in Android (1).pdfKaty Slemon
 
Introduction to Configurator 2.0 architecture design
Introduction to Configurator 2.0 architecture designIntroduction to Configurator 2.0 architecture design
Introduction to Configurator 2.0 architecture designXiaoyan Chen
 
The real beginner's guide to android testing
The real beginner's guide to android testingThe real beginner's guide to android testing
The real beginner's guide to android testingEric (Trung Dung) Nguyen
 
Creating sub zero dashboard plugin for apex with google
Creating sub zero dashboard plugin for apex with googleCreating sub zero dashboard plugin for apex with google
Creating sub zero dashboard plugin for apex with googleRoel Hartman
 
Open Source ERP Technologies for Java Developers
Open Source ERP Technologies for Java DevelopersOpen Source ERP Technologies for Java Developers
Open Source ERP Technologies for Java Developerscboecking
 
Unit Testing Frameworks: A comparative study
Unit Testing Frameworks: A comparative studyUnit Testing Frameworks: A comparative study
Unit Testing Frameworks: A comparative studyIRJET Journal
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationKevin Keraudren
 

Similar to Strategy design pattern (20)

Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design Pattern
 
Google GIN
Google GINGoogle GIN
Google GIN
 
Guice tutorial
Guice tutorialGuice tutorial
Guice tutorial
 
Dependency Injection for Android
Dependency Injection for AndroidDependency Injection for Android
Dependency Injection for Android
 
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop Applications
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
 
Guide to Generate Extent Report in Kotlin
Guide to Generate Extent Report in KotlinGuide to Generate Extent Report in Kotlin
Guide to Generate Extent Report in Kotlin
 
Continuous Integration and Continuous Deployment (CI/CD) with WSO2 Enterprise...
Continuous Integration and Continuous Deployment (CI/CD) with WSO2 Enterprise...Continuous Integration and Continuous Deployment (CI/CD) with WSO2 Enterprise...
Continuous Integration and Continuous Deployment (CI/CD) with WSO2 Enterprise...
 
Exploring CameraX from JetPack
Exploring CameraX from JetPackExploring CameraX from JetPack
Exploring CameraX from JetPack
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slides
 
Unit Testing Using Mockito in Android (1).pdf
Unit Testing Using Mockito in Android (1).pdfUnit Testing Using Mockito in Android (1).pdf
Unit Testing Using Mockito in Android (1).pdf
 
Introduction to Configurator 2.0 architecture design
Introduction to Configurator 2.0 architecture designIntroduction to Configurator 2.0 architecture design
Introduction to Configurator 2.0 architecture design
 
The real beginner's guide to android testing
The real beginner's guide to android testingThe real beginner's guide to android testing
The real beginner's guide to android testing
 
Creating sub zero dashboard plugin for apex with google
Creating sub zero dashboard plugin for apex with googleCreating sub zero dashboard plugin for apex with google
Creating sub zero dashboard plugin for apex with google
 
Open Source ERP Technologies for Java Developers
Open Source ERP Technologies for Java DevelopersOpen Source ERP Technologies for Java Developers
Open Source ERP Technologies for Java Developers
 
Unit Testing Frameworks: A comparative study
Unit Testing Frameworks: A comparative studyUnit Testing Frameworks: A comparative study
Unit Testing Frameworks: A comparative study
 
The MirAL Story
The MirAL StoryThe MirAL Story
The MirAL Story
 
Devoxx 2012 (v2)
Devoxx 2012 (v2)Devoxx 2012 (v2)
Devoxx 2012 (v2)
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimization
 

Recently uploaded

Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricksabhishekparmar618
 
Call Girls Aslali 7397865700 Ridhima Hire Me Full Night
Call Girls Aslali 7397865700 Ridhima Hire Me Full NightCall Girls Aslali 7397865700 Ridhima Hire Me Full Night
Call Girls Aslali 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
Mookuthi is an artisanal nose ornament brand based in Madras.
Mookuthi is an artisanal nose ornament brand based in Madras.Mookuthi is an artisanal nose ornament brand based in Madras.
Mookuthi is an artisanal nose ornament brand based in Madras.Mookuthi
 
Architecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfArchitecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfSumit Lathwal
 
昆士兰大学毕业证(UQ毕业证)#文凭成绩单#真实留信学历认证永久存档
昆士兰大学毕业证(UQ毕业证)#文凭成绩单#真实留信学历认证永久存档昆士兰大学毕业证(UQ毕业证)#文凭成绩单#真实留信学历认证永久存档
昆士兰大学毕业证(UQ毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
3D Printing And Designing Final Report.pdf
3D Printing And Designing Final Report.pdf3D Printing And Designing Final Report.pdf
3D Printing And Designing Final Report.pdfSwaraliBorhade
 
Passbook project document_april_21__.pdf
Passbook project document_april_21__.pdfPassbook project document_april_21__.pdf
Passbook project document_april_21__.pdfvaibhavkanaujia
 
原版美国亚利桑那州立大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
原版美国亚利桑那州立大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree原版美国亚利桑那州立大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
原版美国亚利桑那州立大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
FiveHypotheses_UIDMasterclass_18April2024.pdf
FiveHypotheses_UIDMasterclass_18April2024.pdfFiveHypotheses_UIDMasterclass_18April2024.pdf
FiveHypotheses_UIDMasterclass_18April2024.pdfShivakumar Viswanathan
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一z xss
 
西北大学毕业证学位证成绩单-怎么样办伪造
西北大学毕业证学位证成绩单-怎么样办伪造西北大学毕业证学位证成绩单-怎么样办伪造
西北大学毕业证学位证成绩单-怎么样办伪造kbdhl05e
 
Dubai Calls Girl Tapes O525547819 Real Tapes Escort Services Dubai
Dubai Calls Girl Tapes O525547819 Real Tapes Escort Services DubaiDubai Calls Girl Tapes O525547819 Real Tapes Escort Services Dubai
Dubai Calls Girl Tapes O525547819 Real Tapes Escort Services Dubaikojalkojal131
 
定制(RMIT毕业证书)澳洲墨尔本皇家理工大学毕业证成绩单原版一比一
定制(RMIT毕业证书)澳洲墨尔本皇家理工大学毕业证成绩单原版一比一定制(RMIT毕业证书)澳洲墨尔本皇家理工大学毕业证成绩单原版一比一
定制(RMIT毕业证书)澳洲墨尔本皇家理工大学毕业证成绩单原版一比一lvtagr7
 
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)jennyeacort
 
Call Girls Meghani Nagar 7397865700 Independent Call Girls
Call Girls Meghani Nagar 7397865700  Independent Call GirlsCall Girls Meghani Nagar 7397865700  Independent Call Girls
Call Girls Meghani Nagar 7397865700 Independent Call Girlsssuser7cb4ff
 
办理卡尔顿大学毕业证成绩单|购买加拿大文凭证书
办理卡尔顿大学毕业证成绩单|购买加拿大文凭证书办理卡尔顿大学毕业证成绩单|购买加拿大文凭证书
办理卡尔顿大学毕业证成绩单|购买加拿大文凭证书zdzoqco
 
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`dajasot375
 
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一Fi L
 
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一Fi L
 

Recently uploaded (20)

Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricks
 
Call Girls Aslali 7397865700 Ridhima Hire Me Full Night
Call Girls Aslali 7397865700 Ridhima Hire Me Full NightCall Girls Aslali 7397865700 Ridhima Hire Me Full Night
Call Girls Aslali 7397865700 Ridhima Hire Me Full Night
 
Mookuthi is an artisanal nose ornament brand based in Madras.
Mookuthi is an artisanal nose ornament brand based in Madras.Mookuthi is an artisanal nose ornament brand based in Madras.
Mookuthi is an artisanal nose ornament brand based in Madras.
 
Architecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfArchitecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdf
 
昆士兰大学毕业证(UQ毕业证)#文凭成绩单#真实留信学历认证永久存档
昆士兰大学毕业证(UQ毕业证)#文凭成绩单#真实留信学历认证永久存档昆士兰大学毕业证(UQ毕业证)#文凭成绩单#真实留信学历认证永久存档
昆士兰大学毕业证(UQ毕业证)#文凭成绩单#真实留信学历认证永久存档
 
3D Printing And Designing Final Report.pdf
3D Printing And Designing Final Report.pdf3D Printing And Designing Final Report.pdf
3D Printing And Designing Final Report.pdf
 
Passbook project document_april_21__.pdf
Passbook project document_april_21__.pdfPassbook project document_april_21__.pdf
Passbook project document_april_21__.pdf
 
原版美国亚利桑那州立大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
原版美国亚利桑那州立大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree原版美国亚利桑那州立大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
原版美国亚利桑那州立大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
FiveHypotheses_UIDMasterclass_18April2024.pdf
FiveHypotheses_UIDMasterclass_18April2024.pdfFiveHypotheses_UIDMasterclass_18April2024.pdf
FiveHypotheses_UIDMasterclass_18April2024.pdf
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
 
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
 
西北大学毕业证学位证成绩单-怎么样办伪造
西北大学毕业证学位证成绩单-怎么样办伪造西北大学毕业证学位证成绩单-怎么样办伪造
西北大学毕业证学位证成绩单-怎么样办伪造
 
Dubai Calls Girl Tapes O525547819 Real Tapes Escort Services Dubai
Dubai Calls Girl Tapes O525547819 Real Tapes Escort Services DubaiDubai Calls Girl Tapes O525547819 Real Tapes Escort Services Dubai
Dubai Calls Girl Tapes O525547819 Real Tapes Escort Services Dubai
 
定制(RMIT毕业证书)澳洲墨尔本皇家理工大学毕业证成绩单原版一比一
定制(RMIT毕业证书)澳洲墨尔本皇家理工大学毕业证成绩单原版一比一定制(RMIT毕业证书)澳洲墨尔本皇家理工大学毕业证成绩单原版一比一
定制(RMIT毕业证书)澳洲墨尔本皇家理工大学毕业证成绩单原版一比一
 
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
 
Call Girls Meghani Nagar 7397865700 Independent Call Girls
Call Girls Meghani Nagar 7397865700  Independent Call GirlsCall Girls Meghani Nagar 7397865700  Independent Call Girls
Call Girls Meghani Nagar 7397865700 Independent Call Girls
 
办理卡尔顿大学毕业证成绩单|购买加拿大文凭证书
办理卡尔顿大学毕业证成绩单|购买加拿大文凭证书办理卡尔顿大学毕业证成绩单|购买加拿大文凭证书
办理卡尔顿大学毕业证成绩单|购买加拿大文凭证书
 
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
 
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
 
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
 

Strategy design pattern

  • 1. Slide 1 Java Design Pattern Strategy Pattern Swapnal Agrawal Module Lead Ganesh Kolhe Senior Team Lead
  • 2. Slide 2 Outline  Overview  Analysis and Design  Module and Client Implementation  Strategy  Variation in the Implementation  Java API Usage  Consequences
  • 3. Slide 3 Overview  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 client's request and the required value to the client is returned.
  • 4. Slide 4 Analysis 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 5 Module Implementation public 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, double a , double b){ if (type == COMPUTE_SUM){ return a + b; }else if (type == COMPUTE_PRODUCT){ return a * b; } throw new IllegalArgumentException(); } }
  • 6. Slide 6 Client Implementation public 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 7 The Design Easy Testing Easy Testing Code Reuse Code Reuse Easy Easy Maintenance Maintenance Easy Easy Expansion Expansion
  • 8. Slide 8 The Strategy Design Pattern Type: Behavioral Idea: Algorithm Alias: Policy A class defines many behaviors and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class. Strategy pattern defines family of algorithms, encapsulates each one, and makes 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 9 Strategy 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 10 The 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 11 Module Implementation By Using Strategy public 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 12 Client Code Using the Implementation public 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 13 The Strategy Design Easy Testing Easy Testing Code Reuse Code Reuse Easy Easy Maintenance Maintenance Easy Easy Expansion Expansion
  • 14. Slide 14 Variation In The Implementation  Singleton:  Concrete classes as singleton objects.  Define a static method to get a singleton instance.
  • 15. Slide 15 Strategy Implementation By Using Singleton //singleton implementation public 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 16 Client - Strategy Implementation By Using Singleton public 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 17 Another Variation In the Implementation Context:  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 Context's Strategy, different behaviors can be obtained.
  • 18. Slide 18 Strategy Implementation By Using Context public interface IComputeStrategy03 { public double getValue(double a, double b); } //strategy context implementation public class StrategyContext03 { private IComputeStrategy03 computeStrategy; public StrategyContext03 (IComputeStrategy03 computeStrategy){ this.computeStrategy = computeStrategy; } public void setComputeStrategy(IComputeStrategy03 computeStrategy){ this.computeStrategy = computeStrategy; } public double executeComputeStrategy(double a , double b){ return computeStrategy.getValue(a, b); } }
  • 19. Slide 19 Client - Strategy Implementation By Using Context public 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 20 The 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 21 Java API Usage checkInputStream and checkOutputStream uses the Java.util.zip strategy pattern to compute checksums on byte stream. compare(), executed by among Java.util.comparator others Collections#sort(). the service() and all doXXX() methods take HttpServletRequest and HttpServletResponse and the Javax.servlet.http.HttpServlet implementor has to process them (and not to get hold of them as instance variables!).
  • 22. Slide 22 Consequences  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 23 About Cross Country Infotech Cross 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 and is well equipped with technical expertise to provide smarter solutions to its customers. Some of our cutting-edge technology offerings include Mobile, Web and BI Application Development; ECM and Informix 4GL Solutions; and Technical Documentation, UI Design and Testing services.

Editor's Notes

  1. Project Name
  2. Project Name
  3. Project Name