awesome-patterns/behavioral/registry
2020-05-11 21:28:07 +08:00
..
LICENSE Create LICENSE 2020-05-11 21:25:25 +08:00
README.md Update README.md 2020-05-11 21:28:07 +08:00
registry.go Create registry.go 2020-05-11 21:23:42 +08:00

go-registry

Merged :https://github.com/Kaezon/go-registry

A generic interface for the registry pattern

Because adding a registry pattern to each project can result in a lot of code duplication, I decided to write a basic registry to slim down other projects.

The registry itself accepts empty interface for the values to be registered, so there's no need to worry about type here. I do realize that this is not necessarily the best answer to making a general purpose type, so I plan on adding some kind of type checking if I can find a good way to do so.


Usage

import (
    "fmt"

    "github.com/crazybber/awesome-patterns/behavioral/registry"
)

var myReg registry.Registry

myReg = registry.New()

newID := myReg.Register(1)

myReg.RegisterName(2, "second")

fmt.Println(myReg.Get(newID))
fmt.Println(myReg.Get("second"))

myReg.Deregister(newID)
myReg.Deregister("second")