- Strategy: This is an interface to describe the individual algorithms.
public interface SortInterface {
public void sort(double[] list);
}SortInterface.java - ConcreteStrategy: Implements Strategy Interface and contains the logic for the algorithm.
public class QuickSort implements SortInterface {
public void sort(double[] a) {
quicksort(a, 0, a.length - 1);
}
private void quicksort(double[] a, int left, int right) {
if (right <= left) return;
int i = partition(a, left, right);
quicksort(a, left, i-1);
quicksort(a, i+1, right);
}
private int partition(double[] a, int left, int right) {
int i = left;
int j = right;
while (true) {
while (a[i]< a[right])
i++;
while (less(a[right], a[--j]))
if (j == left) break;
if (i >= j) break;
exch(a, i, j);
}
exch(a, i, right);
return i;
}
private boolean less(double x, double y) {
return (x < y);
}
private void exch(double[] a, int i, int j) {
double swap = a[i];
a[i] = a[j];
a[j] = swap;
}
}QuickSort.java public class BubbleSort implements SortInterface {
public void sort(double[] list) {
double temp;
for(int i = 0; i < list.length; i++) {
for(int j = 0; j < list.length - i; j++) {
if(list[i] < list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
}BubbleSort.java - Context: The context maintains a reference to a Strategy object and forwards client requests to the strategy. Context may also define an interface to let Strategies access context data.
public class SortingContext {
private SortInterface sorter = null;
public void sortDouble(double[] list) {
sorter.sort(list);
}
public SortInterface getSorter() {
return sorter;
}
public void setSorter(SortInterface sorter) {
this.sorter = sorter;
}
}SortingContext.java - Client: The client sets the concrete strategy in the context and invokes the context to run the algorithm. You can also have the context set the Concrete strategy implementation itself, based on the request.
public class SortingClient {
public class SortingClient {
public static void main(String[] args) {
double[] list = {1,2.4,7.9,3.2,1.2,0.2,10.2,22.5,19.6,14,12,16,17};
SortingContext context = new SortingContext();
context.setSorter(new BubbleSort());
context.sortDouble(list);
for(int i =0; i< list.length; i++) {
System.out.println(list[i]);
}
}
}SortingClient.java
Monday, December 11, 2006
Implementing Strategy Pattern in Java
The previous post described the Strategy pattern in brief. I listed out where and why the strategy pattern may be used. This post describes how to implement command pattern in Java and also some implementation considerations. This example here uses sorting algorithms. Two sorting algorithms (Bubble sort and Quick sort) are implemented and the client can select either the algorithms. Here is the UML diagram for the Strategy Pattern.The following is a simple description of each of the elements of the above diagram, followed by a simple implementation.
Labels:
example/sample code,
patterns
Subscribe to:
Post Comments (Atom)
Popular Posts
-
This post will describe how to create and deploy a Java Web Application war to Heroku using Heroku CLI. You will need a basic understanding ...
-
JUnit 4 introduces a completely different API to the older versions. JUnit 4 uses Java 5 annotations to describe tests instead of using in...
-
In a previous post, I described how to use Quartz scheduler for scheduling . In this post, I describe the configuration changes required for...
-
Recently I was attempting to deploy to weblogic from a Jenkins installed on a Red Hat Enterprise Linux Server release 7.3 , to a remote Webl...
-
New posts with iText 5.5.12 Following are two new posts for PDF Merge with iText 5.5.12 Merge PDF files using iText 5 Merge and Paginate PDF...
-
In this post we will see how to do an offline install Jenkins and required plugins on a Red Hat Enterprise Linux Server release 7.3. This is...
-
Displaytag is an opensource tag library that can be used to display tables on JSPs. Apart from being able to display tables, the displaytag...
-
I put up a new google co-op search box on top of the blog. I am trying to build a Java search service using Google co-op. I left the custom ...
-
The example here demonstrates the use of an anonymous PL/SQL block to return data to a calling Java program. It also shows how to use nested...
-
The previous post described the Command pattern in brief. I listed out where and why the command pattern may be used. This post describes h...
hi,
ReplyDeletethis program is very useful to me...
Hi,
ReplyDeleteGood one really helped me understanding the Strategy Pattern
Thank You.
Thanks for putting it together. I was really able to get this into my head.
ReplyDeleteYour descriptions and examples are really good.
ReplyDeleteEasy to understand. Helped to describe me Strategy pattern!
ReplyDeleteCould you post more pattern explanations and implementation examples?
ReplyDeleteYou are great!
hi,
ReplyDeleteShould we add defauld constructor
as : SortingContext(SortInterface sorter) to oblige to set strategies or set a defaut strategy thus to avoid null..sort(list); ?
Thanks. I used it. :)
ReplyDeletevery useful...thnx
ReplyDeleteThis is a great example! Could you elaborate a little more on this statement: "You can also have the context set the Concrete strategy implementation itself, based on the request."
ReplyDeleteThanks!
Abhi thanks Man...
ReplyDeleteOh`, thank you very much !
ReplyDeleteYou are excelent...
Thank, Again... !
phanleson@gmail.com
Security And Development in java
Beauty in Simplicity thats it .Real simple example does the trick again.Great ...
ReplyDeleteYeah thats great...now i have a little idean about the strategy pattern thank you so much
ReplyDeleteVery Informative this program.
ReplyDeleteThanks,
R. Mahendran
thx a lot, helped me to understand the strategy patter quite fast!
ReplyDeletethe return array is not sorted!why?
ReplyDeleteHi,the array is not sorted!could please tell me why?
ReplyDeleteI'm extremely impressed with your writing skills as well as with the layout on your blog.
ReplyDeleteHi! This is my first visit to your blog!
ReplyDeleteinformation to work on. You have done a outstanding job!
ReplyDeleteReally very informative and creative.
ReplyDeleteThanks for ones marvelous posting!
ReplyDeleteThe information you are providing that is really good.
ReplyDelete