再探设计模式之观察者模式(股票通知系统)

package com.linewell.modeldesign.observer

import scala.collection.mutable.ArrayBuffer
import scala.util.Random

/**
* 某软件公司欲开发一款实时在线证劵软件,该软件需要提供以下功能:
* 当股票购买者购买的某只证劵价格幅度达到5%时,系统将自动的发送通知
* 包括新价格给购买该股票的所有股民
*
*
* 设计思路:
* 采用观察者模式
* 股票是目标类
* 股民是抽象观察者
* 具体人是具体的观察者
* 须在目标类中注册观察者
*
* Created by ctao on 2015/9/4.
*/

/**
* 抽象目标:证券
* @param name 名字
* @param price 价格
*/
abstract class Securities(val name: String, var price: Double) {
    /**
     * 观察者集合
     */
    private var investors = new ArrayBuffer[Investor]()

    /**
     * 注册观察者
     * @param investor 观察者
     */
    def addInvestor(investor: Investor) = investor match {
        case i if investors.exists(i.eq(_)) => println("您已购买该证券")
        case _ => println()
            investors += investor
    }

    /**
     * 注销观察者
     * @param investor 观察者
     * @return
     */
    def removeInvestor(investor: Investor) = investor match {
        case i if investors.exists(i.eq(_)) =>println()
            investors -= investor
        case _ => println("您尚未购买该证券")
    }

    /**
     * 调整价格,调整幅度为Random一个数,调整可能为上涨也可能为下跌
     */
    def changePrice(): Unit = {
        val range = Random.nextInt((price * 2).toInt) * (if (Random.nextBoolean()) 0.1 else -0.1)
        range.abs / price * 100 match {
            case r if r >= 10 && range > 0 =>
                val newPrice = price * 1.1
                val message = s"${name}涨停了,原价格$price,现价格$newPrice"
                notifyInvestors(message)
                price = newPrice
            case r if r >= 10 && range < 0 =>
                val newPrice = price * 0.9
                val message = s"${name}跌停了,原价格$price,现价格$newPrice"
                notifyInvestors(message)
                price = newPrice
            case r if r > 5 && range > 0 =>
                val newPrice = price + range
                val message = s"${name}上涨了$r%,原价格$price,现价格$newPrice"
                notifyInvestors(message)
                price = newPrice
            case r if r > 5 && range < 0 =>
                val newPrice = price + range
                val message = s"${name}下跌了$r%,原价格$price,现价格$newPrice"
                notifyInvestors(message)
                price = newPrice

            case _ => price = price + range
                println(s"${name}原价格${price-range},现价格$price")

        }

    }

    /**
     * 通知观察者
     * @param message 消息
     */
    def notifyInvestors(message: String): Unit = {
        investors.foreach(_.receiveMessage(message))
    }

}

/**
* 股票,具体目标类
* @param name 名字
* @param price 价格
*/
class Stock(name: String, price: Double) extends Securities(name, price)
package com.linewell.modeldesign.observer

/**
* 投资人:抽象观察者
* Created by ctao on 2015/9/4.
*/
abstract class Investor(name: String) {
    /**
     * 买入证券:注册观察者
     * @param securities 证券
     */
    def buy(securities: Securities) = {
        print(s"${name}购买${securities.name}")
        securities.addInvestor(this)
    }

    /**
     * 出售证券:注销观察者
     * @param securities 证券
     */
    def sell(securities: Securities) = {
        print(s"${name}出售${securities.name}")
        securities.removeInvestor(this)
    }

    /**
     * 接收观察者的消息
     * @param message 消息
     */
    def receiveMessage(message: String) = println(s"${name}收到消息:$message")
}


/**
* 股民:具体观察者
* @param name 姓名
*/
case class StockInvestor(name: String) extends Investor(name)


package com.linewell.modeldesign.observer

/**
* 观察者测试客户端
* Created by ctao on 2015/9/5.
*/
object Client extends App {
    /**
     * 股票投资者
     */
    val ct: Investor = StockInvestor("ct")
    val cjx: Investor = StockInvestor("cjx")
    val pk: Investor = StockInvestor("pk")

    /**
     * 股票,发行价
     */
    val lineWell: Securities = new Stock("lineWell",30)
    /**
     * 买入
     */
    ct.buy(lineWell)
    ct.buy(lineWell)
    cjx.buy(lineWell)
    pk.buy(lineWell)
    cjx.sell(lineWell)
    cjx.sell(lineWell)

    /**
     * 持续50天的股市变化
     */
    for(i <- 1 to 50){
        /**
         * 每天会调整一次价格
         */
        lineWell.changePrice()
    }

}


猜你喜欢

转载自cjuexuan.iteye.com/blog/2240685