标签:
外观模式:提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。——《HEAD FIRST 设计模式》
我的golang代码:
package facade import "fmt" type PopcornPopper struct { } func (f *PopcornPopper) On() { fmt.Println("popper on!") } func (f *PopcornPopper) Off() { fmt.Println("popper off!") } func (f *PopcornPopper) Pop() { fmt.Println("popper pop!") } type TheaterLights struct { } func (f *TheaterLights) Dim() { fmt.Println("theaterlights dim!") } func (f *TheaterLights) On() { fmt.Println("theaterlights on!") } type Screen struct { } func (f *Screen) Down() { fmt.Println("screen down!") } func (f *Screen) Up() { fmt.Println("screen up!") } type Projector struct { } func (f *Projector) On() { fmt.Println("projector on!") } func (f *Projector) Off() { fmt.Println("projector off!") } func (f *Projector) WideScreenMode() { fmt.Println("projector widescreenmode!") } type Amplifier struct { } func (f *Amplifier) On() { fmt.Println("amplifier on!") } func (f *Amplifier) Off() { fmt.Println("amplifier off!") } func (f *Amplifier) SetDvd() { fmt.Println("amplifier setdvd!") } func (f *Amplifier) SetSurroundSound() { fmt.Println("amplifier setsurroundsound!") } func (f *Amplifier) SetVolume() { fmt.Println("amplifier setvolume!") } type DvdPlayer struct { } func (f *DvdPlayer) On() { fmt.Println("dvd on!") } func (f *DvdPlayer) Play() { fmt.Println("dvd play!") } func (f *DvdPlayer) Stop() { fmt.Println("dvd stop!") } func (f *DvdPlayer) Eject() { fmt.Println("dvd eject!") } func (f *DvdPlayer) Off() { fmt.Println("dvd off!") } type HomeTheaterFacade struct { amp Amplifier dvd DvdPlayer projector Projector light TheaterLights screen Screen popper PopcornPopper } func NewHomeTheaterFacade(a Amplifier, d DvdPlayer, p Projector, l TheaterLights, s Screen, po PopcornPopper) *HomeTheaterFacade { r := new(HomeTheaterFacade) r.amp = a r.dvd = d r.projector = p r.light = l r.screen = s r.popper = po return r } func (f *HomeTheaterFacade) WatchMovie() { fmt.Println("watch movie!") f.popper.On() f.popper.Pop() f.light.Dim() f.screen.Down() f.projector.On() f.projector.WideScreenMode() f.amp.On() f.amp.SetDvd() f.amp.SetSurroundSound() f.amp.SetVolume() f.dvd.On() f.dvd.Play() } func (f *HomeTheaterFacade) EndMovie() { fmt.Println("end movie!") f.popper.Off() f.light.On() f.screen.Up() f.projector.Off() f.amp.Off() f.dvd.Stop() f.dvd.Eject() f.dvd.Off() }
个人感悟:待留。
标签:
原文地址:http://www.cnblogs.com/foolbread/p/4464478.html