单例模式有效解决过多的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: