Java设计模式

Java实现23种设计模式(十七):策略模式(重点掌握)

2020-06-11  本文已影响0人  依然慢节奏

二十三种设计模式分类

设计模式三大分类.jpg

一、概述

策略(Strategy)模式的定义:该模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换,且算法的变化不会影响使用算法的客户。策略模式属于对象行为模式,它通过对算法进行封装,把使用算法的责任和算法的实现分割开来,并委派给不同的对象对这些算法进行管理。

主要意图:定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。

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

优点

缺点

场景

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


二、实现

1. 结构图

策略模式的主要角色如下。

策略模式

PS:UML结构图可以参考,例子实现并不根据UML图来完成,灵活实现即可;

2. 实现

package cn.missbe.model.strategy;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 * @author lyg  2020/4/22 下午4:26
 * description:
 * 比较策略接口,依赖接口编程
 **/
@FunctionalInterface
public interface Comparator<T> {
    /**
     * 自己定义的比较策略接口
     * @param o1 对象o1
     * @param o2 对象o2
     * @return 比较结果
     */
    int comparator(T o1,T o2);
}
package cn.missbe.model.strategy;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 * @author lyg  2020/4/22 下午4:28
 * description:
 **/

public class CatStrategy implements Comparator<Cat> {
    @Override
    public int comparator(Cat o1, Cat o2) {
        if (o1.weight > o2.weight) {
            return 1;
        } else if (o1.weight < o2.weight) {
            return  -1;
        }
        return 0;
    }
}
package cn.missbe.model.strategy;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 *
 * @author lyg  2020/4/22 下午4:28
 * description:
 **/

public class Cat {
    double weight;
    double height;

    public Cat(double weight, double height) {
        this.weight = weight;
        this.height = height;
    }

    @Override
    public String toString() {
        return "Cat{" + "weight=" + weight + ", height=" + height + '}';
    }
}
package cn.missbe.model.strategy;

import java.util.Arrays;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 * @author lyg  2020/4/22 下午4:30
 * description:
 * 策略模式
 **/

public class SortMain<T> {
    public void sort(T[] array, Comparator<T> comparator) {
        int minIndex;
        for (int i = 0; i < array.length; i++) {
            minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                minIndex = comparator.comparator(array[minIndex],array[j]) > 0 ? minIndex : j;
            }
            if (minIndex != i) {
                swap(array, minIndex, i);
            }
        }
    }

    private void swap(T[] array, int minIndex, int i) {
        T temp = array[minIndex];
        array[minIndex] = array[i];
        array[i] = temp;
    }

    public static void main(String[] args) {
        Cat[] cats = {new Cat(1.2, 3.3), new Cat(1.3, 2.3), new Cat(1.4, 1.3), new Cat(1.5, 0.3)};
        SortMain<Cat> catSortMain = new SortMain<>();
        ///根据体重排序
        catSortMain.sort(cats, new CatStrategy());
        System.out.println(Arrays.toString(cats));
        ///对修改关闭,对扩展开放,灵活更换比较策略,根据身高排序
        catSortMain.sort(cats,(o1,o2)->{
            if (o1.height > o2.height) {
                return 1;
            } else if (o1.height < o2.height) {
                return -1;
            }
            return 0;
        });
        System.out.println(Arrays.toString(cats));
    }
}
上一篇下一篇

猜你喜欢

热点阅读