單例模式有效解決過多的if-else

策略模式

引例:假如我們要分享一個篇文章。有微信分享、微博分享、QQ分享等……我們是先判斷類型是哪個,然後再調用各自得API去做分享操作

一般來說,大多數人都會根據類型判斷是哪個渠道吧,如下程式碼,但是隨著渠道越來越多,那if也越來越多……

//假如要分享一個東西。有微信分享、微博分享、QQ分享......
public void share(String type) {
   	if ("微信".equals(type)) {
           //微信分享操作
           System.out.println("微信分享處理...");
       } else if ("微博".equals(type)) {
           System.out.println("微博分享處理...");
       }else if ("頭條".equals(type)) {
           System.out.println("頭條分享處理...");
       }else if ("抖音".equals(type)) {
           System.out.println("抖音分享處理...");
       }else if ("網易雲".equals(type)) {
           System.out.println("網易雲分享處理...");
       }else if ("知乎".equals(type)) {
           System.out.println("知乎分享處理...");
       } else if ("QQ".equals(type)) {
           System.out.println("QQ分享處理...");
       } else {
   		// 一大堆,這種可以採用策略設計模式重構
   	}
}

換種寫法可以嘛,switch case試試,看起來稍微好看了點,問題依然存在

public void share(String type) {
	switch(trpe) {
		case "微信":
			System.out.println("微信分享處理...");
			break;
		case "微信":
			System.out.println("微信分享處理...");
			break;
		case "微博":
			System.out.println("微博分享處理...");
			break;
		case "頭條":
			System.out.println("頭條分享處理...");
			break;
		default:
			//其他得
			break;
	}
}

再思考咋整,於是乎,想到用策略模式

簡單說說策略模式

  • 就是定義一系列演算法,把他們一個個封裝起來,並且使他們可相互替換
  • 主要解決再多種演算法相似得情況下,使用if-else所帶來得複雜和難以維護
  • 簡單案例:
    • 諸葛亮得錦囊妙計,每一個錦囊就是一個策略
    • 旅行得出遊方式,每一種方式就是一個策略

示例:

  1. 搞一個介面 DealStrategy 裡面是一個處理方法
 public interface DealStrategy {
     public void dealWith(String type);
 }
  1. 創建實現類
  • QQ分享得實例
public class QQDealStrategy implements DealStrategy {
	@Override
	public void dealWith(String type) {
		System.out.println(type + "QQ 分享成功...");
	}
}
  • Sina分享
public class SinaDealStrategy implements DealStrategy {
    @Override
    public void dealWith(String type) {
        System.out.println(type + "sina 微博 分享成功...");
    }
}
  • Wechat分享
public class WechatDealStrategy implements DealStrategy {
    @Override
    public void dealWith(String type) {
        System.out.println(type + "Wechat 分享成功...");
    }
}
  1. 創建一個Context上下文,負責使用策略
public class StrategyContext {
    private String type;
    private DealStrategy strategy;

    public StrategyContext(String type, DealStrategy strategy) {
        this.type = type;
        this.strategy = strategy;
    }

    public DealStrategy getStrategy() {
        return strategy;
    }

    public boolean options(String type) {
        return this.type.equals(type);
    }
}
  1. 使用上下文來做我們分享文章得需求
  • 把所有策略裝到list里,並且載入好
  • 通過context上下文,去切換使用策略分享
public class TestStrategy {

    private static List<StrategyContext> list = new ArrayList<>();
    //載入所有策略
    static {
        list.add(new StrategyContext("Wechat", new WechatDealStrategy()));
        list.add(new StrategyContext("QQ", new QQDealStrategy()));
        list.add(new StrategyContext("Sina", new SinaDealStrategy()));
    }

    public void share(String type) {
        DealStrategy dealStrategy = null;
        for (StrategyContext context: list) {
            if (context.options(type)) {
                dealStrategy = context.getStrategy();
                break;
            }
        }
        dealStrategy.dealWith(type);
    }

    public static void main(String[] args) {
        new TestStrategy().share("Wechat");
    }
}

以上,我們隨意使用,type為啥都行,只要有這個策略,自動去切換…
想要擴展得話,也很簡單,先把獨立得策略寫好,裝配到上下文,然後使用

–紙上得來終覺淺,絕知此事要躬行–大佬們要去試試哈~!!

Tags: