diff --git a/README.md b/README.md index 747f404..f848885 100644 --- a/README.md +++ b/README.md @@ -1,63 +1,116 @@ -# Go Patterns [![Awesome Widget]][Awesome] [![Travis Widget]][Travis] [![License Widget]][License] +

+ +

+ +# Go Patterns [![Travis Widget]][Travis] [![Awesome Widget]][Awesome] [![License Widget]][License] [Awesome Widget]: https://img.shields.io/badge/awesome-%E2%9C%93-ff69b4.svg?style=flat-square [Awesome]: https://github.com/sindresorhus/awesome [Travis Widget]: https://img.shields.io/travis/tmrts/awesome-google-cloud.svg?style=flat-square [Travis]: http://travis-ci.org/tmrts/go-patterns [License Widget]: https://img.shields.io/badge/license-Creative%20Commons%204.0-E91E63.svg?style=flat-square [License]: http://creativecommons.org/licenses/by/4.0/ -A curated collection of common patterns & idioms for Go. +A curated collection of idiomatic design & application patterns for Go language. __Creational Patterns__: | Pattern | Description | |:-------:| ----------- | -| [abstract_factory](abstract_factory.go) | use a generic function with specific factories | -| [borg](borg.go) | a singleton with shared-state among instances | -| [builder](builder.go) | instead of using multiple constructors, builder object receives parameters and returns constructed objects | -| [factory_method](factory_method.go) | delegate a specialized function/method to create instances | -| [lazy_evaluation](lazy_evaluation.go) | lazily-evaluated property pattern in Go | -| [pool](pool.go) | preinstantiate and maintain a group of instances of the same type | -| [prototype](prototype.go) | use a factory and clones of a prototype for new instances (if instantiation is expensive) | +| [Abstract Factory](abstract_factory.go) | use a generic function with specific factories | +| [Borg Singleton](borg_singleton.go) | a singleton with shared-state among instances | +| [Builder](builder.go) | instead of using multiple constructors, builder object receives parameters and returns constructed objects | +| [Factory Method](factory_method.go) | delegate a specialized function/method to create instances | +| [Lazy Evaluation](lazy_evaluation.go) | lazily-evaluated property pattern in Go | +| [Object Pool](object_pool.go) | preinstantiate and maintain a group of instances of the same type | +| [Prototype](prototype.go) | use a factory and clones of a prototype for new instances (if instantiation is expensive) | __Structural Patterns__: | Pattern | Description | |:-------:| ----------- | -| [3-tier](3-tier.go) | data<->business logic<->presentation separation (strict relationships) | -| [adapter](adapter.go) | adapt one interface to another using a white-list | -| [bridge](bridge.go) | a client-provider middleman to soften interface changes | -| [composite](composite.go) | encapsulate and provide access to a number of different objects | -| [decorator](decorator.go) | wrap functionality with other functionality in order to affect outputs | -| [facade](facade.go) | use one class as an API to a number of others | -| [flyweight](flyweight.go) | transparently reuse existing instances of objects with similar/identical state | -| [front_controller](front_controller.go) | single handler requests coming to the application | -| [mvc](mvc.go) | model<->view<->controller (non-strict relationships) | -| [proxy](proxy.go) | an object funnels operations to something else | +| [Adapter](adapter.go) | adapt one interface to another using a white-list | +| [Bridge](bridge.go) | a client-provider middleman to soften interface changes | +| [Composite](composite.go) | encapsulate and provide access to a number of different objects | +| [Decorator](decorator.go) | Adds behavior to an object, statically or dynamically | +| [Facade](facade.go) | use one class as an API to a number of others | +| [Flyweight](flyweight.go) | transparently reuse existing instances of objects with similar/identical state | +| [Model View Controller](mvc.go) | model<->view<->controller (non-strict relationships) | +| [Proxy](proxy.go) | an object funnels operations to something else | __Behavioral Patterns__: | Pattern | Description | |:-------:| ----------- | -| [chain](chain.go) | apply a chain of successive handlers to try and process the data | -| [catalog](catalog.go) | general methods will call different specialized methods based on construction parameter | -| [chaining_method](chaining_method.go) | continue callback next object method | -| [command](command.go) | bundle a command and arguments to call later | -| [mediator](mediator.go) | an object that knows how to connect other objects and act as a proxy | -| [memento](memento.go) | generate an opaque token that can be used to go back to a previous state | -| [observer](observer.go) | provide a callback for notification of events/changes to data | -| [publish_subscribe](publish_subscribe.go) | a source syndicates events/data to 0+ registered listeners | -| [registry](registry.go) | keep track of all subclasses of a given class | -| [specification](specification.go) | business rules can be recombined by chaining the business rules together using boolean logic | -| [state](state.go) | logic is organized into a discrete number of potential states and the next state that can be transitioned to | -| [strategy](strategy.go) | selectable operations over the same data | -| [template](template.go) | an object imposes a structure but takes pluggable components | -| [visitor](visitor.go) | invoke a callback for all items of a collection | +| [Chain](chain.go) | apply a chain of successive handlers to try and process the data | +| [Catalog](catalog.go) | general methods will call different specialized methods based on construction parameter | +| [Chaining Method](chaining_method.go) | continue callback next object method | +| [Command](command.go) | bundle a command and arguments to call later | +| [Mediator](mediator.go) | an object that knows how to connect other objects and act as a proxy | +| [Memento](memento.go) | generate an opaque token that can be used to go back to a previous state | +| [Observer](observer.go) | provide a callback for notification of events/changes to data | +| [Publish/Subscribe](publish_subscribe.go) | Passes information to a collection of recipients who subscribed to a topic | +| [Registry](registry.go) | keep track of all subclasses of a given class | +| [Specification](specification.go) | business rules can be recombined by chaining the business rules together using boolean logic | +| [State](state.go) | logic is organized into a discrete number of potential states and the next state that can be transitioned to | +| [Strategy](strategy.go) | Encapsulates an algorithm inside a struct | +| [Template](template.go) | an object imposes a structure but takes pluggable components | +| [Visitor](visitor.go) | invoke a callback for all items of a collection | +__Synchronization Patterns__: + +| Pattern | Description | +|:-------:| ----------- | +| [Lock/Mutex](mutex.go) | Enforces mutual exclusion limit on accessing a resource | +| [Read-Write Lock](read_write_lock.go) | | +| [Condition Variable](condition_variable.go) | | +| [Monitor](monitor.go) | Combination of mutex and condition patterns | +| [Semaphore](semaphore.go) | Allows controlling access to a common resource | + __Concurrency Patterns__: | Pattern | Description | |:-------:| ----------- | -| [semaphore](semaphore.go) | data type for controlling access, to a common resource| +| [Scheduler](scheduler.go) | | +| [Barrier](barrier.go) | | +| [Producer Consumer](producer_consumer.go) | | +| [Futures](future.go) | | +| [Broadcast](broadcast.go) | | +| [Multiplex](multiplex.go) | | +| [Fan-In](fan_in.go) | | +| [Generators](generator.go) | | +| [Coroutines](coroutine.go) | | + +__Stability Patterns__: + +| Pattern | Description | +|:-------:| ----------- | +| [Bulkheads](bulkhead.go) | | +| [Circuit Breaker](circuit_breaker.go) | | +| [Deadline](deadline.go) | | +| [Fail Fast](fail_fast.go) | | +| [Handshaking](handshaking.go) | | +| [Steady State](steady_state.go) | | + +__Profiling Patterns__: + +| Pattern | Description | +|:-------:| ----------- | +| [Timing Functions](timing.go) | Wraps a function and logs the execution | + +__Idioms__: + +| Pattern | Description | +|:-------:| ----------- | +| [Functional Options](functional_options.go) | Allows creating clean APIs with sane defaults and idiomatic overrides | + +__Anti-Patterns__: + +| Pattern | Description | +|:-------:| ----------- | + +__Other Patterns__: + +| Pattern | Description | +|:-------:| ----------- | # License diff --git a/functional_options.go b/functional_options.go index 198717d..249a462 100644 --- a/functional_options.go +++ b/functional_options.go @@ -2,7 +2,7 @@ package main import "os" -type arguments struct { +type Arguments struct { UID int GID int Flags int @@ -10,35 +10,35 @@ type arguments struct { Permissions os.FileMode } -type argument func(*arguments) +type Argument func(*Arguments) -func UID(userID int) argument { - return func(args *arguments) { +func UID(userID int) Argument { + return func(args *Arguments) { args.UID = userID } } -func GID(groupID int) argument { - return func(args *arguments) { +func GID(groupID int) Argument { + return func(args *Arguments) { args.GID = groupID } } -func Contents(c string) argument { - return func(args *arguments) { +func Contents(c string) Argument { + return func(args *Arguments) { args.Contents = c } } -func Permissions(perms os.FileMode) argument { - return func(args *arguments) { +func Permissions(perms os.FileMode) Argument { + return func(args *Arguments) { args.Permissions = perms } } -func New(filepath string, setters ...argument) error { - // Default arguments - args := &arguments{ +func New(filepath string, setters ...Argument) error { + // Default Arguments + args := &Arguments{ UID: os.Getuid(), GID: os.Getgid(), Contents: "", diff --git a/gopher.jpg b/gopher.jpg new file mode 100644 index 0000000..68795a9 Binary files /dev/null and b/gopher.jpg differ diff --git a/publish_subscribe.go b/publish_subscribe.go new file mode 100644 index 0000000..441e01b --- /dev/null +++ b/publish_subscribe.go @@ -0,0 +1,62 @@ +package main + +import ( + "errors" + "time" +) + +var ( + ErrTopicClosed = errors.New("Topic has been closed") +) + +// Message +type Message string + +// Topic +type Topic struct { + Subscribers []Authentication + MessageHistory []struct { + Author string + Message Message + Timestamp time.Time + } +} + +// Subscribe +func (t *Topic) Subscribe(Authentication) (Subscription, error) { + // Implementation +} + +// Unsubscribe +func (t *Topic) Unsubscribe(Subscription) error { + // Implementation +} + +// Delete +func (t *Topic) Delete() error { + // Implementation +} + +type Subscription struct { + ch chan<- Message + + Inbox chan Message +} + +// Publish +func (s *Subscription) Publish(msg Message) error { + if _, ok := ch; !ok { + return ErrTopicClosed + } + + ch <- msg + + return nil +} + +// Authentication +type Authentication struct { +} + +func main() { +}