设计模式(19):策略模式

策略模式

  • 策略模式对应与解决某一个问题的一个算法族,允许用户从该算法族中任选一个算法解决某一问题,同时可以方便的更换算法或者增加新的算法。并且由客户端决定调用哪个算法。

本质

  • 分离算法,选择实现;

策略模式角色

  • 上下文类(Context): 维护了一个策略类的引用,并将客户端的请求委托给具体策略类处理;
  • 抽象策略类(Strategy): 定义了具体的算法方法;
  • 具体策略类(ConcreteStrategy): 对抽象策略类的算法具体实现;

开发中常见的场景

  • spring框架中,Resource接口,资源访问策略;
  • springmvc框架中,controller方法入参解析
  • shiro框架中,权限的认证策略;

场景

  • 某个市场人员接到单后的报价策略(CRM系统中常见问题)。报价策略很复杂,可以简单作如下分类:
    • 普通客户小批量报价
    • 普通客户大批量报价
    • 老客户小排量报价
    • 老客户大批量报价
  • 具体选用哪个报价策略,这需要根据实际情况来确定。这时候,我们采用策略模式即可。

代码实现

  • 抽象策略类(Strategy)
java 复制代码
/**
 * 抽象算法 接口
 */
public interface  Strategy {
	double getPrice(double standardPrice);
}
  • 具体策略类(ConcreteStrategy)
java 复制代码
/**
 * 新客户小批量
 */
public class NewCustomerFewStrategy implements Strategy{
	@Override
	public double getPrice(double standardPrice) {
		System.out.println("不好意思,不打折,原件");
		return standardPrice;
	}	
}
/**
 * 新客户大批量
 */
public class newCustomermanyStrategy implements Strategy{
	@Override
	public double getPrice(double standardPrice) {
		System.out.println("九折");
		return standardPrice*0.9;
	}	
}
/**
 * 老客户小批量
 */
public class OldCustomerFewStrategy implements Strategy{
	@Override
	public double getPrice(double standardPrice) {
		System.out.println("八五折");
		return standardPrice*0.85;
	}	
}
/**
 * 老客户大批量
 */
public class OldCustomermanyStrategy implements Strategy{
	@Override
	public double getPrice(double standardPrice) {
		System.out.println("七折");		
		return standardPrice*0.7;	}	
}
  • 上下文类(Context)
java 复制代码
/**
 * 负责和具体的策略类交互
 * 这样的话,具体的算法和直接的客户端调用分离了,使得算法可以独立于客户端独立的变化
 */
public class Context {
	private Strategy strategy;
	public Context(Strategy strategy) {
		this.strategy = strategy;
	}
	public void setStrategy(Strategy strategy) {
		this.strategy = strategy;
	}
	public void PrintPrice(double s){
		System.out.println("你该报价:"+strategy.getPrice(s));
	}	
}
  • 客户端调用
java 复制代码
public static void main(String[] args) {
	Strategy st=new OldCustomermanyStrategy();
	Context ct=new Context(st);
	ct.PrintPrice(999);
}

更多设计模式学习:

设计模式(1):介绍

设计模式(2):单例模式

设计模式(3):工厂模式

设计模式(4):建造者模式

设计模式(5):原型模式

设计模式(6):桥接模式

设计模式(7):装饰器模式

设计模式(8):组合模式

设计模式(9):外观模式

设计模式(10):享元模式

设计模式(11):适配器模式

设计模式(12):代理模式

设计模式(13):模板方法模式

设计模式(14):命令模式

设计模式(15):迭代器模式

设计模式(16):观察者模式

设计模式(17):中介者模式

设计模式(18):状态模式

设计模式持续更新中...

相关推荐
染指111031 分钟前
135.Agent-多Agent框架-LangChain多智能体(SubAgents子代理)
数据库·人工智能·设计模式·langchain·agent·agents
路弥行至1 天前
【Head First 设计模式】第 2 章:观察者模式 —— 用现代 C++ 重新解读“交互对象的松耦合
c++·经验分享·笔记·观察者模式·设计模式·入门教程·headfirst
YYYing.1 天前
【设计模式系列 (九) 】装饰器模式
c++·后端·设计模式·装饰器模式·c/c++
沫璃染墨1 天前
《从零入门Linux系统篇(五十六):线程篇·九——生产者消费者模型:从条件变量到BlockingQueue实现》
linux·服务器·c++·驱动开发·设计模式·架构·系统架构
c萱2 天前
AI产品经理——01基础认知篇
设计模式·prompt·aigc·产品经理·ai编程·gitcode·ai-native
蜗牛互联网2 天前
Computer Use公共预览的安全设计:应用、动作与数据三维门禁
java·人工智能·后端·安全·策略模式
YYYing.2 天前
【设计模式系列 (八) 】组合模式
c++·后端·设计模式·组合模式·c/c++
geovindu3 天前
rust: Composite Pattern
开发语言·后端·设计模式·rust·组合模式
今年下半年3 天前
【Spring Boot】多种存储文件(FastDFS / MinIO / 阿里云 OSS)接入设计说明(附源码)
spring boot·分布式·中间件·简单工厂模式·策略模式
YYYing.3 天前
【设计模式系列 (七) 】桥接模式
c++·后端·设计模式·桥接模式