-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediator.go
36 lines (32 loc) · 1 KB
/
mediator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Package mediator 中介模式: 减少对象无序依赖关系, 让网状依赖变为星状依赖
package mediator
// AirportMediator 机场调度中介者
type AirportMediator interface {
CanLandAirport(aircraft Aircraft) bool // 确认是否可以降落
NotifyWaitingAircraft() // 通知等待降落的其他飞机
}
// ApproachTower 机场塔台
type ApproachTower struct {
hasFreeAirstrip bool
waitingQueue []Aircraft // 等待降落的飞机队列
}
func (a *ApproachTower) CanLandAirport(aircraft Aircraft) bool {
if a.hasFreeAirstrip {
a.hasFreeAirstrip = false
return true
}
// 没有空余的跑道,加入等待队列
a.waitingQueue = append(a.waitingQueue, aircraft)
return false
}
func (a *ApproachTower) NotifyWaitingAircraft() {
if !a.hasFreeAirstrip {
a.hasFreeAirstrip = true
}
if len(a.waitingQueue) > 0 {
// 如果存在等待降落的飞机,通知第一个降落
first := a.waitingQueue[0]
a.waitingQueue = a.waitingQueue[1:]
first.ApproachAirport()
}
}