aiyoudiao aiyoudiao
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档

码二

扫微信二维码,认识一下码二吧😉。
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档
  • LeetCode

  • 算法

  • 数据结构

  • 设计模式

    • 浅聊设计理念
    • 谈一谈设计原则
    • 工厂方法模式
    • 抽象工厂模式
    • 建造者模式
    • 原型模式
    • 单例模式
    • 桥接模式
    • 代理模式
    • 装饰器模式
    • 适配器模式
    • 享元模式
    • 组合模式
    • 外观模式
    • 观察者模式
    • 模板方法模式
    • 策略模式
      • 前言
      • 策略模式
    • 职责链模式
    • 状态模式
    • 迭代器模式
    • 访问者模式
    • 备忘录模式
    • 命令模式
    • 解释器模式
    • 中介者模式
  • Other

  • vue3设计与实现

  • 算法与设计
  • 设计模式
aiyoudiao
2022-02-23

策略模式

# 前言

策略模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

# 策略模式

使用场景:这种模式是将一个个算法、行为封装起来,然后通过切换的方式去使用这些算法、行为,最后进行最终的运算、执行。这种模式在生活种比较常见,比如 不同接口的螺丝刀、不同风格的衣服、不同款式的显示器等等,非常多。

理解:这是一种类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,很常见。使用这种设计模式的前提是,要先定义好功能接口,这样切换不同算法、行为时才能正常运行。这种设计模式本身就是为了减少过多的 if else冗余,所以功能接口的设计很重要,比如借助Map这样的数据结构,那么就更加方便了,甚至都不需要去封装类,直接封装成函数放入map中,根据不同的key来使用相应的函数。

namespace action_mode_03 {

    // 接口
    interface IPlay {
        buy(price: number): number
        run(): string

    }


    class Computer implements IPlay {
        price: number = 6000

        buy(price: number): number {
            // 打六折
            const finalPrice = this.price * 0.6
            return price  - finalPrice
        }

        run(): string {
            return '计算机开机 =======> 欢迎使用 windows10'
        }

    }

    class Pad implements IPlay {
        price: number = 10000

        run(): string {
            return '平板开机 =======> 欢迎使用 华为平板'
        }

        buy(price: number): number {
            // 打七五折
            const finalPrice = this.price * 0.75
            return price - finalPrice
        }
    }


    class TV implements IPlay {
        price: number = 3000

        run(): string {
            return '电视开机 =======> 欢迎使用 小米电视'
        }

        buy(price: number): number {
            // 打8折
            const finalPrice = this.price * 0.8
            return price - finalPrice
        }
    }

    // 操作类
    class Student {

        money: number

        constructor(money: number) {
            this.money = money
        }

        get(strategyType: StrategyType) {
            const strategy = new strategyList[strategyType]
            const result = strategy.buy(this.money)
            if (result < 0) {
                return '钱不够,购买失败'
            }
            return strategy.run()
        }
    }

    // 策略的枚举
    enum StrategyType {
        Computer,
        Pad,
        TV,
    }

    // 策略的定义
    const strategyList = {
        [StrategyType.Computer]: Computer,
        [StrategyType.Pad]: Pad,
        [StrategyType.TV]: TV,
    }

    const xiaoming = new Student(5000)

    console.log("小明买计算机", xiaoming.get(StrategyType.Computer))
    console.log("小明买平板电脑", xiaoming.get(StrategyType.Pad))
    console.log("小明买液晶电视机", xiaoming.get(StrategyType.TV))
}


#设计模式
上次更新时间: 10年18月2023日 01时57分53秒
模板方法模式
职责链模式

← 模板方法模式 职责链模式 →

最近更新
01
01.数据结构导论一览.md
10-16
02
30.2023年06月04日.md
06-04
03
08.与测量相关.md
05-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 aiyoudiao 码二 备案号: 鄂ICP备2022002654号-1