mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-24 21:46:03 +03:00
Create factory_method.go
This commit is contained in:
parent
f515db98ba
commit
e2de2e1600
33
creational/factory_method.go
Normal file
33
creational/factory_method.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
type Store interface {
|
||||||
|
Open(string) (io.ReadWriteCloser, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type StorageType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
DiskStorage StorageType = 1 << iota
|
||||||
|
TempStorage
|
||||||
|
MemoryStorage
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewStore(t StorageType) Store {
|
||||||
|
switch t {
|
||||||
|
case MemoryStorage:
|
||||||
|
return newMemoryStorage( /*...*/ )
|
||||||
|
case DiskStorage:
|
||||||
|
return newDiskStorage( /*...*/ )
|
||||||
|
default:
|
||||||
|
return newTempStorage( /*...*/ )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s, _ := NewStore(data.MemoryStorage)
|
||||||
|
f, _ := s.Open("file")
|
||||||
|
|
||||||
|
n, _ := f.Write([]byte("data"))
|
||||||
|
defer f.Close()
|
Loading…
Reference in New Issue
Block a user