肝经不通吃什么中成药| siemens是什么品牌| 耳石是什么| 吃什么补血小板效果最好| 格列本脲和格列美脲有什么区别| 肉桂属于什么茶类| 外地车进北京有什么限制| 老克勒是什么意思| 非萎缩性胃炎吃什么药效果好| qs什么意思| 壬子五行属什么| 抗原是什么| 什么的地方| 链球菌感染是什么病| 肿标五项查的是什么| 浓茶喝多了有什么危害| 稽留热常见于什么病| 鸡腿炖什么好吃| 鱼胶是什么| 咂是什么意思| 肛门潮湿瘙痒用什么药最好| 墙内开花墙外香是什么意思| 梦到人死了是什么意思| 什么叫轻断食| 锡兵是什么| kgs是什么单位| 吃完海鲜不能吃什么水果| 什么是承兑| 胃炎吃什么药好使| 键盘侠是什么意思| 澳门为什么叫澳门| 拔萝卜是什么意思| 泥鳅吃什么饲料| 头发突然秃了一块是什么原因| 爱放屁吃什么药| 槟榔长什么样子| 口腔溃疡吃什么水果好得快| 梅肉是什么肉| 什么食物含镁| 戒断反应是什么意思| cpb是什么意思| 旁风草长什么样| 养狗的人容易得什么病| 十八罗汉分别叫什么| 7月28是什么星座| 生意是什么意思| 九牛一毛什么意思| 黄芪搭配什么不上火| ahc是什么牌子| 属猪男和什么属相最配| 断眉有什么说法| 赖床什么意思| 太爷爷的爸爸叫什么| 国标舞是什么舞| igg抗体阳性是什么意思| 长期口苦是什么原因| 血稠吃什么药好| 吃什么东西养胃| 流鼻血去药店买什么药| 为什么遗精| 暂告一段落是什么意思| 11是什么意思| 便秘喝什么茶好| 孕妇耳鸣是什么原因引起的| 左心室高电压是什么意思| 摆谱是什么意思| 什么米好吃又香又软| 练八段锦有什么好处| editor是什么意思| 什么的雨| 什么头什么臂| 大水冲了龙王庙什么意思| 藏红花是什么| 忐忑什么意思| 红红的苹果像什么句子| 窦性心律什么意思| 骨盆前倾有什么危害| 大眼角痒是什么原因| 乙肝核心抗体是什么意思| 省长是什么级别| 苏州五行属什么| 全职太太是什么意思| 吃什么增加白细胞最快| 手淫是什么意思| 小孩拉肚子应该吃什么食物好| 蓝莓泡酒有什么功效| 纵什么意思| 打胎药叫什么名字| 脊柱侧弯是什么原因引起的| 为什么空调不制冷| andy是什么意思| 六月二十一是什么日子| 苯磺酸氨氯地平片什么时候吃最好| 孩子出汗多是什么原因| 井底之蛙是什么意思| mdzz是什么意思| c919是什么意思| 男人占有欲强说明什么| 红细胞数目偏高是什么意思| 煮海带放什么容易烂| 狗仗人势是什么生肖| 皮肤为什么会痒| 花生什么时候成熟| 天天睡不着觉什么原因| 梦见家被偷了什么预兆| 学富五车是什么意思| 体质是什么意思| 痛风能喝什么饮料| 无条件是什么意思| hcv是什么病毒| 尿道痛什么原因| 早上吃黄瓜有什么好处| 白蜡烛代表什么| 梦到蛇预示着什么| 1027是什么星座| 吃什么好消化| 叔公是什么辈分| 清创是什么意思| 奇亚籽在中国叫什么| 金标是什么意思| 赵构为什么杀岳飞| 似水年华是什么意思| 塞翁失马是什么生肖| 嘴里起泡是什么原因| 五月初九是什么星座| 狗感冒了吃什么药| 以色列是什么人种| 人的五官指什么| 状元及第是什么意思| 婴儿湿疹用什么药膏| 脚有酸味是什么原因| 巴宝莉属于什么档次| 感冒发烧能吃什么水果| 来月经吃什么排得最干净| hpv什么症状| 金益什么字| v是什么化学元素| 不知道为什么| 1996年是属什么生肖| 酱油的原料是什么| 发福了是什么意思| 啤酒花是什么东西| 胎盘1级什么意思| 布鲁斯是什么| 沙僧是什么生肖| 什么是封闭针| 吃什么补营养最快| 得了咽炎有什么症状| 阿飞是什么意思| 皮肤瘙痒用什么药| 血常规异常是什么意思| 钛色是什么颜色| 为什么空调不制冷| gdp是什么意思| 手指甲有竖纹是什么原因| 酵母样真菌是什么意思| 虫草花不能和什么一起吃| 嗝什么意思| 750是什么材质| 心里想的话用什么标点符号| 什么药治痒效果最好| 属虎生什么属相宝宝好| 脑供血不足做什么检查能查出来| 巴厘岛机场叫什么| sp是什么面料| 龙眼树上的臭虫叫什么| 铁剂什么时候吃最好| 新生儿甲状腺偏高有什么影响| 什么入伏| 蒹葭苍苍是什么意思| 吃什么水果可以减肥| rosa是什么意思| 小舌头学名叫什么| 检查盆腔炎做什么检查| 壁虎在家里是什么征兆| 胎头位于耻上是什么意思| 梦到明星是什么意思| 替代品是什么意思| 咽炎吃什么| 错构瘤是什么意思| 血管堵塞吃什么好| 不外乎是什么意思| 姓卢的男孩起什么名字好| 子宫内膜炎用什么药效果好| 异烟肼是什么药| 螃蟹过街的歇后语是什么| 鹿鞭泡酒有什么功效| 肾结石有什么症状哪里疼| 芹菜不能和什么食物一起吃| 七月是什么生肖| 盐为什么要加碘| 人均可支配收入是什么意思| 流黄鼻涕是什么原因| 耳结是什么原因造成的| 同房子宫疼痛什么原因| 60是什么意思| 油漆味对人有什么危害| 扁桃体发炎不能吃什么东西| 圆珠笔是什么笔| 黄盖代表什么生肖| asics是什么牌子| 静脉曲张是什么意思| 警察两杠一星是什么级别| 金鱼可以吃什么| 立加羽念什么| 柴鱼是什么鱼| 秦二世为什么姓胡| 看胃病挂什么科| 尿频去药店买什么药| 20度穿什么| 温度计代表什么生肖| 229什么星座| hpv16是什么意思| 钾低了会出现什么症状| 痈肿疮疖是什么意思| 为什么手会脱皮| 恐惧感是什么意思| momax是什么牌子| 被弹颏是什么意思| 晚上吃什么水果减肥效果最好| 头晕是什么情况| 烧钱是什么意思| 小确幸是什么意思| 建字五行属什么| 子宫为什么会长息肉| 氯化镁是什么| 不带壳的蜗牛叫什么| 靶向药是什么药| 凌晨1点是什么时辰| 牡丹花是什么颜色| 冲击波治疗有什么效果| 平均血小板体积低是什么原因| 补脾吃什么食物最好| 感冒发烧挂什么科室| 女人蜕变是什么意思| 核桃不能和什么一起吃| 化疗吃什么补白细胞| 数值是什么意思| 什么叫潮汐车道| 黄瓜和什么不能一起吃| 广西狗肉节是什么时候| 郎中是什么意思| 边界欠清是什么意思| 五月七号是什么星座| ca125是查什么的| 淋巴细胞百分比高是什么意思| 人流后可以吃什么水果| 霸屏是什么意思| 木加一笔有什么字| 脐炎用什么药| 内疚是什么意思| 指甲发黑是什么原因| 商务专员是做什么的| 手足口疫苗叫什么名字| Picu病房是什么意思| 便秘吃什么能通便| 闷葫芦是什么意思| 前胸贴后背是什么意思| 蜈蚣是什么样的| 4月4日是什么星座| 肠炎是什么症状| 血蛋白低是什么原因| 牙龈黑紫色是什么原因| 一什么湖面| 百度Jump to content

圣彼得堡地铁爆炸后:地面交通工具全部免费运营

From Wikipedia, the free encyclopedia
百度 专家指出,网站服务在向着“应上尽上”方向发展的同时,实际生活中能否“在线”办理直接关系公众体验的好坏。

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 runtime instructions as to which in a family of algorithms to use.[1]

Strategy lets the algorithm vary independently from clients that use it.[2] Strategy is one of the patterns included in the influential book Design Patterns by Gamma et al.[3] that popularized the concept of using design patterns to describe how to design flexible and reusable object-oriented software. Deferring the decision about which algorithm to use until runtime allows the calling code to be more flexible and reusable.

For instance, a class that performs validation on incoming data may use the strategy pattern to select a validation algorithm depending on the type of data, the source of the data, user choice, or other discriminating factors. These factors are not known until runtime and may require radically different validation to be performed. The validation algorithms (strategies), encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication.

Typically, the strategy pattern stores a reference to code in a data structure and retrieves it. This can be achieved by mechanisms such as the native function pointer, the first-class function, classes or class instances in object-oriented programming languages, or accessing the language implementation's internal storage of code via reflection.

Structure

[edit]

UML class and sequence diagram

[edit]
A sample UML class and sequence diagram for the Strategy design pattern.[4]

In the above UML class diagram, the Context class does not implement an algorithm directly. Instead, Context refers to the Strategy interface for performing an algorithm (strategy.algorithm()), which makes Context independent of how an algorithm is implemented. The Strategy1 and Strategy2 classes implement the Strategy interface, that is, implement (encapsulate) an algorithm.
The UML sequence diagram shows the runtime interactions: The Context object delegates an algorithm to different Strategy objects. First, Context calls algorithm() on a Strategy1 object, which performs the algorithm and returns the result to Context. Thereafter, Context changes its strategy and calls algorithm() on a Strategy2 object, which performs the algorithm and returns the result to Context.

Class diagram

[edit]
Strategy Pattern in UML

[5]

Strategy pattern in LePUS3 (legend)


Strategy and open–closed principle

[edit]
Accelerate and brake behaviors must be declared in each new car model.

According to the strategy pattern, the behaviors of a class should not be inherited. Instead, they should be encapsulated using interfaces. This is compatible with the open–closed principle (OCP), which proposes that classes should be open for extension but closed for modification.

As an example, consider a car class. Two possible functionalities for car are brake and accelerate. Since accelerate and brake behaviors change frequently between models, a common approach is to implement these behaviors in subclasses. This approach has significant drawbacks; accelerate and brake behaviors must be declared in each new car model. The work of managing these behaviors increases greatly as the number of models increases, and requires code to be duplicated across models. Additionally, it is not easy to determine the exact nature of the behavior for each model without investigating the code in each.

The strategy pattern uses composition instead of inheritance. In the strategy pattern, behaviors are defined as separate interfaces and specific classes that implement these interfaces. This allows better decoupling between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code changes. Behaviors can also be changed at runtime as well as at design-time. For instance, a car object's brake behavior can be changed from BrakeWithABS() to Brake() by changing the brakeBehavior member to:

brakeBehavior = new Brake();
/* Encapsulated family of Algorithms
 * Interface and its implementations
 */
public interface IBrakeBehavior {
    public void brake();
}

public class BrakeWithABS implements IBrakeBehavior {
    public void brake() {
        System.out.println("Brake with ABS applied");
    }
}

public class Brake implements IBrakeBehavior {
    public void brake() {
        System.out.println("Simple Brake applied");
    }
}

/* Client that can use the algorithms above interchangeably */
public abstract class Car {
    private IBrakeBehavior brakeBehavior;

    public Car(IBrakeBehavior brakeBehavior) {
      this.brakeBehavior = brakeBehavior;
    }

    public void applyBrake() {
        brakeBehavior.brake();
    }

    public void setBrakeBehavior(IBrakeBehavior brakeType) {
        this.brakeBehavior = brakeType;
    }
}

/* Client 1 uses one algorithm (Brake) in the constructor */
public class Sedan extends Car {
    public Sedan() {
        super(new Brake());
    }
}

/* Client 2 uses another algorithm (BrakeWithABS) in the constructor */
public class SUV extends Car {
    public SUV() {
        super(new BrakeWithABS());
    }
}

/* Using the Car example */
public class CarExample {
    public static void main(final String[] arguments) {
        Car sedanCar = new Sedan();
        sedanCar.applyBrake();  // This will invoke class "Brake"

        Car suvCar = new SUV();
        suvCar.applyBrake();    // This will invoke class "BrakeWithABS"

        // set brake behavior dynamically
        suvCar.setBrakeBehavior( new Brake() );
        suvCar.applyBrake();    // This will invoke class "Brake"
    }
}

See also

[edit]

References

[edit]
  1. ^ "The Strategy design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2025-08-05.
  2. ^ Eric Freeman, Elisabeth Freeman, Kathy Sierra and Bert Bates, Head First Design Patterns, First Edition, Chapter 1, Page 24, O'Reilly Media, Inc, 2004. ISBN 978-0-596-00712-6
  3. ^ Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 315ff. ISBN 0-201-63361-2.{{cite book}}: CS1 maint: multiple names: authors list (link)
  4. ^ "The Strategy design pattern - Structure and Collaboration". w3sDesign.com. Retrieved 2025-08-05.
  5. ^ "Design Patterns Quick Reference – McDonaldLand".
[edit]
鼻子经常出血是什么病征兆 阴吹是什么 救济的近义词是什么 缺钾吃什么药 球拍状胎盘对胎儿有什么影响
怀姜是什么姜 啮齿类动物什么意思 结石吃什么好 什么东西止血最快最好 心功能一级什么意思
热水器什么品牌好 粉红粉红的什么填空 癖是什么意思 牛肉和什么炒最好吃 天上的星星为什么会发光
割包皮挂什么科 胃食管反流吃什么中成药最好 荣辱与共是什么生肖 消化功能紊乱吃什么药 老蒯是什么意思
梦见上班迟到什么意思hcv9jop0ns1r.cn 经常腿麻是什么原因hcv9jop2ns9r.cn 激光是什么hcv8jop5ns2r.cn 脚背上长痣代表什么hcv8jop3ns0r.cn 口扫是什么hcv9jop3ns4r.cn
左手臂有痣代表什么hcv8jop9ns1r.cn 爱是什么词hcv7jop9ns2r.cn 肥牛是什么肉hcv8jop3ns9r.cn www是什么网hcv7jop5ns1r.cn 维生素b12又叫什么hcv9jop1ns2r.cn
穿孔是什么意思hcv8jop8ns9r.cn 感冒为什么会打喷嚏inbungee.com dpo是什么意思hcv9jop3ns9r.cn 凌晨三四点是什么时辰hcv8jop8ns2r.cn 做梦笑出声是什么预兆hcv9jop1ns6r.cn
抹茶粉是什么做的sanhestory.com 红痣是什么原因引起的hcv9jop5ns1r.cn cg是什么意思hcv9jop5ns4r.cn 晚上左眼皮跳预示什么hcv9jop3ns4r.cn 月子里生气有什么危害hcv7jop5ns4r.cn
百度