mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-12-01 07:46:01 +03:00
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
|
// Package state is an example of the State Pattern.
|
||
|
package state
|
||
|
|
||
|
////////////////////////////////
|
||
|
//闹装有两种状态,震铃状态,非震铃状态
|
||
|
|
||
|
// MobileAlertStater provides a common interface for various states.
|
||
|
type MobileAlertStater interface {
|
||
|
Alert() string
|
||
|
}
|
||
|
|
||
|
// MobileAlert implements an alert depending on its state.
|
||
|
type MobileAlert struct {
|
||
|
state MobileAlertStater
|
||
|
}
|
||
|
|
||
|
// Alert returns a alert string
|
||
|
func (a *MobileAlert) Alert() string {
|
||
|
return a.state.Alert()
|
||
|
}
|
||
|
|
||
|
// SetState changes state
|
||
|
func (a *MobileAlert) SetState(state MobileAlertStater) {
|
||
|
a.state = state
|
||
|
}
|
||
|
|
||
|
// NewMobileAlert is the MobileAlert constructor.
|
||
|
func NewMobileAlert() *MobileAlert {
|
||
|
return &MobileAlert{state: &MobileAlertVibration{}}
|
||
|
}
|
||
|
|
||
|
// MobileAlertVibration implements vibration alert
|
||
|
type MobileAlertVibration struct {
|
||
|
}
|
||
|
|
||
|
// Alert returns a alert string
|
||
|
func (a *MobileAlertVibration) Alert() string {
|
||
|
return "vibrating humming ... vibrating humming...vibrating humming..."
|
||
|
}
|
||
|
|
||
|
// MobileAlertSong implements beep alert
|
||
|
type MobileAlertSong struct {
|
||
|
}
|
||
|
|
||
|
// Alert returns a alert string
|
||
|
func (a *MobileAlertSong) Alert() string {
|
||
|
return "sun rise ,get up ,get up get up..."
|
||
|
}
|