Observer mode Go language implementation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Pattern Composition:

The Observer pattern contains the following roles:
Goal (Subject): The target knows its observer. There can be any number of observers observing the same target. Provides an interface for registering and deleting observer objects.
Target (ConcreteSubject): The status is deposited into each Concreteobserver object.
Observer (Observer): Defines an update interface for objects that need to be notified when a target is changed. Notifies its individual observers when its state has changed.
Specific observer (CONCRETEOBSERVER): maintains a reference to the ConcreteSubject object. Stores the state, which should be consistent with the state of the target. Implements an update interface for O B S e R v E R to align its state with the state of the target.

Effect:

Observer mode allows you to change the target and observer independently. You can reuse the target object individually without having to reuse its observers at the same time, and vice versa. It also allows you to increase the observer without altering the target and other observers.
The following are some other advantages of the Observer pattern:
1) The Observer pattern can realize the separation of the presentation layer and the data logic layer, and defines a stable message update delivery mechanism, which abstracts the update interface, so that there can be a variety of different presentation layers as the specific observer role.
2) Create an abstract coupling between the observation target and the Observer: only one object knows that it has a series of observers, each of which conforms to the simple interface of the abstract observer class. The target does not know which specific class any observer belongs to. This coupling between the target and the observer is abstract and minimal. Because the target and the observer are not tightly coupled, they can belong to different levels of abstraction in a system. A target object at a lower level can communicate with a higher-ranking observer and notify it, thus preserving the integrity of the system hierarchy. If the target is mixed with the observer, the resulting object will either traverse two levels (in violation of the hierarchy) or must be placed in one layer of the two layers (which may damage the hierarchy abstraction).
3) Support Broadcast communication: Unlike the usual request, the notification sent by the target does not need to specify its recipient. Notifications are automatically broadcast to all relevant objects that have been registered with the target object. The target object does not care how many objects are interested in itself; its sole responsibility is to inform its observers. This gives you the freedom to add and remove observers at any time. Processing or ignoring a notification depends on the observer.
4) The Observer pattern conforms to the "open and close principle" requirement.

UML diagram:


Source:

Package main import ("Container/list") type Subject interface {Attach (Observer)//Registered observer De           Tach (Observer)//Release observer Notify ()//Notify all registered observers}type Observer Interface {update (SUBJECT)//Viewer to update status} Implements SubjectType ConcreteSubject struct {observers *list. List value int} func newconcretesubject () *concretesubject {s: = new (ConcreteSubject) s.observers = list. New () return S} func (S *concretesubject) Attach (observe Observer) {//Registered observer S.observers.pushback (observe } func (S *concretesubject) DETACH (Observer observer) {//release observer for OB: = S.observers.front (); OB! = nil; o b = ob. Next () {if ob. Value. (*observer) = = &observer {s.observers.remove (ob) Break}} func (S *concre Tesubject) Notify () {///notifies all observers for OB: = S.observers.front (); OB! = nil; ob = ob. Next () {ob. Value. (Observer). Update (s)}} func (s *concretesubject) setValue (value int) {S.value = value s.notify ()} func (S *concretesubject) getValue () int {return S.value}/** * Specific observer implements Observer * */type ConcreteObserver1 struct {} func (c *concreteobserver1) Update (subject subject) {println ("ConcreteObserver1 value is", subject. ( *concretesubject). GetValue ())}/** * Specific observer implements Observer * */type ConcreteObserver2 struct {} fu NC (c *concreteobserver2) Update (subject subject) {println ("ConcreteObserver2 value is", subject. ( *concretesubject). GetValue ())} func main () {subject: = Newconcretesubject () Observer1: = new (C OncreteObserver1) Observer2: = new (ConcreteObserver2) subject. Attach (observer1) subject. Attach (Observer2) Subject.setvalue (5)}


Operation Result:

12 ConcreteObserver1  value is  5ConcreteObserver2 vaue is  5
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.