企业宣传网站设计论文,163免费注册入口,上海网站建站服务,wordpress中调用文章文章目录 1. 概念1.1 角色1.2 类图 2. 代码示例2.1 代码2.2 类图 1. 概念
观察者#xff08;Observer#xff09;指当目标对象状态发生变化后#xff0c;对状态变化事件进行响应或处理的对象。
1.1 角色
Subject#xff08;抽象主题#xff09;#xff1a; 它可以有多… 文章目录 1. 概念1.1 角色1.2 类图 2. 代码示例2.1 代码2.2 类图 1. 概念
观察者Observer指当目标对象状态发生变化后对状态变化事件进行响应或处理的对象。
1.1 角色
Subject抽象主题 它可以有多个观察者并将所有观察者对象的引用保存在一个集合里被观察者提供一个接口可以增加和删除观察者角色 ConcreteSubject具体主题 将有关状态存入具体观察者对象在主题发生改变时给所有的观察者发出通知 Observer抽象观察者 为所有的具体观察者定义一个更新接口在收到主题的通知时能够及时的更新自己 ConcreteObserver具体观察者 实现抽象观察者角色定义的更新接口以便使本身的状态与主题状态相协调。如果需要具体观察者角色可以保存一个指向具体主题角色的引用。
1.2 类图 #mermaid-svg-CxI5GUrUYhRa6wHp {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-CxI5GUrUYhRa6wHp .error-icon{fill:#552222;}#mermaid-svg-CxI5GUrUYhRa6wHp .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-CxI5GUrUYhRa6wHp .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-CxI5GUrUYhRa6wHp .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-CxI5GUrUYhRa6wHp .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-CxI5GUrUYhRa6wHp .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-CxI5GUrUYhRa6wHp .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-CxI5GUrUYhRa6wHp .marker{fill:#333333;stroke:#333333;}#mermaid-svg-CxI5GUrUYhRa6wHp .marker.cross{stroke:#333333;}#mermaid-svg-CxI5GUrUYhRa6wHp svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-CxI5GUrUYhRa6wHp g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-CxI5GUrUYhRa6wHp g.classGroup text .title{font-weight:bolder;}#mermaid-svg-CxI5GUrUYhRa6wHp .nodeLabel,#mermaid-svg-CxI5GUrUYhRa6wHp .edgeLabel{color:#131300;}#mermaid-svg-CxI5GUrUYhRa6wHp .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-CxI5GUrUYhRa6wHp .label text{fill:#131300;}#mermaid-svg-CxI5GUrUYhRa6wHp .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-CxI5GUrUYhRa6wHp .classTitle{font-weight:bolder;}#mermaid-svg-CxI5GUrUYhRa6wHp .node rect,#mermaid-svg-CxI5GUrUYhRa6wHp .node circle,#mermaid-svg-CxI5GUrUYhRa6wHp .node ellipse,#mermaid-svg-CxI5GUrUYhRa6wHp .node polygon,#mermaid-svg-CxI5GUrUYhRa6wHp .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-CxI5GUrUYhRa6wHp .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-CxI5GUrUYhRa6wHp g.clickable{cursor:pointer;}#mermaid-svg-CxI5GUrUYhRa6wHp g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-CxI5GUrUYhRa6wHp g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-CxI5GUrUYhRa6wHp .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-CxI5GUrUYhRa6wHp .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-CxI5GUrUYhRa6wHp .dashed-line{stroke-dasharray:3;}#mermaid-svg-CxI5GUrUYhRa6wHp #compositionStart,#mermaid-svg-CxI5GUrUYhRa6wHp .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp #compositionEnd,#mermaid-svg-CxI5GUrUYhRa6wHp .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp #dependencyStart,#mermaid-svg-CxI5GUrUYhRa6wHp .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp #dependencyStart,#mermaid-svg-CxI5GUrUYhRa6wHp .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp #extensionStart,#mermaid-svg-CxI5GUrUYhRa6wHp .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp #extensionEnd,#mermaid-svg-CxI5GUrUYhRa6wHp .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp #aggregationStart,#mermaid-svg-CxI5GUrUYhRa6wHp .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp #aggregationEnd,#mermaid-svg-CxI5GUrUYhRa6wHp .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-CxI5GUrUYhRa6wHp .edgeTerminals{font-size:11px;}#mermaid-svg-CxI5GUrUYhRa6wHp :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Subject -observer:List Add(o:Observer) NotifyObserver() ChangeState() ConcreteSubject ChangeState() «interface» Observer Update(s:State) ConcreteObserverA Update(s:State) ConcreteObserverB Update(s:State) 2. 代码示例
2.1 代码
代码
package mainimport fmt//定义抽象主题它是观察者的聚合
type Subject struct{ observers []Observer
}//定义一个方法为抽象主题添加观察者
func (c *Subject) Attach(o ...Observer) {c.observers append(c.observers, o...)
}//定义实际主题继承抽象主题它是实际的被观察者。
type ConcreteSubject struct {Subject Subjectcontext string}//依次通知观察者
func (c *ConcreteSubject) notify() {for _, o : range c.Subject.observers {o.Update(c)}}//具体抽象主题的同时发送通知给观察者
func (c *ConcreteSubject) UpdateContext(context string) {c.context contextc.notify()
}//定义抽象观察者
type Observer interface {Update(*ConcreteSubject)
}//定义具体观察者
type ConcreteObserverA struct {name string
}//更新观察者
func (r *ConcreteObserverA) Update(c *ConcreteSubject) {fmt.Printf(%s receive %q\n, r.name, c.context)
} //定义具体观察者
type ConcreteObserverB struct {name string
}//更新观察者
func (r *ConcreteObserverB) Update(c *ConcreteSubject) {fmt.Printf(%s receive %q\n, r.name, c.context)
} //定义具体观察者
type ConcreteObserverC struct {name string
}//更新观察者
func (r *ConcreteObserverC) Update(c *ConcreteSubject) {fmt.Printf(%s receive %q\n, r.name, c.context)
} func main() {//实例化抽象主题subject : Subject{observers: make([]Observer, 0),}//实例化3观察者concreteObserverA : ConcreteObserverA{name: concreteObserverA,}concreteObserverB : ConcreteObserverB{name: concreteObserverB,}concreteObserverC : ConcreteObserverC{name: concreteObserverC,}//将3个观察者加入抽象主题subject.Attach(concreteObserverA,concreteObserverB,concreteObserverC)//实例化具体主题concreteSubject : ConcreteSubject{Subject: subject,}//更新具体主题它将通知所有观察者concreteSubject.UpdateContext(updata context)
}输出
concreteObserverA receive updata context
concreteObserverB receive updata context
concreteObserverC receive updata context2.2 类图 #mermaid-svg-3O9VrFaNJnm5T67t {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-3O9VrFaNJnm5T67t .error-icon{fill:#552222;}#mermaid-svg-3O9VrFaNJnm5T67t .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-3O9VrFaNJnm5T67t .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-3O9VrFaNJnm5T67t .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-3O9VrFaNJnm5T67t .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-3O9VrFaNJnm5T67t .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-3O9VrFaNJnm5T67t .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-3O9VrFaNJnm5T67t .marker{fill:#333333;stroke:#333333;}#mermaid-svg-3O9VrFaNJnm5T67t .marker.cross{stroke:#333333;}#mermaid-svg-3O9VrFaNJnm5T67t svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-3O9VrFaNJnm5T67t g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-3O9VrFaNJnm5T67t g.classGroup text .title{font-weight:bolder;}#mermaid-svg-3O9VrFaNJnm5T67t .nodeLabel,#mermaid-svg-3O9VrFaNJnm5T67t .edgeLabel{color:#131300;}#mermaid-svg-3O9VrFaNJnm5T67t .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-3O9VrFaNJnm5T67t .label text{fill:#131300;}#mermaid-svg-3O9VrFaNJnm5T67t .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-3O9VrFaNJnm5T67t .classTitle{font-weight:bolder;}#mermaid-svg-3O9VrFaNJnm5T67t .node rect,#mermaid-svg-3O9VrFaNJnm5T67t .node circle,#mermaid-svg-3O9VrFaNJnm5T67t .node ellipse,#mermaid-svg-3O9VrFaNJnm5T67t .node polygon,#mermaid-svg-3O9VrFaNJnm5T67t .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-3O9VrFaNJnm5T67t .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-3O9VrFaNJnm5T67t g.clickable{cursor:pointer;}#mermaid-svg-3O9VrFaNJnm5T67t g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-3O9VrFaNJnm5T67t g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-3O9VrFaNJnm5T67t .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-3O9VrFaNJnm5T67t .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-3O9VrFaNJnm5T67t .dashed-line{stroke-dasharray:3;}#mermaid-svg-3O9VrFaNJnm5T67t #compositionStart,#mermaid-svg-3O9VrFaNJnm5T67t .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t #compositionEnd,#mermaid-svg-3O9VrFaNJnm5T67t .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t #dependencyStart,#mermaid-svg-3O9VrFaNJnm5T67t .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t #dependencyStart,#mermaid-svg-3O9VrFaNJnm5T67t .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t #extensionStart,#mermaid-svg-3O9VrFaNJnm5T67t .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t #extensionEnd,#mermaid-svg-3O9VrFaNJnm5T67t .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t #aggregationStart,#mermaid-svg-3O9VrFaNJnm5T67t .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t #aggregationEnd,#mermaid-svg-3O9VrFaNJnm5T67t .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3O9VrFaNJnm5T67t .edgeTerminals{font-size:11px;}#mermaid-svg-3O9VrFaNJnm5T67t :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Subject []Observe observers Attach(o ...Observer) ConcreteSubject Subject:Subject context:String Notify() UpdateContext(context string) «interface» Observer Update(c *ConcreteSubject) ConcreteObserverA Name:String Update(c *ConcreteSubject) ConcreteObserverB Name:String Update(c *ConcreteSubject) ConcreteObserverC Name:String Update(c *ConcreteSubject)
文章转载自: http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.jphxt.cn.gov.cn.jphxt.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.pbbzn.cn.gov.cn.pbbzn.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.amonr.com.gov.cn.amonr.com http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.lhxkl.cn.gov.cn.lhxkl.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.lkbkd.cn.gov.cn.lkbkd.cn http://www.morning.zjrnq.cn.gov.cn.zjrnq.cn http://www.morning.wngpq.cn.gov.cn.wngpq.cn http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.rlpmy.cn.gov.cn.rlpmy.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.bccls.cn.gov.cn.bccls.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.ndngj.cn.gov.cn.ndngj.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.qgjwx.cn.gov.cn.qgjwx.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.mnlk.cn.gov.cn.mnlk.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.sqnxk.cn.gov.cn.sqnxk.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.tpxgm.cn.gov.cn.tpxgm.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.dzzjq.cn.gov.cn.dzzjq.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.ptxwg.cn.gov.cn.ptxwg.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn