modes

package
v0.5.42 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Observer

type Observer[OBJ any, ID comparable] interface {
	Update(data OBJ)
	ID() ID
}

Observer 观察者.

Example
package main

import (
	"fmt"

	"github.com/xuender/oils/modes"
)

type Customer struct {
	id int
}

func (p *Customer) ID() int {
	return p.id
}

func (p *Customer) Update(data string) {
	fmt.Printf("%d: %s\n", p.id, data)
}

type Sub struct {
	modes.Subject[string, int]
}

func main() {
	sub := &Sub{}

	sub.Register(&Customer{id: 1})
	sub.Register(&Customer{id: 2})

	sub.Notify("test1", 2)
	sub.NotifyAll("test2")

	sub.Deregister(&Customer{id: 2})
	sub.Deregister(&Customer{id: 3})

	sub.NotifyAll("test3")

}
Output:

2: test1
1: test2
2: test2
1: test3

type Subject

type Subject[OBJ any, ID comparable] struct {
	// contains filtered or unexported fields
}

Subject 主题.

func (*Subject[OBJ, ID]) Deregister

func (p *Subject[OBJ, ID]) Deregister(observer Observer[OBJ, ID])

Deregister 取消注册者.

func (*Subject[OBJ, ID]) Notify

func (p *Subject[OBJ, ID]) Notify(obj OBJ, ids ...ID)

Notify 通知.

func (*Subject[OBJ, ID]) NotifyAll

func (p *Subject[OBJ, ID]) NotifyAll(obj OBJ)

NotifyAll 通知所有.

func (*Subject[OBJ, ID]) Register

func (p *Subject[OBJ, ID]) Register(observer Observer[OBJ, ID])

Register 注册.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL