GithubHelp home page GithubHelp logo

observer's Introduction

observer

设计模式之观察者模式

快速上手

可以查看observer_test,了解怎么使用

package main

import (
	"fmt"
	"github.com/ranxx/observer"
)

func main() {
	obs := observer.NewObserver[int64]()
	obs.SubscribeByTopicFunc("func_arg_slice", func(a string, b ...int) {
		fmt.Println(a, b)
	})
	obs.Publish("func_arg_slice", -99, "axing", 1, 2, 3, 4, 5)
	obs.Wait()
}

函数方式

package main

import (
	"fmt"
	"github.com/ranxx/observer"
)

func main() {
	obs := observer.NewObserver[interface{}]()
	obs.SubscribeByTopicFunc("func_arg_slice", func(a string, b ...int) {
		fmt.Println(a, b)
	})
	obs.SyncPublish("func_arg_slice", nil, "axing", 1, 2, 3, 4, 5)
	obs.SyncPublishWithRet("func_arg_slice", nil, func(){
		fmt.Println("阿星 执行完毕")
	}, "阿星", 1, 2, 3, 4, 5)
}

结构体方式

package main

import (
	"fmt"
	"github.com/ranxx/observer"
)

type person struct {
	event string `topic:"pain" notice:"Say"`
	name  string `json:"name"`
}

func (p *person) Say() {
	fmt.Printf("name:%s 疼痛\n", p.name)
}

func main() {
	obs := observer.NewObserver[string]()
	obs.Subscribe(&person{name:"小明"})
	obs.Subscribe(&person{name:"小红"})
	obs.Publish("pain", "common")
	obs.Wait()
}

observer's People

Contributors

ranxx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.