标签:
适配器模式:将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。——《HEAD FIRST 设计模式》
我的golang代码:
package adapter import "fmt" type Duck interface { Quack() Fly() } type Turkey interface { Gobble() Fly() } type MallardDuck struct { } func (a *MallardDuck) Quack() { fmt.Println("Quack") } func (a *MallardDuck) Fly() { fmt.Println("I‘m flying") } type WildTurkey struct { } func (a *WildTurkey) Gobble() { fmt.Println("Gobble gobble") } func (a *WildTurkey) Fly() { fmt.Println("I‘m flying a short distance") } type TurkeyAdapter struct { turkey Turkey } func NewTurkeyAdapter(t Turkey) *TurkeyAdapter { r := new(TurkeyAdapter) r.turkey = t return r } func (a *TurkeyAdapter) Quack() { a.turkey.Gobble() } func (a *TurkeyAdapter) Fly() { a.turkey.Fly() }
个人感悟:待留。
标签:
原文地址:http://www.cnblogs.com/foolbread/p/4464236.html