mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-25 06:26:06 +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.
|
Singleton creational design pattern restricts the instantiation of a type to a single object.
|
||||||
|
|
||||||
## Implementation
|
## Implementation
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package singleton
|
package singleton
|
||||||
|
|
||||||
@ -21,6 +23,7 @@ func New() *singleton {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```go
|
```go
|
||||||
s := singleton.New()
|
s := singleton.New()
|
||||||
|
|
||||||
@ -28,8 +31,10 @@ s["this"] = "that"
|
|||||||
|
|
||||||
s2 := singleton.New()
|
s2 := singleton.New()
|
||||||
|
|
||||||
// s2["this"] == "that"
|
fmt.Println("This is ", s2["this"])
|
||||||
|
// This is that
|
||||||
```
|
```
|
||||||
|
|
||||||
## Rules of Thumb
|
## Rules of Thumb
|
||||||
|
|
||||||
- Singleton pattern represents a global state and most of the time reduces testability.
|
- Singleton pattern represents a global state and most of the time reduces testability.
|
||||||
|
Loading…
Reference in New Issue
Block a user