1、策略模式
1.1、概述
策略模式是一種行為設計模式,它允許在運行時選擇算法的行為。它將算法封裝在獨立的策略類中,使得它們可以相互替換,而不影響客戶端代碼。這種模式通過將算法的選擇從客戶端代碼中分離出來,提供了更大的靈活性和可維護性。
在Java中,策略模式的設計理念可以通過以下步驟實現(xiàn):
- 定義一個策略接口(或抽象類),該接口定義了所有具體策略類都必須實現(xiàn)的方法。
- 創(chuàng)建具體的策略類,實現(xiàn)策略接口,并提供具體的算法實現(xiàn)。
- 在客戶端代碼中,創(chuàng)建一個策略對象,并將其傳遞給需要使用算法的對象。
- 客戶端對象使用策略對象來執(zhí)行特定的算法。
在你提供的代碼片段中,我無法確定與策略模式相關(guān)的代碼。如果你有更多的上下文或示例代碼,我可以更好地幫助你理解和應用策略模式。
1.2、優(yōu)缺點
策略模式具有以下優(yōu)點:
- 可以代替if-else
-
算法的獨立性:策略模式將算法封裝在獨立的策略類中,使得算法可以獨立于客戶端代碼進行修改和擴展。這樣可以提高代碼的靈活性和可維護性。
-
可替換性:由于策略模式將算法封裝在不同的策略類中,因此可以在運行時動態(tài)地切換和替換算法。這樣可以根據(jù)不同的需求選擇最合適的算法,而無需修改客戶端代碼。
-
單一職責原則:策略模式將不同的算法封裝在不同的策略類中,使得每個策略類只負責一個具體的算法。這樣符合單一職責原則,提高了代碼的可讀性和可維護性。
-
擴展性:由于策略模式將算法封裝在獨立的策略類中,因此可以很容易地添加新的策略類來擴展系統(tǒng)的功能。
策略模式也有一些缺點:
-
增加了類的數(shù)量:使用策略模式會增加系統(tǒng)中的類的數(shù)量,因為每個具體的算法都需要一個對應的策略類。這可能會增加代碼的復雜性和理解難度。
-
客戶端必須了解所有的策略類:客戶端必須了解所有可用的策略類,并在運行時選擇合適的策略。這可能會增加客戶端代碼的復雜性。
-
策略切換的開銷:在運行時切換策略可能會帶來一定的開銷,特別是在需要頻繁切換策略的情況下。這可能會影響系統(tǒng)的性能。
綜上所述,策略模式在提供靈活性、可維護性和可擴展性方面具有很多優(yōu)點,但也需要權(quán)衡其增加的類數(shù)量和策略切換的開銷。在設計和使用策略模式時,需要根據(jù)具體的需求和情況進行權(quán)衡和選擇。
2、SpringBean方式實現(xiàn)
- bean的名字(默認):實現(xiàn)策略類的名字首字母小寫
2.1、實現(xiàn)步奏
- 可以看到,去獲取bean是需要用戶自己去做的。
2.2、實現(xiàn)
①定義策略接口
package com.cc.eed.strategy;
/**
* <p>基于SpringBean的策略模式</p>
*
* @author CC
* @since 2023/10/13
*/
public interface ISpringBeanStrategy {
/**
* 吃飯
*/
String eating();
/**
* 玩
*/
String play();
}
②定義實現(xiàn)類1
package com.cc.eed.strategy.impl;
import com.cc.eed.strategy.ISpringBeanStrategy;
import org.springframework.stereotype.Component;
/**
* <p>小美</p>
*
* @author CC
* @since 2023/10/13
*/
@Component
public class MeiSpringBeanImpl implements ISpringBeanStrategy {
/**
* 吃飯
*/
@Override
public String eating() {
return "小美,吃飯!";
}
/**
* 玩
*/
@Override
public String play() {
return "小美,玩!";
}
}
定義實現(xiàn)類2
package com.cc.eed.strategy.impl;
import com.cc.eed.strategy.ISpringBeanStrategy;
import org.springframework.stereotype.Component;
/**
* <p>小明</p>
*
* @author CC
* @since 2023/10/13
*/
@Component
public class MingSpringBeanImpl implements ISpringBeanStrategy {
/**
* 吃飯
*/
@Override
public String eating() {
return "小明,吃飯!";
}
/**
* 玩
*/
@Override
public String play() {
return "小明,玩!";
}
}
③定義beanName的枚舉
- 用于使用類型int獲取對應的beanName
package com.cc.eed.enums;
import lombok.Getter;
import org.springframework.util.Assert;
import java.util.Arrays;
/**
* <p></p>
*
* @author CC
* @since 2023/10/13
*/
@Getter
public enum PeopleEnum {
MING(1, "小明", "mingSpringBeanImpl"),
MEI(2, "小美", "meiSpringBeanImpl")
;
public Integer type;
public String name;
public String beanName;
/** <p>根據(jù)類型獲取beanName<p>
* @param type type
* @return {@link String}
* @since 2023/10/13
* @author CC
**/
public static String getBeanName(Integer type) {
PeopleEnum peopleEnum = Arrays.stream(values())
.filter(p -> p.getType().equals(type))
.findAny().orElse(null);
Assert.notNull(peopleEnum, "暫不支持的策略模式!");
return peopleEnum.getBeanName();
}
PeopleEnum(Integer type, String name, String beanName) {
this.type = type;
this.name = name;
this.beanName = beanName;
}
public void setType(Integer type) {
this.type = type;
}
public void setName(String name) {
this.name = name;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
}
④使用springBean工具類獲取beanName
- 略,見:https://www.cnblogs.com/kakarotto-chen/p/17760069.html
⑤使用
- 傳入不同的類型,獲取不同的策略
@Test
public void test02()throws Exception{
//根據(jù)BeanName獲取具體的bean,實現(xiàn)策略模式
//根據(jù)人員ID(或者類型)獲取不同的bean
String beanName = PeopleEnum.getBeanName(1);
ISpringBeanStrategy bean = (ISpringBeanStrategy) SpringBeanUtil.getBean(beanName);
String eating = bean.eating();
System.out.println(eating);
String play = bean.play();
System.out.println(play);
}
- 結(jié)果:
傳入1
傳入2
傳入除了1/2的
3、簡單工廠模式實現(xiàn)(推薦)
- 加自定義Bean的名字
3.1、實現(xiàn)步奏
- 可以看到,去獲取bean是交給工廠去做的,用戶只需要傳入類型即可。
3.2、實現(xiàn)
①定義策略接口
package com.cc.eed.strategy;
/**
* <p>簡單工廠模式 - 實現(xiàn)的策略模式</p>
*
* @author CC
* @since 2023/10/13
*/
public interface IFactoryStrategy {
/**
* 吃飯
*/
String eating();
/**
* 玩
*/
String play();
}
②生產(chǎn)策略bean的工廠
- 由于使用的@Resource注解,BUSINESS_FACTORY會自動注入所有實現(xiàn)了IFactoryStrategy接口的Bean。@Resource注解是Spring提供的一種依賴注入的方式,它會根據(jù)類型進行自動裝配。在這個例子中,BUSINESS_FACTORY是一個Map類型的成員變量,它的鍵是字符串類型,值是IFactoryStrategy類型。當Spring容器啟動時,會掃描并找到所有實現(xiàn)了IFactoryStrategy接口的Bean,并將它們自動注入到BUSINESS_FACTORY中。
- 由于BUSINESS_FACTORY使用了ConcurrentHashMap作為實現(xiàn),它會根據(jù)PirateEnum.values().length的大小來初始化容量。這樣可以確保BUSINESS_FACTORY的大小與實際注入的Bean數(shù)量一致,提高性能和效率。
package com.cc.eed.strategy;
import com.cc.eed.enums.PirateEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* <p>簡單工廠</p>
* <li>可以生產(chǎn)多個策略</li>
*
* @author CC
* @since 2023/10/13
*/
@Component
public class StrategyByFactory {
/**
* 1、批量注入實現(xiàn)了 IFactoryStrategy 的Bean。
* 2、用bean的數(shù)量當做map的大小
*/
@Resource
private final Map<String, IFactoryStrategy> BUSINESS_FACTORY = new ConcurrentHashMap<>(PirateEnum.values().length);
//生成的策略...
/** <p>根據(jù)類獲取不同的Bean<p>
* @param type type
* @return {@link IFactoryStrategy}
* @since 2023/10/13
* @author CC
**/
public IFactoryStrategy getBusinessMap(Integer type){
Assert.notNull(type, "類型不能為空!");
String beanName = PirateEnum.getBeanName(type);
return BUSINESS_FACTORY.get(beanName);
}
//生成的其他策略...
}
③策略實現(xiàn)類1
package com.cc.eed.strategy.impl;
import com.cc.eed.enums.PirateEnum;
import com.cc.eed.strategy.IFactoryStrategy;
import org.springframework.stereotype.Component;
/**
* <p>路飛</p>
*
* @author CC
* @since 2023/10/13
*/
@Component(PirateEnum.LF_BEAN_NAME)
public class LuFeiFactoryStrategy implements IFactoryStrategy {
/**
* 吃飯
*/
@Override
public String eating() {
return "路飛,吃飯!";
}
/**
* 玩
*/
@Override
public String play() {
return "路飛,玩!";
}
}
③策略實現(xiàn)類2
package com.cc.eed.strategy.impl;
import com.cc.eed.enums.PirateEnum;
import com.cc.eed.strategy.IFactoryStrategy;
import org.springframework.stereotype.Component;
/**
* <p>明哥</p>
*
* @author CC
* @since 2023/10/13
*/
@Component(PirateEnum.MG_BEAN_NAME)
public class MingGgFactoryStrategy implements IFactoryStrategy {
/**
* 吃飯
*/
@Override
public String eating() {
return "明哥,吃飯!";
}
/**
* 玩
*/
@Override
public String play() {
return "明哥,玩!";
}
}
④定義beanName的枚舉
package com.cc.eed.enums;
import org.springframework.util.Assert;
import java.util.Arrays;
/**
* <p></p>
*
* @author CC
* @since 2023/10/13
*/
public enum PirateEnum {
MG(11, "明哥", PirateEnum.MG_BEAN_NAME),
LF(22, "路飛", PirateEnum.LF_BEAN_NAME)
;
public Integer type;
public String name;
public String beanName;
/**
* 自定義的beanName
*/
public static final String MG_BEAN_NAME = "mingGg_11";
public static final String LF_BEAN_NAME = "luFei_22";
/** <p>根據(jù)類型獲取beanName<p>
* @param type type
* @return {@link String}
* @since 2023/10/13
* @author CC
**/
public static String getBeanName(Integer type) {
PirateEnum pirateEnum = Arrays.stream(values())
.filter(p -> p.getType().equals(type))
.findAny().orElse(null);
Assert.notNull(pirateEnum, "暫不支持的策略模式!");
return pirateEnum.getBeanName();
}
PirateEnum(Integer type, String name, String beanName) {
this.type = type;
this.name = name;
this.beanName = beanName;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
}
⑤使用springBean工具類獲取beanName
- 略,見:https://www.cnblogs.com/kakarotto-chen/p/17760069.html
⑥使用
- 需要注入工廠來調(diào)用方法即可
@Resource
private StrategyByFactory strategyByFactory;
@Test
public void test03()throws Exception{
//使用簡單工廠生產(chǎn)的策略模式 —— 明顯發(fā)現(xiàn)使用起來更簡單,把創(chuàng)建bean的權(quán)利交給了簡單工廠
IFactoryStrategy businessMap = strategyByFactory.getBusinessMap(33);
System.out.println(businessMap.eating());
System.out.println(businessMap.play());
}
結(jié)果:
-
傳入:11
-
傳入:22
文章來源:http://www.zghlxwxcb.cn/news/detail-711650.html
-
傳入:33
文章來源地址http://www.zghlxwxcb.cn/news/detail-711650.html
4、總結(jié)-工具類
- 實現(xiàn)的重點在于獲取Spring的bean
- 工具類:使用springBean工具類獲取beanName:
見:https://www.cnblogs.com/kakarotto-chen/p/17760069.html - 項目:https://gitee.com/KakarottoChen/blog-code.git
的:DesignDemo
到了這里,關(guān)于Java設計模式-策略模式-基于Spring實現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!