`
cjuexuan
  • 浏览: 13191 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

scala实现状态模式

 
阅读更多
package com.linewell.modeldesgin.state


/**
* * 状态模式,银行账户,环境类
* Created by ctao on 2015/9/2.
* @param owner 持卡人
* @param state 账户状态
* @param balance 余额
*/
class Account(owner: String, var state: State = null, var balance: Double = 0) {
    /**
     * 初始化帐号,设置帐号为正常状态
     */
    state = NormalState(this)

    /**
     * 存款
     * @param amount 操作金额
     */
    def deposit(amount: Double): Unit = {
        println(s"${owner}存款$amount")
        state.deposit(amount)
        println(s"现在余额为$balance")
        println(s"现在账户状态为${state.getClass.getName.split("\\.").last}")
        println("---------------------------------------")
    }

    /**
     * 取款
     * @param amount 操作金额
     */
    def withdraw(amount: Double): Unit = {
        println(s"${owner}取款$amount")
        state.withdraw(amount)
        println(s"现在余额为$balance")
        println(s"现在账户状态为${state.getClass.getName.split("\\.").last}")
        println("---------------------------------------")
    }

    /**
     * 检测帐号状态是否需要支付利息
     */
    def computeInterest(): Unit = state.computeInterest()


    /**
     * 正常状态
     * @param account 账户
     */
    case class NormalState(var account: Account) extends State {

        override def computeInterest(): Unit = println("正常状态无需利息")

        override def withdraw(amount: Double): Unit = amount match {
            case x if account.balance + 2000 < x => println(s"卡上余额不足,请重新选择取款金额")
            case _ => account.balance -= amount
                stateCheck()

        }

        override def deposit(amount: Double): Unit = {
            account.balance += amount
            stateCheck()
        }

        override def stateCheck(): Unit = account.balance match {
            case x if x > -2000 && x <= 0 => account.state = OverdraftState(account)
            case x if x == -2000 => account.state = RestrictedState(account)
            case x if x < -2000 => println("操作受限")
            case x if x > 0 =>
            case _ => println(s"正常下异常${account.balance}")
        }
    }

    /**
     * 冻结状态
     * @param account 账户
     */
    case class OverdraftState(var account: Account) extends State {
        override def withdraw(amount: Double): Unit = amount match {
            case x if account.balance + 2000 < x => println(s"卡上余额不足,请重新选择取款金额")
            case _ => account.balance -= amount
                stateCheck()

        }

        override def deposit(amount: Double): Unit = {
            account.balance += amount
            stateCheck()
        }

        override def computeInterest(): Unit = println("透支状态计算利息")


        override def stateCheck(): Unit = account.balance match {
            case x if x > 0 => account.state = NormalState(account)
            case x if x == -2000 => account.state = RestrictedState(account)
            case x if x < -2000 => println("操作受限")
            case x if x < 0 && x > -2000 =>
            case _ => println(s"冻结下异常${account.balance}")
        }
    }


    /**
     * 限制状态
     * @param account 账户
     */
    case class RestrictedState(var account: Account) extends State {
        override def deposit(amount: Double): Unit = {
            account.balance += amount
            stateCheck()
        }

        override def computeInterest(): Unit = println("受限状态计算利息")

        override def withdraw(amount: Double): Unit = println("帐号受限,取款失败")

        override def stateCheck(): Unit = account.balance match {
            case x if x > 0 => account.state = NormalState(account)
            case x if x > -2000 => account.state = OverdraftState(account)
        }
    }

}

trait State {
    /**
     * 帐号
     */
    protected var account: Account

    /**
     * 存钱
     * @param amount 操作金额
     */
    def deposit(amount: Double)

    /**
     * 取钱
     * @param amount 操作金额
     */
    def withdraw(amount: Double)

    /**
     * 检测状态
     */
    def computeInterest(): Unit

    /**
     * 状态转化
     */
    def stateCheck(): Unit
}

package com.linewell.modeldesgin.state

/**
* 测试客户端
* Created by ctao on 2015/9/2.
*/
object Client extends App {
    val acc1 = new Account("chen")
    val acc2 = new Account("chen")

    acc1.deposit(1000)
    acc1.withdraw(20000)
    acc1.withdraw(2000)
    acc1.withdraw(1001)
    acc1.computeInterest()

    println("++++++++++++++++++++++++++")

    acc2.deposit(10001)
    acc2.withdraw(200)
    acc2.withdraw(2000)
    acc2.withdraw(1001)
    acc2.computeInterest()
}


package com.linewell.modeldesgin.state

/**
* 屏幕类,环境类
* Created by ctao on 2015/9/2.
*/
class Screen(var long: Int = 1, var wide: Int = 1) {

    /**
     * 初始化屏幕状态
     */
    private var screenState: ScreenState = NormalScreenState(this)
    /**
     * 显示长宽
     */
    show()

    /**
     * 点击事件,点击触发状态的改变
     */
    def onClick(): Unit = screenState match {
        case NormalScreenState(_) =>
            this.wide += this.wide
            this.long += this.long
            screenState = LargerScreenState(this)
            print("点击:")
            screenState.display()
        case LargerScreenState(_) =>
            this.wide += this.wide
            this.long += this.long
            screenState = LargestScreenState(this)
            print("点击:")
            screenState.display()
        case LargestScreenState(_) =>
            this.wide = this.wide / 4
            this.long = this.long / 4
            screenState = NormalScreenState(this)
            print("点击:")
            screenState.display()
    }

    /**
     * 展示
     */
    def show(): Unit ={
        print("显示:")
        screenState.display()
    }


}

/**
* 密封屏幕状态
*/
sealed abstract  class  ScreenState {
    def display(): Unit = {}
}

/**
* 正常状态
* @param screen 屏幕
*/
case class NormalScreenState(screen: Screen) extends ScreenState {

    override def display(): Unit = {
        println(s"正常大小,长:${screen.wide},宽:${screen.long}")
    }
}

/**
* 放大两倍状态
* @param screen 屏幕
*/
case class LargerScreenState(screen: Screen) extends ScreenState {
    override def display(): Unit = {
        println(s"两倍大小,长:${screen.wide},宽:${screen.long}")
    }
}

/**
* 放大四倍状态
* @param screen 屏幕
*/
case class LargestScreenState(screen: Screen) extends ScreenState {
    override def display(): Unit = {
        println(s"四倍大小,长:${screen.wide},宽:${screen.long}")
    }

}





package com.linewell.modeldesgin.state

/**
* 测试客户端
* Created by ctao on 2015/9/2.
*/
object ClientScreen extends App {
    val screen = new Screen
    screen.show()
    screen.onClick()
    screen.show()
    screen.onClick()
    screen.onClick()
    screen.onClick()

}


package com.linewell.modeldesgin.state

/**
*开关
* Created by ctao on 2015/9/2.
*/
case class Switch(name: String) {
    /**
     * 初始化状态
     */
    protected var state: SwitchState = OnState


    def on() = {
        print(name)
        state.on(this)
    }

    def off() = {
        print(name)
        state.off(this)
    }

    /**
     * 打开状态
     */
    case object OnState extends SwitchState {
        def on(switch: Switch) = println("已经打开")

        def off(switch: Switch) = {
            println("关闭")
            switch.state = OffState
        }
    }

    /**
     * 关闭状态
     */
    case object OffState extends SwitchState {
        def on(switch: Switch) = {
            println("打开")
            switch.state = OnState
        }

        def off(switch: Switch) = println("已经关闭")
    }


}

/**
* 密封开关状态类
*/
sealed abstract class SwitchState {
    /**
     * 打开
     * @param switch 开关
     */
    def on(switch: Switch): Unit

    /**
     * 关闭
     * @param switch 开关
     */
    def off(switch: Switch): Unit
}

package com.linewell.modeldesgin.state

/**
* 测试客户端
* Created by ctao on 2015/9/2.
*/
object  ClientSwitch extends  App{
    val s1 = Switch("1")
    val s2 = Switch("2")
    s1.on()
    s1.off()
    s1.off()
    s1.on()
    s2.on()
    s2.off()
    s2.off()
    s2.on()


}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics