- 浏览: 13209 次
- 性别:
- 来自: 南京
文章分类
最新评论
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()
}
}
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()
}
}
发表评论
-
linux mongodb3安装
2015-10-29 15:33 261http://blog.csdn.net/cjuexuan/a ... -
scala代码重构(用递归代替var和while)
2015-09-07 19:47 380原有代码,其中使用了while循环和var /** ... -
再探设计模式之状态模式(纸牌游戏的开发)
2015-09-04 13:52 537状态模式主要用于检测和切换状态,在状态模式下状态的转化对于客户 ... -
再探设计模式之访问者模式
2015-09-03 14:44 479访问者模式的使用条件比较苛刻主要可以以用于新增访问者时只需要 ... -
scala实现访问者模式
2015-09-02 20:54 376package com.linewell.modeldesgi ... -
scala实现模版方法模式
2015-09-02 20:54 415package com.linewell.modeldesgi ... -
scala实现状态模式
2015-09-02 16:37 385package com.linewell.modeldesgi ... -
scala实现观察者模式
2015-09-02 16:35 588package com.linewell.modeldesgi ... -
scala实现迭代器模式
2015-09-01 22:56 465package com.linewell.modeldesgi ... -
scala实现备忘录模式
2015-09-01 22:54 341package com.linewell.modeldesgi ... -
scala实现中介者模式
2015-09-01 14:48 286package com.linewell.modeldesgi ... -
scala实现解释器模式
2015-08-31 20:20 316本来是看解释器模式,但感觉scala的实现的流利接口可以更优雅 ... -
scala实现命令模式
2015-08-30 21:20 269package com.linewell.modeldesgi ... -
scala实现职责链模式
2015-08-30 13:38 304package com.linewell.modeldesgi ... -
scala实现享元模式
2015-08-30 00:17 336package com.linewell.modeldesgi ... -
scala实现代理模式
2015-08-30 00:16 488package com.linewell.modeldesgi ... -
scala实现外观模式
2015-08-28 21:41 311package com.linewell.modeldesgi ... -
scala实现装饰者模式
2015-08-16 19:52 394package com.linewell.modeldesgi ... -
scala实现组合模式
2015-08-16 11:14 834package com.linewell.modeldesgi ... -
scala实现桥接模式
2015-08-16 11:13 311package com.linewell.modeldesgi ...
相关推荐
观察者模式(有时又被称为发布(publish )-订阅(Subscribe)模式、模型-视图(View)模式、源-收听者(Listener)模式或从属者模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件...
设计模式-观察者模式(讲解及其实现代码)
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新
在观察者模式中,被观察者维护观察者对象的集合,当被观察者对象变化时,它会通知观察者。观察者模式主要是用于解决对象之间一对多的关系。 类视图 实现 class Observer { public: virtual ~Observ
通过一个小的Android应用,学习一下设计模式之观察者模式;观察者模式定义对象间一种一对多的依赖关系,使得当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新。这里演示了通过改变一个数值来改变...
观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。 Observer Pattern defines a one-to-many dependency between objects so that when one object ...
观察者模式(有时又被称为模型-视图(View)模式、源-收听者(Listener)模式或从属者模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。...
定义对象间的一种一对多的依赖关系,当一个对象的状态发生变化时,所有依赖于它的对象都得到通知并被自动更新。
观察者模式(有时又被称为发布(publish )-订阅(Subscribe)模式、模型-视图(View)模式、源-收听者(Listener)模式或从属者模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件...
Observer (观察者模式) 又叫做发布/订阅(Publish/Subscribe)模式。 当一个对象的改变同时会影响其他对象的行为的时候,可以使用此设计模式。 l 主题对象 :一个需要被关注的主题对象,这个主题对象改变会影响...
观察者设计模式观察者模式(有时又被称为发布-订阅模式、模型-视图模式、源-收听者模式或从属者模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时...
观察者模式 概述 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。 适用性 1.当一个抽象模型有两个方面,其中一个方面依赖于另一方面。 将这二者封装...
定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新。
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知依赖它的对象。观察者模式属于行为型模式。 观察者模式demo java
Java 观察者模式的浅析 简单地说,观察者模式定义了一个一对多的依赖关系,让一个或多个观察者对象监察一个主题对象。这样一个主题对象在状态上的变化能够通知所有的依赖于此对象的那些观察者对象,使这些观察者...
观察者模式,定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
本文实例讲述了JavaScript设计模式之观察者模式与发布订阅模式。分享给大家供大家参考,具体如下: 学习了一段时间设计模式,当学到观察者模式和发布订阅模式的时候遇到了很大的问题,这两个模式有点类似,有点傻傻...
本文实例讲述了Python设计模式之观察者模式。分享给大家供大家参考,具体如下: 观察者模式是一个软件设计模式,一个主题对象包涵一系列依赖他的观察者,自动通知观察者的主题对象的改变,通常会调用每个观察者的一...
观察者模式在android中的小样例 通过按钮增加观察者 Observer 并且维护一个观察者list 当被观察者状态变化的时候 所有的观察者都收到了通知 并且所有的观察者都更新了自己的信息 通过update方法
c++设计模式-行为型模式-观察者模式;qt工程;c++简单源码; 观察者(Observer)模式的定义:指多个对象间存在一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。这种模式...