设计模式-策略模式(行为型)

行为型-策略模式

了解策略模式

策略模式是一种行为型设计模式,在策略模式中定义了一系列算法或者策略,并将这些策略封装到独立的类中,使得可以相互替换。在使用时,可以指定响应的策略使用。

角色

  1. 策略接口:对于某种行为的抽象,具体的行为由实现类决定;
  2. 具体策略类:实现策略接口,具体化其中的策略行为;
  3. 上下文类:次类中包含策略对象的引用,并通过引用调用策略实现类;

图解

实战

比较策略,可以根据不同的策略进行排序;

实体类:

java 复制代码
/** 动物类*/
public class Animal {
    private int height;
    private int weight;
    ......
}
/** 人类*/
public class Person {
    private int weight;
    private double height;
    private BigDecimal money;
        ......
}

策略接口:

java 复制代码
/** 自定义比较的策略接口*/
public interface Comparator<T> {
    int compareTo(T o1,T o2);
}

策略的实现类:

java 复制代码
/** 按动物的重量比较动物*/
public class AnimalComparatorByHeight implements Comparator<Animal> {
    @Override
    public int compareTo(Animal o1, Animal o2) {
        if(o1.getHeight()<o2.getHeight()){
            return -1;
        }else if (o1.getHeight() > o2.getHeight()){
            return 1;
        }else {
            return 0;
        }
    }
}
/** 按人的钱数量比较人*/
public class PersonComparatorByMoney implements Comparator<Person> {
    @Override
    public int compareTo(Person o1, Person o2) {
        if(o1.getMoney().compareTo(o2.getMoney()) < 0){
            return -1;
        }else if(o1.getMoney().compareTo(o2.getMoney()) > 0){
            return 1;
        }else{
            return 0;
        }
    }
}
/** ......
 * 还可一定义其它的排序策略,只需要实现策略接口
 */

context类:

java 复制代码
/** 排序类,可根据不同的策略排序不同的对象*/
public class Sorter<T> {
    private Comparator<T> comparator;
    private T [] arr;

    public Sorter() {
    }

    public Sorter(Comparator<T> comparator, T[] arr) {
        this.comparator = comparator;
        this.arr = arr;
    }

    /**
     * 冒泡排序
     */
    public void sort(){
        for (int i = 0; i < arr.length -1; i++) {
            for (int j = 0; j < arr.length -1 -i; j++) {
                if(comparator.compareTo(arr[j], arr[j+1]) > 0){
                    change(j,j+1);
                }
            }
        }
    }
    public void change(Integer a, Integer b){
        T temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}

使用:

java 复制代码
public class Test {
    public static void main(String[] args) {
        /**
         *  根据动物重量排序
         */
        Animal[] animals = new Animal[]{
                new Animal(3,3),
                new Animal(5,5),
                new Animal(1,1),
                new Animal(4,4)
        };
        Sorter<Animal> animalSorter = new Sorter<>(new AnimalComparatorByHeight(),animals);
        animalSorter.sort();
        System.out.println(Arrays.toString(animals));

        /**
         * 根据人钱数量排序
         */
        Person[] person = new Person[]{
                new Person(3,3,new BigDecimal(900)),
                new Person(5,5,new BigDecimal(300)),
                new Person(1,1,new BigDecimal(500)),
                new Person(4,4,new BigDecimal(600))
        };
        Sorter<Person> personSorter = new Sorter<>(new PersonComparatorByMoney(),person);
        personSorter.sort();
        System.out.println(Arrays.toString(person));
    }
}

总结

使用策略模式定义行为的抽象,行为的具体方式由实现类实现;如果再添加其他行为的时候只需要增加策略接口的实现类,而不需要修改现有的代码,提高了代码的拓展性能,同时保证的对拓展开放对修改关闭的开闭原则;

相关推荐
刷帅耍帅22 分钟前
设计模式-桥接模式
设计模式·桥接模式
MinBadGuy1 小时前
【GeekBand】C++设计模式笔记5_Observer_观察者模式
c++·设计模式
刷帅耍帅2 小时前
设计模式-生成器模式/建造者模式Builder
设计模式·建造者模式
蜡笔小新..1 天前
【设计模式】软件设计原则——开闭原则&里氏替换&单一职责
java·设计模式·开闭原则·单一职责原则
性感博主在线瞎搞1 天前
【面向对象】设计模式概念和分类
设计模式·面向对象·中级软件设计师·设计方法
lucifer3111 天前
JavaScript 中的组合模式(十)
javascript·设计模式
lucifer3111 天前
JavaScript 中的装饰器模式(十一)
javascript·设计模式
蜡笔小新..1 天前
【设计模式】软件设计原则——依赖倒置&合成复用
设计模式·依赖倒置原则·合成复用原则
刷帅耍帅1 天前
设计模式-代理模式
设计模式·代理模式
神的孩子都在歌唱1 天前
行为设计模式 -观察者模式- JAVA
java·观察者模式·设计模式