From e2de2e1600905003fb0ed18226867f7a0bbf87ea Mon Sep 17 00:00:00 2001 From: YikesHome <31678324+yikeshome@users.noreply.github.com> Date: Thu, 31 Oct 2019 03:05:23 +0800 Subject: [PATCH] Create factory_method.go --- creational/factory_method.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 creational/factory_method.go 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()