一、首先我们要了解到策略模式中它有四种角色:
1、抽象策略,也就是策略接口类或者策略抽象类。
2、具体策略,就是策略实现类。
3、策略执行者。
4、策略使用者。
二、接下来看看下面四个小案例:
1、对一组人的身高进行排序。
抽象策略
package com.liuyc.designpattern.strategy;
public class PersonHeightSortStrategy implements SortStrategy{
@Override
public int compareTo(Person t1, Person t2) {
if(t1.getHeight()>t2.getHeight()){
return 1;
}else {
return 0;
}
}
}
具体策略类
package com.liuyc.designpattern.strategy;
public class PersonHeightSortStrategy implements SortStrategy{
@Override
public int compareTo(Person t1, Person t2) {
if(t1.getHeight()>t2.getHeight()){
return 1;
}else {
return 0;
}
}
}
策略执行者
package com.liuyc.designpattern.strategy;
public class SortHelper {
public void sort(T [] t ,SortStrategy sortStrategy){
//选择排序法
//第一个循环,控制比较次数
for (int i = 0 ; i < t.length -1 ; i ++ ){
//第2个循环,从左往右找出最大的数字
int tempMaxIndex = i;
for (int j = i+1 ;j < t.length ; j ++){
tempMaxIndex = sortStrategy.compareTo(t[j],t[tempMaxIndex]) > 0 ? j:tempMaxIndex;
}
//每一次找出最大数字放到已排好序的数字最右边
T temp = t[i];
t[i] = t[tempMaxIndex];
t[tempMaxIndex] = temp;
}
}
}
策略使用者
Person person1 = new Person(100, 500);
Person person2 = new Person(200, 400);
Person person3 = new Person(300, 200);
Person [] peoples = new Person[]{person1,person2,person3};
SortHelper sortHelper2 = new SortHelper();
//2、使用按身高排序策略(从高到矮)
sortHelper2.sort(peoples,new PersonHeightSortStrategy());
for (int i = 0; i < peoples.length; i++) {
System.out.println(peoples[i]);
}
person类
package com.liuyc.designpattern.strategy;
public class Person {
/**
* 身高
*/
private int height;
/**
* 体重
*/
private int weight;
public Person(int height, int weight) {
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 "Person{" +
"height=" + height +
", weight=" + weight +
'}';
}
}
2、对一组人的体重进行排序,只需要提供具体策略即可。
具体策略类
package com.liuyc.designpattern.strategy;
public class PersonWeightSortStrategy implements SortStrategy{
@Override
public int compareTo(Person t1, Person t2) {
if(t1.getWeight()>t2.getWeight()){
return 1;
}else {
return 0;
}
}
}
策略使用者
Person person1 = new Person(100, 500);
Person person2 = new Person(200, 400);
Person person3 = new Person(300, 200);
Person [] peoples = new Person[]{person1,person2,person3};
//3、使用按体重排序策略(重到轻)
sortHelper2.sort(peoples,new PersonWeightSortStrategy());
for (int i = 0; i < peoples.length; i++) {
System.out.println(peoples[i]);
}
3、对一个double数据进行排序,从大到小,只需要提供具体策略即可。
具体策略类
package com.liuyc.designpattern.strategy;
public class DoubleArraySortStrategy implements SortStrategy{
@Override
public int compareTo(Double t1, Double t2) {
if(t1 > t2){
return 1;
}else{
return -1;
}
}
}
策略使用者
//4、给double数组排序
Double [] doubles = {31d,11d,25d,91d,43d,10d,5d};
SortHelper sortHelper3 = new SortHelper();
sortHelper3.sort(doubles,new DoubleArraySortStrategy());
for (int i = 0; i < doubles.length; i++) {
System.out.print(doubles[i] +" ");
}
有问题欢迎留言,谢谢!