来源于麦子学院sunny老师的在线视频
根据老师提示,略做了点扩展,但仍有些小功能没能实现
如:如何把MM jessica今天着装等展现出来,
如何把三个妹子不同的状态展现出来?
通知三个不同小伙不同妹子动向已经实现
以下xcode 6.3 以playground方式编写,实时查看代码演示效果
//: Playground - noun: a place where people can play
import UIKit
class Jessica {
var hairColor:String = ""
var diner:String = ""
init(hairColor:String,diner:String){
self.hairColor = hairColor
self.diner = diner
}
func show() {
println("today, Her hairColor is \(hairColor) and diner is \(diner)")
}
}
protocol SubMike{
}
/** <发布者 */
class Mike:SubMike {
var status:String = ""
var boys:[Boy] = []
var boys2:NSMutableArray?
func attach(newBoy:Boy){
boys.append(newBoy)
}
func del(badBOY:Boy){
boys2?.removeObject(Boy)
}
func notify() {
for boy in boys{
boy.update()
Jessica(hairColor: "rouge", diner: "bread")
/***< 没想明白为啥再增加的内容无法演示出来*/
}
}
}
/** < 订阅者*/
class Boy {
var name:String = ""
var status:String = ""
var mm:String = ""
init(name:String,sub:Mike,mm:String){
self.name = name
self.status = sub.status
self.mm = mm
}
func update(){
/** < just do it */
println("\(self.name) 同学, \(self.mm)去 \(self.status) 了")
Jessica(hairColor: "rouge", diner: "bread")
}
}
class BoyA:Boy{
override func update() {
self.name = name
self.status = "人民公园看电影"
println("\(self.name) 同学, \(self.mm)去 \(self.status) 了")
}
}
class BoyB:Boy{
override func update() {
self.name = name
self.status = "人民公园跑步"
println("\(self.name) 同学, \(self.mm)去 \(self.status) 了")
}
}
/** < 当状态发生改变了 */
var mike = Mike()
mike.status = "人民公园遛狗" //为何一定要放在前面,放后面就无结果?没想明白
mike.attach(Boy(name: "张元",sub:mike,mm:"jessica"))
Jessica(hairColor: "rouge", diner: "bread")
mike.attach(BoyB(name: "李元",sub:mike,mm:"rebecca"))
mike.attach(BoyA(name: "铜元",sub:mike,mm:"monica"))
mike.notify()