二十三種設計模式分類
一、概述
策略(Strategy
)模式的定義:該模式定義了一系列算法,並將每個算法封裝起來,使它們可以相互替換,且算法的變化不會影響使用算法的客戶。策略模式屬於對象行爲模式,它通過對算法進行封裝,把使用算法的責任和算法的實現分割開來,並委派給不同的對象對這些算法進行管理。
主要意圖:定義一系列的算法,把它們一個個封裝起來, 並且使它們可相互替換。
主要解決:在有多種算法相似的情況下,使用if...else
所帶來的複雜和難以維護。
優點
- 多重條件語句不易維護,而使用策略模式可以避免使用多重條件語句。
- 策略模式提供了一系列的可供重用的算法族,恰當使用繼承可以把算法族的公共代碼轉移到父類裏面,從而避免重複的代碼。
- 策略模式可以提供相同行爲的不同實現,客戶可以根據不同時間或空間要求選擇不同的。
- 策略模式提供了對開閉原則的完美支持,可以在不修改原代碼的情況下,靈活增加新算法。
- 策略模式把算法的使用放到環境類中,而算法的實現移到具體策略類中,實現了二者的分離。
缺點
- 客戶端必須理解所有策略算法的區別,以便適時選擇恰當的算法類。
- 策略模式造成很多的策略類
場景
1、如果在一個系統裏面有許多類,它們之間的區別僅在於它們的行爲,那麼使用策略模式可以動態地讓一個對象在許多行爲中選擇一種行爲。
2、一個系統需要動態地在幾種算法中選擇一種。
3、如果一個對象有很多的行爲,如果不用恰當的模式,這些行爲就只好使用多重的條件選擇語句來實現。
二、實現
1. 結構圖
策略模式的主要角色如下。
- 抽象策略(
Strategy
)類:定義了一個公共接口,各種不同的算法以不同的方式實現這個接口,環境角色使用這個接口調用不同的算法,一般使用接口或抽象類實現。 - 具體策略(
Concrete Strategy
)類:實現了抽象策略定義的接口,提供具體的算法實現。 - 環境(
Context
)類:持有一個策略類的引用,最終給客戶端調用。
PS
:UML
結構圖可以參考,例子實現並不根據UML
圖來完成,靈活實現即可;
2. 實現
- 策略接口
package cn.missbe.model.strategy;
/**
* Copyright (c) 2020.
* Email: [email protected]
* @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: [email protected]
* @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: [email protected]
*
* @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: [email protected]
* @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));
}
}