forked from tmrts/go-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#Singleton Pattern | ||
Singleton creational design pattern restricts the instantiation of a type to a single object. | ||
|
||
## Implementation | ||
```go | ||
package singleton | ||
|
||
type singleton map[string]string | ||
|
||
var once sync.Once | ||
|
||
var instance *singleton | ||
|
||
func New() *singleton { | ||
once.Do(func() { | ||
instance = make(singleton) | ||
}) | ||
|
||
return instance | ||
} | ||
``` | ||
|
||
## Usage | ||
```go | ||
s := singleton.New() | ||
|
||
s["this"] = "that" | ||
|
||
s2 := singleton.New() | ||
|
||
// s2["this"] == "that" | ||
``` | ||
|
||
## Rules of Thumb | ||
- Singleton pattern represents a global state and most of the time reduces testability. |
This file was deleted.
Oops, something went wrong.