go-design-pattern | 设计模式 | Architecture library
kandi X-RAY | go-design-pattern Summary
kandi X-RAY | go-design-pattern Summary
设计模式
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- GetInstance3 returns an initialized Singleton
- NewFruit returns a newruit with the given number
- GetInstance2 returns a new Singleton object .
- GetInstance1 returns a new Singleton object .
- GetInstance4 returns an initialized Instleton
- NewMainSystemImpl returns a new MainSystemImpl .
- NewAdaptor returns a new Adaptor
- Run implements the MainSystem interface .
- newSubsystemA returns a new subSystemA .
- newSubsystemB returns a new instance of a subsystem .
go-design-pattern Key Features
go-design-pattern Examples and Code Snippets
Community Discussions
Trending Discussions on go-design-pattern
QUESTION
I'm following the Singleton design pattern as described in this book (https://github.com/PacktPublishing/Go-Design-Patterns/blob/master/Chapter02/main.go) and I have this code in file "singleton2.go":
...ANSWER
Answered 2019-Jun-12 at 22:01Follow the logic of your code and it's apparent where the problem is.
When you declare, but do not initialize the singleton (var instance *singleton
) then instance
is nil
. Calling GetInstance
evaluates instance == nil
as true and returns a new *singleton
every time it is called. That is why counter2
will never equal expectedCounter
- each call to GetInstance
is returning a new counter instance.
When you initialize the singleton instance (var instance = &singleton{}
) then calls to GetInstance
will return that singleton instance since it is not nil
.
I imagine you'd want to modify GetInstance
to something like this:
QUESTION
I've just read the chapter on Creational patterns in the book Design Patterns in golang. These are:
- factory
- abstract factory
- prototype
- singleton
- builder
I've created a repository on github to list all of them. I've looked for other repositories like mine:
Some list object-pool
as a pattern, others simple-factory
and factory-method
. Does there exist an official list of creational patterns in go? Is it important to know the list, or is it more important to know what is possible and be able to use right code in right context? With or without know all the patterns in the world?
And, ... what is the difference between prototype and object pool patterns? They look very similar to me.
...ANSWER
Answered 2018-Jun-14 at 20:14Design Patterns do not exist in isolation, they have relationships of their own. So for example, an object pool might use a factory or prototype to initially fill the pool or deal with exhaustion. The objects in that pool might conform to the command pattern, or be examples of the builder pattern.
In practice you cannot possibly know all the details of all patterns, the most important thing to know is the intent. What the pattern achieves. When using the pattern the remaining details can and should be taken from a pattern catalogue, over time you will learn some patterns comprehensively and reference others.
A Pattern Catalogue is the a collection of fully documented Design Patterns. The GoF Design Patterns book is probably the most famous Pattern Catalogue, however the book you mention and even github repositories can fulfil the same purpose.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install go-design-pattern
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page