[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

stategy design pattern in java

hi,
i have started learning design pattern in java.i m using "head first design patterns".can u give me 1/2 problem and there step by step solution using strategy design pattern.den i will hav a clear concept.
thnks in advance
0
mithunda5011
Asked:
mithunda5011
  • 3
  • 2
2 Solutions
 
basav_comCommented:
0
 
basav_comCommented:
0
 
mithunda5011Author Commented:
thnks,i hv not started studying it yet,but is it using php?will it be helpful for java too?thank you
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
basav_comCommented:
Design patterns are language independent. The explanation is good that's why i have provided the php link.

Check this link for java example:
http://en.wikipedia.org/wiki/Strategy_pattern

By the head first book is really good. Refer the above content after going thru the book :).
0
 
mithunda5011Author Commented:
ok....thank u...pretty impressive....but lets c if any one else leaves any better comment or not..thanks
0
 
Jim CakalicSenior Developer/ArchitectCommented:
The Strategy pattern can be used anytime you want to externally influence the algorithm used by another class/object. You have a family of algorithms and want to tell another object which one to use when it fulfills its responsibility for you.

In this rather trivial contrived example, the StringPrinter class exists to print a List of Strings on the console. Before printing, it will sort the Strings based on a previously specified Comparator. Comparator here is the Strategy. There are multiple instances (different classes) that have been defined to sort the Strings according to their natural order, case insensitive order, and reversed order. Ultimately, the StringPrinter class doesn't care which one is in use. It just knows that it has a responsibility to sort the List<String> it is given and then print it. The client that calls StringPrinter gets to select which sorting strategy is used by instantiating the appropriate Comparator.

This is the whole point of Strategy: define a family of algorithms and select the algorithm to use at run time. The following code demonstrates:

public class StrategyExample {
    public static void main(String[] args) {
        List<String> sargs = (List<String>) Arrays.asList(args);
        new StringPrinter(new NaturalStringComparator()).print(sargs);
        new StringPrinter(new CaseInsensitiveStringComparator()).print(sargs);
        new StringPrinter(new ReverseStringComparator()).print(sargs);
    }
}
 
class StringPrinter {
    private Comparator<String> comparator;
 
    public StringPrinter(Comparator<String> comparator) {
        this.comparator = comparator;
    }
 
    public void print(List<String> strings) {
        Collections.sort(strings, comparator);
        for (String s : strings) {
            System.out.println(s);
        }
    }
}
 
class NaturalStringComparator implements Comparator<String> {
    public int compare(String s1, String s2) {
        return s1.compareTo(s2);
    }
}
 
class CaseInsensitiveStringComparator implements Comparator<String> {
    public int compare(String s1, String s2) {
        return s1.compareToIgnoreCase(s2);
    }
}
 
class ReverseStringComparator implements Comparator<String> {
    public int compare(String s1, String s2) {
        String r1 = new StringBuilder(s1).reverse().toString();
        String r2 = new StringBuilder(s1).reverse().toString();
        return r1.compareTo(r2);
    }
}

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now