diff --git a/creational/factory_method.go b/creational/factory_method.go new file mode 100644 index 0000000..16dee30 --- /dev/null +++ b/creational/factory_method.go @@ -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()