美文网首页
策略模式与状态模式

策略模式与状态模式

作者: copyLeft | 来源:发表于2022-03-05 06:18 被阅读0次

状态模式

状态模式将状态的切换交由具体的处理节点做判断, 容器只提供执行上下文

类模式实现

/**
 * 处理节点类
 */
abstract class State{
  state: string
  next: State
  
  constructor(state: string, next?: State){
    this.state = state
    this.next = next || this
  }
  // 状态切换方法,交由具体的子类实现
  abstract change():State
}


/**
 * 状态控制类
 */
class Store{
  // 游标, 标记下一可调用状态
  currentState: State
  constructor(currentState: State){
    this.currentState = currentState
  }
  run(){
    // 修改当前游标指向
    this.currentState = this.currentState.change()    
  }
}


/**
 * 具体的状态节点类实现
 */
class Success extends State{
  constructor(next?: State){
    const state = 'SUCCESS'
    super(state, next)
  }
  
  // 子类实现具体的状态处理
  change(): State {
    console.log(this.state)
    return this.next    
  }
}


class Fail extends State{
  constructor(next?: State){
    const state = 'Fail'
    super(state, next)
  }
  
  change(): State {
    console.log(this.state)
    return this.next
  }
}


class Loading extends State{


  success: State
  fail: State


  constructor(success?: State, fail?: State){
    const state = 'Loading'
    super(state)
    this.success = success || this
    this.fail = fail || this
  }
  
  change(): State {
    console.log(`
      ---------- LOADING ----------
    `)
    this.next = Number.parseInt(`${Math.random() * 10}`) % 2 ? this.success : this.fail
    return this.next
  }
}


function stateMod(){
  const success = new Success()
  const fail = new Fail()
  const loading = new Loading()
  const store = new Store(loading)


  success.next = loading
  fail.next = loading
  loading.success = success
  loading.fail = fail
  
  for(let i = 0; i < 10; i++){
    store.run()
  }
}


stateMod()

策略模式

调用具体执行的函数的决策交由容器决定

类实现

// 决策容器
abstract class Strategy<T extends string>{
  state: T
  constructor(initState: T){
    this.state = initState
  }


  // 状态的的切换交由的策略决策处理
  abstract strategy():void
}


type State = 'success' | 'fail' | 'loading'


class LoadData extends Strategy<State>{
  loading: Function
  success: Function
  fail: Function


  constructor(loading: Function, success: Function, fail: Function){
    super('loading')
    this.loading = loading
    this.success = success
    this.fail = fail
  }


  strategy(){
    switch (this.state) {
      case 'success':
        this.success()
        this.state = 'loading'
        break;
      case 'fail':
        this.fail()
        this.state = 'loading' 
        break;
      case 'loading':
       this.state = this.loading() ? 'success' : 'fail'
       break; 
    }
  }
}


function StrategyMod(){


  // 具体的执行不参与状态的切换
  const success = () => console.log('Success')
  const fail = () => console.log('Fail')
  const loading = () => {
    console.log(`
    ---------- LOADING ----------
  `)
   return Number.parseInt(`${Math.random() * 10}`) % 2
  } 


  const loadData = new LoadData(
    loading,
    success,
    fail
  )


  for(let i = 0; i < 10; i++){
    loadData.strategy()
  }
}
StrategyMod()

函数方式

interface IStrategy<T>{
  (s: T): T
}


type State = 'success' | 'fail' | 'loading'


/**
 * 值容器
 */
class Container<T>{
  state: T
  constructor(state: T){
    this.state = state
  }


  of(s: T){
    return new Container(s)
  }


  map(cb:IStrategy<T>){
    return this.of(cb(this.state))
  }
}


type Warp<T extends string> = (s: T) => Warp<T> 


function StrategyMod(){


  // 具体的执行不参与状态的切换
  const success = () => console.log('Success')
  const fail = () => console.log('Fail')
  const loading = () => {
    console.log(`
    ---------- LOADING ----------
  `)
   return Number.parseInt(`${Math.random() * 10}`) % 2
  } 


  // 决策函数
  const loadData = function(s: State): State{
    switch (s) {
      case 'success':
        success()
        return 'loading' 
      case 'fail':
        fail()
        return 'loading' 
      case 'loading':
        return loading() ? 'success' : 'fail' 
    }
}


  const list = new Array(10).fill('')
  list.reduce<Container<State>>((acc) => acc.map(loadData), new Container<State>('success'))
}


StrategyMod()

总结

策略模式与状态模式,关注的是状态切换的控制权。

  • 策略模式由中心容器根据输入控制具体的执行函数,控制权在容器中
  • 状态模式由当前的执行节点控制具体的状态设置, 控制权在状态节点中

策略模式更中心化,状态模式更分布式。

相关文章

  • 设计模式-行为模式-策略模式

    策略模式与状态模式看起来差不多,只是从概念上侧重不同。策略模式封装的是策略或算法,状态模式封装的是状态,主要区别在...

  • 策略模式和状态模式

    不怕跌倒,所以飞翔 本文中知识点概述: 策略模式和状态模式的基本概念 策略模式和状态模式的区别 策略模式和状态模式...

  • 策略模式与状态模式

    状态模式 状态模式将状态的切换交由具体的处理节点做判断, 容器只提供执行上下文 类模式实现 策略模式 调用具体执行...

  • 设计模式整理(7) 状态模式

    学习《Android 源码设计模式解析与实践》系列笔记 介绍 状态模式和策略模式都是行为型模式。状态模式中的行为是...

  • Java设计模式 - 状态模式

    定义 状态模式,即因状态而定,因状态而产生具体行为。状态模式与策略模式极其相似,但是二者却有着天壤之别。状态模式的...

  • 状态模式

    什么是状态模式 通过不同的状态,动态调用不同的对象,完成不同的行为(状态模式与策略模式挺相似,但是状态模式是没有共...

  • 现实中的模式应用

    桥接模式 针对接口编程,就是桥接模式 策略模式 策略模式也可看作是针对接口编程 状态模式 状态模式通过 Conte...

  • 设计模式-状态模式

    介绍 状态模式和策略模式是一对双胞胎,他们都属于行为设计模式。状态模式和策略模式都是为具有多种可能情形设计的模式,...

  • Android设计模式—策略模式与状态模式

    策略模式和状态模式都比较简单好理解,都是为了解决一个问题有多种方案这样的场景,两者结构几乎一样。 不同的是状态模式...

  • 设计模式之——状态模式与策略模式

    如果你的简历里出现了"设计模式"的字样,那么作为面试官的我几乎都会问到一个问题: "状态模式与策略模式有哪些区别"...

网友评论

      本文标题:策略模式与状态模式

      本文链接:https://www.haomeiwen.com/subject/ytuvrrtx.html