简单易用策略模式分析与实例详解
1.简介 In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use. ——Quote from wikipedia 在计算机编程中,策略模式(也称为策略模式)是一种行为软件设计模式,它支持在运行时选择算法。代码接收运行时指令,而不是直接实现单个算法,这些指令在一系列算法中使用。 [站外图片上传中...(image-c98784-1573659608688)] 2.策略模式的特点 优点: 1、算法可以自由切换。 2、避免使用多重条件判断。 3、扩展性良好。 缺点: 1、策略类会增多。 2、所有策略类都需要对外暴露。 使用场景: 1、如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为