mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-25 21:26:03 +03:00
refine Alert example for state pattern
This commit is contained in:
parent
98f9de8e4d
commit
218e77d7f2
@ -4,45 +4,45 @@ package state
|
|||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
//闹装有两种状态,震铃状态,非震铃状态
|
//闹装有两种状态,震铃状态,非震铃状态
|
||||||
|
|
||||||
// MobileAlertStater provides a common interface for various states.
|
// AlertStater provides a common interface for various states.
|
||||||
type MobileAlertStater interface {
|
type AlertStater interface {
|
||||||
Alert() string
|
Alert() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// MobileAlert implements an alert depending on its state.
|
// Alert implements an alert depending on its state.
|
||||||
type MobileAlert struct {
|
type Alert struct {
|
||||||
state MobileAlertStater
|
state AlertStater
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alert returns a alert string
|
// Alert returns a alert string 代表振铃
|
||||||
func (a *MobileAlert) Alert() string {
|
func (a *Alert) Alert() string {
|
||||||
return a.state.Alert()
|
return a.state.Alert()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetState changes state
|
// SetState changes state
|
||||||
func (a *MobileAlert) SetState(state MobileAlertStater) {
|
func (a *Alert) SetState(state AlertStater) {
|
||||||
a.state = state
|
a.state = state
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMobileAlert is the MobileAlert constructor.
|
// NewAlert is the Alert constructor,默认振铃是震动
|
||||||
func NewMobileAlert() *MobileAlert {
|
func NewAlert() *Alert {
|
||||||
return &MobileAlert{state: &MobileAlertVibration{}}
|
return &Alert{state: &AlertVibration{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MobileAlertVibration implements vibration alert
|
// AlertVibration implements vibration alert
|
||||||
type MobileAlertVibration struct {
|
type AlertVibration struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alert returns a alert string
|
// Alert returns a alert string ,默认振铃
|
||||||
func (a *MobileAlertVibration) Alert() string {
|
func (a *AlertVibration) Alert() string {
|
||||||
return "vibrating humming ... vibrating humming...vibrating humming..."
|
return "vibrating humming ... vibrating humming...vibrating humming..."
|
||||||
}
|
}
|
||||||
|
|
||||||
// MobileAlertSong implements beep alert
|
// AlertSong implements beep alert
|
||||||
type MobileAlertSong struct {
|
type AlertSong struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alert returns a alert string
|
// Alert returns a new alert string 歌曲振铃,设置这个状态模式,闹钟只会唱歌
|
||||||
func (a *MobileAlertSong) Alert() string {
|
func (a *AlertSong) Alert() string {
|
||||||
return "sun rise ,get up ,get up get up..."
|
return "sun rise ,get up ,get up get up..."
|
||||||
}
|
}
|
||||||
|
@ -8,36 +8,20 @@ func TestState(t *testing.T) {
|
|||||||
"vibrating humming ... vibrating humming...vibrating humming..." +
|
"vibrating humming ... vibrating humming...vibrating humming..." +
|
||||||
"sun rise ,get up ,get up get up..."
|
"sun rise ,get up ,get up get up..."
|
||||||
|
|
||||||
mobile := NewMobileAlert()
|
//创建一个手机闹铃
|
||||||
|
mobile := NewAlert()
|
||||||
|
|
||||||
result := mobile.Alert()
|
ringsSounds := mobile.Alert()
|
||||||
result += mobile.Alert()
|
|
||||||
|
|
||||||
mobile.SetState(&MobileAlertSong{})
|
//叠加振铃声音,振铃响两遍
|
||||||
|
ringsSounds += mobile.Alert()
|
||||||
|
|
||||||
result += mobile.Alert()
|
//设置振铃的铃声
|
||||||
|
mobile.SetState(&AlertSong{})
|
||||||
|
|
||||||
if result != expect {
|
ringsSounds += mobile.Alert()
|
||||||
|
|
||||||
|
if ringsSounds != expect {
|
||||||
t.Errorf("Expect result to equal %s, but %s.\n", expect, result)
|
t.Errorf("Expect result to equal %s, but %s.\n", expect, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func TestWeeks(t *testing.T) {
|
|
||||||
ctx := NewDayContext()
|
|
||||||
todayAndNext := func() {
|
|
||||||
ctx.Today()
|
|
||||||
ctx.Next()
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 8; i++ {
|
|
||||||
todayAndNext()
|
|
||||||
}
|
|
||||||
// Output:
|
|
||||||
// Sunday
|
|
||||||
// Monday
|
|
||||||
// Tuesday
|
|
||||||
// Wednesday
|
|
||||||
// Thursday
|
|
||||||
// Friday
|
|
||||||
// Saturday
|
|
||||||
// Sunday
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user