package com.linewell.modeldesgin.observer
import scala.collection.mutable.ArrayBuffer
/**
* 观察者模式
* Created by ctao on 2015/9/2.
* 抽象观察类
*/
abstract class Observer(var name: String) {
/**
* 支援盟友的方法
*/
def help(): Unit
/**
* 被攻击的方法
* @param allControlCenter 目标类
*/
def beAttacked(allControlCenter: AllControlCenter): Unit
}
/**
* 具体观察者
* @param name 姓名
*/
class Player(name: String) extends Observer(name) {
/**
* 支援方法
*/
override def help(): Unit = println(s"坚持住,${name}来救你!")
/**
*
* @param allControlCenter 目标类
*/
override def beAttacked(allControlCenter: AllControlCenter): Unit = {
println(s"${name}被攻击")
allControlCenter.notifyObserver(name)
}
}
/**
* 抽象目标类,战队控制中心
* @param allyName 战队名称
*/
abstract class AllControlCenter(allyName: String) {
/**
* 观察者队列
*/
protected var players = new ArrayBuffer[Observer]()
/**
* 添加观察者
* @param observer 观察者
*/
def join(observer: Observer):Unit = observer match {
case o if players.exists(_.eq(o)) => println(s"${o.name}已加入${allyName}战队")
case o: Observer => println(s"${o.name}加入${allyName}战队")
players += observer
case _ => println("异常")
}
/**
* 删除观察者
* @param observer 观察者
*/
def quit(observer: Observer):Unit = observer match {
case o if players.exists(o.eq(_)) => println(s"${o.name}退出${allyName}战队")
players -= observer
case o if !players.exists(_.eq(o)) => println(s"${o.name}已退出${allyName}战队")
case _ => println("异常")
}
/**
* 通知方法
* @param oName 观察者姓名
*/
def notifyObserver(oName: String): Unit
}
/**
* 具体目标类
* @param name 战队名
*/
class ConcreteAllyControlCenter(name: String) extends AllControlCenter(name) {
override def notifyObserver(oName: String): Unit = oName match {
case o if players.exists(_.name == o) => println(s"${name}战队紧急通知,盟友${oName}遭到攻击")
players.filterNot(_.name == oName).foreach(_.help())
case _ => println(s"$oName,您已不是战队${name}成员,无法通知战队您被攻击的消息")
}
}
package com.linewell.modeldesgin.observer
/**
* 测试客户端
* Created by ctao on 2015/9/2.
*/
object Client extends App {
/**
* 目标
*/
val acc: AllControlCenter = new ConcreteAllyControlCenter("justSo")
/**
* 观察者对象
*/
val play1: Observer = new Player("a")
val play2: Observer = new Player("b")
val play3: Observer = new Player("c")
val play4: Observer = new Player("d")
val play5: Observer = new Player("e")
/**
*
* 注册
*/
acc.join(play1)
acc.join(play2)
acc.join(play3)
acc.join(play3)
acc.join(play4)
acc.join(play5)
acc.quit(play1)
acc.quit(play1)
play1.beAttacked(acc)
}
import scala.collection.mutable.ArrayBuffer
/**
* 观察者模式
* Created by ctao on 2015/9/2.
* 抽象观察类
*/
abstract class Observer(var name: String) {
/**
* 支援盟友的方法
*/
def help(): Unit
/**
* 被攻击的方法
* @param allControlCenter 目标类
*/
def beAttacked(allControlCenter: AllControlCenter): Unit
}
/**
* 具体观察者
* @param name 姓名
*/
class Player(name: String) extends Observer(name) {
/**
* 支援方法
*/
override def help(): Unit = println(s"坚持住,${name}来救你!")
/**
*
* @param allControlCenter 目标类
*/
override def beAttacked(allControlCenter: AllControlCenter): Unit = {
println(s"${name}被攻击")
allControlCenter.notifyObserver(name)
}
}
/**
* 抽象目标类,战队控制中心
* @param allyName 战队名称
*/
abstract class AllControlCenter(allyName: String) {
/**
* 观察者队列
*/
protected var players = new ArrayBuffer[Observer]()
/**
* 添加观察者
* @param observer 观察者
*/
def join(observer: Observer):Unit = observer match {
case o if players.exists(_.eq(o)) => println(s"${o.name}已加入${allyName}战队")
case o: Observer => println(s"${o.name}加入${allyName}战队")
players += observer
case _ => println("异常")
}
/**
* 删除观察者
* @param observer 观察者
*/
def quit(observer: Observer):Unit = observer match {
case o if players.exists(o.eq(_)) => println(s"${o.name}退出${allyName}战队")
players -= observer
case o if !players.exists(_.eq(o)) => println(s"${o.name}已退出${allyName}战队")
case _ => println("异常")
}
/**
* 通知方法
* @param oName 观察者姓名
*/
def notifyObserver(oName: String): Unit
}
/**
* 具体目标类
* @param name 战队名
*/
class ConcreteAllyControlCenter(name: String) extends AllControlCenter(name) {
override def notifyObserver(oName: String): Unit = oName match {
case o if players.exists(_.name == o) => println(s"${name}战队紧急通知,盟友${oName}遭到攻击")
players.filterNot(_.name == oName).foreach(_.help())
case _ => println(s"$oName,您已不是战队${name}成员,无法通知战队您被攻击的消息")
}
}
package com.linewell.modeldesgin.observer
/**
* 测试客户端
* Created by ctao on 2015/9/2.
*/
object Client extends App {
/**
* 目标
*/
val acc: AllControlCenter = new ConcreteAllyControlCenter("justSo")
/**
* 观察者对象
*/
val play1: Observer = new Player("a")
val play2: Observer = new Player("b")
val play3: Observer = new Player("c")
val play4: Observer = new Player("d")
val play5: Observer = new Player("e")
/**
*
* 注册
*/
acc.join(play1)
acc.join(play2)
acc.join(play3)
acc.join(play3)
acc.join(play4)
acc.join(play5)
acc.quit(play1)
acc.quit(play1)
play1.beAttacked(acc)
}