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

策略模式

2015-10-02 11:58 246 查看
意图:定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。

主要解决:在有多种算法相似的情况下,使用if..else所带来的复杂和难以维护

什么时候使用:一个系统有许多许多类,而区分他们的只是他们直接的行为

如何解决 :将这些算法封装成一个一个的类,任意的替换

使用场景1、如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,
那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为 
2、一个系统需要动态地在几种算法中选择一种
   
3、如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现

Comparable
package com.bjsxt.dp.strategy;

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

package com.bjsxt.dp.strategy;

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 p(Object[] a) {
for(int i=0; i<a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}

package com.bjsxt.dp.strategy;

public class Dog implements Comparable{
public Dog(int food) {
super();
this.food = food;
}
private int food;

public int getFood() {
return food;
}

@Override
public String toString() {
return this.food + "";
}

public void setFood(int food) {
this.food = food;
}

@Override
public int compareTo(Object o) {
if(o instanceof Dog){
Dog d =  (Dog)o;
if(this.getFood()>d.getFood()) return 1;
else if(this.getFood()<d.getFood()) return -1;
else return 0;
}
return -100;
}

}


Comparator
package com.bjsxt.dp.strategy;

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

package com.bjsxt.dp.strategy;

public class Cat implements Comparable{
private Comparator comparator = new CatHeightComparator();
private int height;
private int weight;

public Comparator getComparator() {
return comparator;
}

public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public Cat(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

@Override
public String toString() {
return height + "|" + weight;
}

@Override
public int compareTo(Object o) {

return comparator.compare(this, o);
}

}

package com.bjsxt.dp.strategy;

public class CatHeightComparator implements Comparator {

@Override
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;
}

}

package com.bjsxt.dp.strategy;

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.getWeight() < c2.getWeight()) return 1;
return 0;
}

}


Test
package com.bjsxt.dp.strategy;

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