本次分享结构型模式,主要包括:
1.代理模式
| package proxy |
| |
| import "fmt" |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type Modem struct {} |
| |
| func (t Modem) Access(url string) { |
| fmt.Println("正常访问:" + url) |
| } |
| |
| type Proxy struct { |
| BlackList []string |
| M Modem |
| } |
| |
| |
| func (t Proxy) FilterAccess(url string) { |
| for _, v := range t.BlackList { |
| if v == url { |
| fmt.Println("禁止访问:" + url) |
| return |
| } |
| } |
| t.M.Access(url) |
| } |
| |
2.装饰器模式
| package decorator |
| |
| import "fmt" |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type Showable interface { |
| Show() |
| } |
| |
| |
| type Girl struct {} |
| |
| func (t *Girl) Show() { |
| fmt.Printf("未化妆 girl") |
| } |
| |
| |
| type MakeUPDecorator struct { |
| Showable |
| } |
| |
| func (t *MakeUPDecorator) Show() { |
| fmt.Print("打粉底(") |
| t.Showable.Show() |
| fmt.Print(")") |
| } |
| |
| type LipDecorator struct { |
| Showable |
| } |
| |
| func (t *LipDecorator) Show() { |
| fmt.Print("涂口红(") |
| t.Showable.Show() |
| fmt.Print(")\n") |
| } |
3.适配器模式
| package adapter |
| |
| import "fmt" |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type USB interface { |
| Work() |
| } |
| |
| |
| func Charge(usb USB) { |
| usb.Work() |
| } |
| |
| |
| type TypeC struct {} |
| |
| func (t TypeC) IPhone() { |
| fmt.Println("Charge IPhone") |
| } |
| |
| |
| type AdapterTypeC struct { |
| TypeC |
| } |
| |
| func (t AdapterTypeC) Work() { |
| t.IPhone() |
| } |
| |
| type Micro struct {} |
| |
| func (t Micro) Work() { |
| fmt.Println("Charge Android") |
| } |
4.享元模式
| package flyWeight |
| |
| import ( |
| "fmt" |
| "sync" |
| ) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type Flyweight interface { |
| Operation() |
| } |
| |
| |
| type ConcreteFlyweight struct { |
| Key string |
| } |
| |
| func (t ConcreteFlyweight) Operation() { |
| fmt.Println(t.Key) |
| } |
| |
| |
| type FlyweightFactory struct { |
| mu sync.Mutex |
| Flys map[string]Flyweight |
| } |
| |
| func (t FlyweightFactory) Get(key string) Flyweight { |
| t.mu.Lock() |
| defer t.mu.Unlock() |
| |
| if f, ok := t.Flys[key]; !ok { |
| f = ConcreteFlyweight{Key: key} |
| t.Flys[key] = f |
| return f |
| } else { |
| return f |
| } |
| } |
| |
| func (t FlyweightFactory) Len() int { |
| return len(t.Flys) |
| } |
| |
| func (t FlyweightFactory) Put() { |
| |
| } |
| |
5.测试
| ... |
| fmt.Println(strings.Repeat("-", 10) + "结构型模式" + strings.Repeat("-", 10)) |
| |
| fmt.Println("proxy mode testing...") |
| p := proxy.Proxy{ |
| BlackList: []string{"www.bilibili.com", "www.youku.com"}, |
| M: proxy.Modem{}, |
| } |
| p.FilterAccess("www.baidu.com") |
| p.FilterAccess("www.bilibili.com") |
| fmt.Printf("+%s+\n", strings.Repeat("-", 50)) |
| |
| |
| fmt.Println("decorator mode testing...") |
| girl := decorator.Girl{} |
| girl.Show() |
| fmt.Println(" 化妆后:") |
| l := decorator.LipDecorator{Showable: &decorator.MakeUPDecorator{&girl}} |
| l.Show() |
| fmt.Printf("+%s+\n", strings.Repeat("-", 50)) |
| |
| |
| fmt.Println("decorator mode testing...") |
| adapter.Charge(adapter.Micro{}) |
| adapter.Charge(adapter.AdapterTypeC{}) |
| fmt.Printf("+%s+\n", strings.Repeat("-", 50)) |
| |
| |
| fmt.Println("flyweight mode testing...") |
| fly := flyWeight.FlyweightFactory{Flys: make(map[string]flyWeight.Flyweight)} |
| fly.Get("A").Operation() |
| fly.Get("A").Operation() |
| fly.Get("B").Operation() |
| fly.Get("C").Operation() |
| fmt.Println("flyweight len:", fly.Len()) |
| fmt.Printf("+%s+\n", strings.Repeat("-", 50)) |
| ... |
参考文档:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具