内容简介:Adapter模式也被称为Wrapper模式,有以下两种:
适配器模式
Adapter模式也被称为Wrapper模式,有以下两种:
- 类适配器(使用继承)
实现目标接口,继承被适配类
- 对象适配器(使用委托)
继承目标类,依赖被适配类
参考 http://blog.51cto.com/liuxp08...
package main
import (
"fmt"
)
func main() {
duck := &MallardDuck{}
turkey := &WildTurkey{}
turkeyAdapter := NewTurkeyAdapter(turkey)
fmt.Println("The Turkey says...")
turkey.gobble()
turkey.fly()
fmt.Println("The Duck says...")
duck.quack()
duck.fly()
fmt.Println("The Turkey Adapter says...")
turkeyAdapter.quack()
turkeyAdapter.fly()
}
type Duck interface {
quack()
fly()
}
type Turkey interface {
gobble()
fly()
}
type MallardDuck struct {
}
func (*MallardDuck) quack() {
fmt.Println("Quark...")
}
func (*MallardDuck) fly() {
fmt.Println("flying...")
}
type WildTurkey struct {
}
func (*WildTurkey) gobble() {
fmt.Println("Gobble...")
}
func (*WildTurkey) fly() {
fmt.Println("flying a short distance")
}
type TurkeyAdapter struct {
turkey Turkey
}
func NewTurkeyAdapter(turkey Turkey) *TurkeyAdapter {
return &TurkeyAdapter{turkey}
}
func (this *TurkeyAdapter) quack() {
this.turkey.gobble()
}
func (this *TurkeyAdapter) fly() {
for i := 0; i < 5; i++ {
this.turkey.fly()
}
}
适配器TurkeyAdpater,持有turkey Turkey,实现Duck接口。
代理模式
uml:
https://design-patterns.readt...
代理模式中的成员构成:
- Subject(主体)
- Proxy (代理人)
- RealSubject(实际的主体)
- Client (请求者)
代理的目的是在目标对象方法的基础上作增强,这种增强的本质通常就是对目标对象的方法进行拦截和过滤。
以上所述就是小编给大家介绍的《gof23结构类模式(golang版)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Design Accessible Web Sites
Jeremy Sydik / Pragmatic Bookshelf / 2007-11-05 / USD 34.95
It's not a one-browser web anymore. You need to reach audiences that use cell phones, PDAs, game consoles, or other "alternative" browsers, as well as users with disabilities. Legal requirements for a......一起来看看 《Design Accessible Web Sites》 这本书的介绍吧!