外观模式
电器接口
package facade
type Electrical interface {
On()
Off()
}
电视机
package facade
import "fmt"
type TV struct{}
func NewTV() *TV {
return &TV{}
}
func (t *TV) On() {
fmt.Println("打开电视!")
}
func (t *TV) Off() {
fmt.Println("关闭电视!")
}
func (t *TV) UpSound() {
fmt.Println("将声音调整到20")
}
电灯
package facade
import "fmt"
type Light struct{}
func NewLight() *Light {
return &Light{}
}
func (l *Light) On() {
fmt.Println("打开电灯!")
}
func (l *Light) Off() {
fmt.Println("关闭电灯!")
}
func (l *Light) Auto() {
fmt.Println("自动调节亮度")
}
外观接口
package facade
type facade interface {
work(string)
}
智能家居系统
package facade
type system struct {
tv *TV
light *Light
}
func NewFacade() facade {
return &system{
tv: NewTV(),
light: NewLight(),
}
}
func (s *system) work(msg string) {
switch msg {
case "打开":
s.tv.On()
s.tv.UpSound()
s.light.On()
s.light.Auto()
case "关闭":
s.tv.Off()
s.light.Off()
default:
fmt.Println("主人,请输入正确指令!")
}
}
测试文件
package facade
func TestFacade(t *testing.T) {
fac := NewFacade()
fac.work("打开")
fmt.Println("**********************")
fac.work("关闭")
}
标签:外观,TV,Light,fmt,模式,Println,func,facade
From: https://www.cnblogs.com/mathsmouse/p/16715936.html