您的位置:首页 > 编程语言 > Java开发

java设计模式之Strategy

2012-08-28 20:35 281 查看
Strategy
策略模式,就是将一个算法的不同实现封装成一个个单独的类,这些类实现同一个接口,使用者直接使用该接口来访问具体的算法。这个样子,使用者就可以使用不同的算法来实现业务逻辑了。

例如我想对两个对象进行比较我们首先会抽象出一个Comparable接口代码如下:

public interface Comparable {
public int compareTo(Object o);
}

接下来如果是要比较Cat类就要让它去实现这个接口代码如下:

public class Cat implements Comparable {
private int height;
//private Comparator comparator = new CatHeightComparator();
private Comparator comparator = new CatHeightComparator();
public Comparator getComparator() {
return comparator;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}

public Cat(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
private int weight;

@Override
public String toString() {
return height + "|" + weight;
}
@Override
public int compareTo(Object o) {
return comparator.compare(this, o);
}
}

之所以要持有一个Comparator 的引用是为了对Cat所要比较的部分进行扩展而抽象出来的一个Comparator 接口代码如下:

public interface Comparator {
int compare(Object o1, Object o2);
}

让CatHeightComparator去实现这个接口用来比较高度代码如下:

public class CatHeightComparator implements Comparator {
public int compare(Object o1, Object o2) {
Cat c1 = (Cat)o1;
Cat c2 = (Cat)o2;
if(c1.getHeight() > c2.getHeight()) return 1;
else if(c1.getHeight() < c2.getHeight()) return -1;
return 0;

}
}

让CatWeightComparator去实现这个接口用来比较重量代码如下:

public class CatWeightComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
Cat c1 = (Cat)o1;
Cat c2 = (Cat)o2;
if(c1.getWeight() > c2.getWeight()) return -1;
else if(c1.getHeight() < c2.getHeight()) return 1;
return 0;
}

}

这样DataSorter中的代码就可以这样组织了:

public class DataSorter {

public static void sort(Object[] a) {
for(int i=a.length; i>0; i--) {
for(int j=0; j<i-1; j++) {
Comparable o1 = (Comparable)a[j];
Comparable o2 = (Comparable)a[j+1];
if(o1.compareTo(o2) == 1) {
swap(a, j , j+1);
}
}
}
}

private static void swap(Object[] a, int x, int y) {
Object temp = a[x];
a[x] = a[y];
a[y] = temp;
}

public static void sort(int[] a) {
for(int i=a.length; i>0; i--) {
for(int j=0; j<i-1; j++) {
if(a[j] > a[j+1]) {
swap(a, j , j+1);
}
}
}
}

private static void swap(int[] a, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}

public static void p(int[] a) {
for(int i=0; i<a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}

public static void p(Object[] a) {
for(int i=0; i<a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}

}

最后测试代码如下:

public class Test {
public static void main(String[] args) {
//int[] a = {9, 5, 3, 7, 1};
Cat[] a = {new Cat(5, 5), new Cat(3, 3), new Cat(1, 1)};
//Dog[] a = {new Dog(5), new Dog(3), new Dog(1)};
//DataSorter.sort(a);
java.util.Arrays.sort(a);
DataSorter.p(a);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: