策略模式
来源:互联网 发布:java 字符串随机排列 编辑:程序博客网 时间:2024/06/04 19:27
今天是2014年9月14号,记得笔者学习java已经将近一年了,只是觉得把java那些特别基础的学习了,今天想开此博客提高自己的java进阶知识,记录java coder的点点滴滴,以此鼓励自己
策略模式 ---strategy模式
今天看了一下马士兵老师的设计模式的视频-->策略模式,我发现他讲的特别的好,介绍了Comparator和Comparable的区别以及在日常的使用,并且让我深刻的懂得了策略模式
现在来一个策略模式的基本套路
第一步:定义一个IStrategy接口
public interface Comparator<T>{public int compare(T t1,T t2);
}
第二步:定义一个CatHeightComparator类,让他们进行高度的比较,如果以后想改变重量进行比较,则可以定义一个CatWeightComparator类,这个可以解耦
public class CatHeightComparator implements Comparator<Cat>{
@Override
public int compare(Cat t1, Cat t2) {
if(t1.getHeight()>t2.getHeight()) return 1;
else if(t1.getHeight()<t2.getHeight()) return -1;
return 0;
}
}
第三步:定义一个cat类,利用面向对象的多态将Comparator注入,让Cat类实现Comparable接口,这样cat对象就可以进行比较了,Comparator通常是使用在比较业务逻辑层上面的
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Cat implements Comparable<Cat>{
private int height;
private int weight;
private Comparator<Cat> comparator;
public int getHeight() {
return height;
}
public Cat() {
super();
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Comparator<Cat> getComparator() {
return comparator;
}
public void setComparator(Comparator<Cat> comparator) {
this.comparator = comparator;
}
@Override
public String toString() {
return "Cat [height=" + height + ", weight=" + weight + ", comparator="
+ comparator + "]";
}
public Cat(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
@Override
public int compareTo(Cat o) {
return comparator.compare(this, o);
}
}
第四步:定义一个Test类
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
public class Test1 {
@Test
public void fun1(){
List<Cat> list = new ArrayList<>();
list.add(new Cat(165,55));
list.add(new Cat(150,54));
list.add(new Cat(125,25));
list.add(new Cat(154,35));
System.out.println("排序前"+list);
System.out.println("排序后");
Collections.sort(list,new CatHeightComparator());
System.out.println(list);
}
}
结果如下:
排序前[Cat [height=165, weight=55, comparator=null], Cat [height=150, weight=54, comparator=null], Cat [height=125, weight=25, comparator=null], Cat [height=154, weight=35, comparator=null]]
排序后
[Cat [height=125, weight=25, comparator=null], Cat [height=150, weight=54, comparator=null], Cat [height=154, weight=35, comparator=null], Cat [height=165, weight=55, comparator=null]]
这就是我对于策略模式以及Comparator以及Comparable的理解,谢谢马士兵老师!
接下来是策略模式的基本套路
步骤一
public interface Strategy{
public void operate();
}
步骤二:定义所需的策略方案并实现Strategy接口
public class XXX1 implements Strategy{
//实现策略的代码
public void operate(){
.....//此处省略一千字....
}
}
public class XXX2 implements Strategy{
//实现策略的代码
public void operate(){
.....//此处省略一千字....
}
}
public class XXX3 implements Strategy{
//实现策略的代码
public void operate(){
.....//此处省略一千字....
}
}
第三步在要使用此略的类里面注入该Strategy接口,这就是多态的重要性
public class XXX {
private Strategy context;
public void setContext(Strategy context){
this.context=context;
}
public Strategy getContext(){
return Strategy ;
}
//使用策略时
public void operate(){
context.operate();
}
}
..............................................................................................................................................................谢谢..............................................................................................................................................
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- XZCVZXCVQWERQWERQWE
- 字符串加减运算
- 反射机制
- 关于 高斯算法计算某数可以被分割成连续自然数之和的组数 个人的一点拙见
- 编程之美:第一章 1.7光影切割问题
- 策略模式
- Search for a Range
- 排序算法比较
- 编程之美:第一章 1.8电梯调度算法
- 黑客星星
- 黑马入学_学习blog_第三篇
- #define EXTERN
- 如何学好C++语言
- ~这些年,我翻过的英文二三事儿~02~随着苹果进入健康应用时代,隐私问题将有怎样变数?~