当前位置:首页 > 编程笔记 > 正文
已解决

《golang设计模式》第二部分·结构型模式-07-代理模式(Proxy)

来自网友在路上 175875提问 提问时间:2023-10-08 12:08:21阅读次数: 75

最佳答案 问答题库758位专家为你答疑解惑

文章目录

  • 1. 概述
    • 1.1 角色
    • 1.2 模式类图
  • 2. 代码示例
    • 2.1 设计
    • 2.2 代码
    • 2.3 示例类图

1. 概述

代理(Proxy)是用于控制客户端访问目标对象的占位对象。

需求:在调用接口实现真是主题之前需要一些提前处理。
解决:写一个代理,同样实现该接口,但是在实现方法中加上这些提前操作。

1.1 角色

  • Subject(抽象主题):定义了真实主题的接口,之后定义代理也需要实现这个接口
  • Real Subject(真实主题):定义了代理所代表的真实对象,是代理模式中被代理的对象。
  • Proxy(代理):持有对真实主题的引用,并在需要的时候创建真实主题对象,实现抽象主题接口,可以访问、控制和扩展真实主题对象。

1.2 模式类图

<style>#mermaid-svg-LzpYUsQpqaBgaOEs {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-LzpYUsQpqaBgaOEs .error-icon{fill:#552222;}#mermaid-svg-LzpYUsQpqaBgaOEs .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-LzpYUsQpqaBgaOEs .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-LzpYUsQpqaBgaOEs .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-LzpYUsQpqaBgaOEs .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-LzpYUsQpqaBgaOEs .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-LzpYUsQpqaBgaOEs .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-LzpYUsQpqaBgaOEs .marker{fill:#333333;stroke:#333333;}#mermaid-svg-LzpYUsQpqaBgaOEs .marker.cross{stroke:#333333;}#mermaid-svg-LzpYUsQpqaBgaOEs svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-LzpYUsQpqaBgaOEs g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-LzpYUsQpqaBgaOEs g.classGroup text .title{font-weight:bolder;}#mermaid-svg-LzpYUsQpqaBgaOEs .nodeLabel,#mermaid-svg-LzpYUsQpqaBgaOEs .edgeLabel{color:#131300;}#mermaid-svg-LzpYUsQpqaBgaOEs .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-LzpYUsQpqaBgaOEs .label text{fill:#131300;}#mermaid-svg-LzpYUsQpqaBgaOEs .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-LzpYUsQpqaBgaOEs .classTitle{font-weight:bolder;}#mermaid-svg-LzpYUsQpqaBgaOEs .node rect,#mermaid-svg-LzpYUsQpqaBgaOEs .node circle,#mermaid-svg-LzpYUsQpqaBgaOEs .node ellipse,#mermaid-svg-LzpYUsQpqaBgaOEs .node polygon,#mermaid-svg-LzpYUsQpqaBgaOEs .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-LzpYUsQpqaBgaOEs .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-LzpYUsQpqaBgaOEs g.clickable{cursor:pointer;}#mermaid-svg-LzpYUsQpqaBgaOEs g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-LzpYUsQpqaBgaOEs g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-LzpYUsQpqaBgaOEs .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-LzpYUsQpqaBgaOEs .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-LzpYUsQpqaBgaOEs .dashed-line{stroke-dasharray:3;}#mermaid-svg-LzpYUsQpqaBgaOEs #compositionStart,#mermaid-svg-LzpYUsQpqaBgaOEs .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs #compositionEnd,#mermaid-svg-LzpYUsQpqaBgaOEs .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs #dependencyStart,#mermaid-svg-LzpYUsQpqaBgaOEs .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs #dependencyStart,#mermaid-svg-LzpYUsQpqaBgaOEs .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs #extensionStart,#mermaid-svg-LzpYUsQpqaBgaOEs .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs #extensionEnd,#mermaid-svg-LzpYUsQpqaBgaOEs .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs #aggregationStart,#mermaid-svg-LzpYUsQpqaBgaOEs .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs #aggregationEnd,#mermaid-svg-LzpYUsQpqaBgaOEs .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-LzpYUsQpqaBgaOEs .edgeTerminals{font-size:11px;}#mermaid-svg-LzpYUsQpqaBgaOEs :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}</style>
Client
«interface»
Subject
+Service()
RealSubject
+Service()
Proxy
+RealSubject:RealSubject
+Service()

2. 代码示例

2.1 设计

  • 定义一个抽象主题sender
  • 定义两个真实主题weChatsms
    • 它们是sender的实现
    • 它们的send()方法负责发送告警
  • 定义一个代理类
    • 它也是sender的实现
    • 它的send()方法在在调用真实主题send()方法之前完成了报警前的处理
  • 调用
    • 实例化一个代理
    • 使用它的send()方法报警

2.2 代码

package mainimport ("fmt"
)// 发送告警接口
type sender interface {Send(event string)
}// 微信类
type weChat struct {
}// 微信类的发送方法
func (w *weChat) Send(event string) {fmt.Printf("微信报警:%s", event)
}// 短信类
type sms struct {
}// 短信类的报警方法
func (s *sms) Send(event string) {fmt.Printf("短信报警:%s", event)
}// 告警代理类
type AlertProxy struct {sendAlter sender
}// 告警代理类的发送方法
func (a *AlertProxy) Send(event string) {fmt.Println("报警前处理event…………")a.sendAlter.Send(event)
}
func main() {proxy := &AlertProxy{sendAlter: &sms{},}proxy.Send("内存使用率%100")}
  • 输出
报警前处理event…………
短信报警:内存使用率%100

2.3 示例类图

<style>#mermaid-svg-NjXgqtqSvQ8qKg0O {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-NjXgqtqSvQ8qKg0O .error-icon{fill:#552222;}#mermaid-svg-NjXgqtqSvQ8qKg0O .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NjXgqtqSvQ8qKg0O .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-NjXgqtqSvQ8qKg0O .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NjXgqtqSvQ8qKg0O .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NjXgqtqSvQ8qKg0O .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NjXgqtqSvQ8qKg0O .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NjXgqtqSvQ8qKg0O .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NjXgqtqSvQ8qKg0O .marker.cross{stroke:#333333;}#mermaid-svg-NjXgqtqSvQ8qKg0O svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NjXgqtqSvQ8qKg0O g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-NjXgqtqSvQ8qKg0O g.classGroup text .title{font-weight:bolder;}#mermaid-svg-NjXgqtqSvQ8qKg0O .nodeLabel,#mermaid-svg-NjXgqtqSvQ8qKg0O .edgeLabel{color:#131300;}#mermaid-svg-NjXgqtqSvQ8qKg0O .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-NjXgqtqSvQ8qKg0O .label text{fill:#131300;}#mermaid-svg-NjXgqtqSvQ8qKg0O .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-NjXgqtqSvQ8qKg0O .classTitle{font-weight:bolder;}#mermaid-svg-NjXgqtqSvQ8qKg0O .node rect,#mermaid-svg-NjXgqtqSvQ8qKg0O .node circle,#mermaid-svg-NjXgqtqSvQ8qKg0O .node ellipse,#mermaid-svg-NjXgqtqSvQ8qKg0O .node polygon,#mermaid-svg-NjXgqtqSvQ8qKg0O .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-NjXgqtqSvQ8qKg0O .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O g.clickable{cursor:pointer;}#mermaid-svg-NjXgqtqSvQ8qKg0O g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-NjXgqtqSvQ8qKg0O g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-NjXgqtqSvQ8qKg0O .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-NjXgqtqSvQ8qKg0O .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-NjXgqtqSvQ8qKg0O .dashed-line{stroke-dasharray:3;}#mermaid-svg-NjXgqtqSvQ8qKg0O #compositionStart,#mermaid-svg-NjXgqtqSvQ8qKg0O .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O #compositionEnd,#mermaid-svg-NjXgqtqSvQ8qKg0O .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O #dependencyStart,#mermaid-svg-NjXgqtqSvQ8qKg0O .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O #dependencyStart,#mermaid-svg-NjXgqtqSvQ8qKg0O .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O #extensionStart,#mermaid-svg-NjXgqtqSvQ8qKg0O .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O #extensionEnd,#mermaid-svg-NjXgqtqSvQ8qKg0O .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O #aggregationStart,#mermaid-svg-NjXgqtqSvQ8qKg0O .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O #aggregationEnd,#mermaid-svg-NjXgqtqSvQ8qKg0O .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NjXgqtqSvQ8qKg0O .edgeTerminals{font-size:11px;}#mermaid-svg-NjXgqtqSvQ8qKg0O :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}</style>
«interface»
sendAlert
+Send()
sms
+Send()
weChat
+Send()
client
AlertProxy
+Send()
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"《golang设计模式》第二部分·结构型模式-07-代理模式(Proxy)":http://eshow365.cn/6-17267-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!