mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-21 20:46:08 +03:00
creational/singleton: use fmt.Println to show the result
This commit is contained in:
parent
066d2f6b42
commit
077bcecea0
@ -1,7 +1,9 @@
|
||||
#Singleton Pattern
|
||||
# Singleton Pattern
|
||||
|
||||
Singleton creational design pattern restricts the instantiation of a type to a single object.
|
||||
|
||||
## Implementation
|
||||
|
||||
```go
|
||||
package singleton
|
||||
|
||||
@ -21,6 +23,7 @@ func New() *singleton {
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
s := singleton.New()
|
||||
|
||||
@ -28,8 +31,10 @@ s["this"] = "that"
|
||||
|
||||
s2 := singleton.New()
|
||||
|
||||
// s2["this"] == "that"
|
||||
fmt.Println("This is ", s2["this"])
|
||||
// This is that
|
||||
```
|
||||
|
||||
## Rules of Thumb
|
||||
|
||||
- Singleton pattern represents a global state and most of the time reduces testability.
|
||||
|
Loading…
Reference in New Issue
Block a user