Skip to content

Commit eaffaf6

Browse files
author
tensor-programming
committed
add factory pattern
1 parent 80ee561 commit eaffaf6

File tree

3 files changed

+576
-0
lines changed

3 files changed

+576
-0
lines changed

factory/factory/factory.go

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package factory
2+
3+
import "fmt"
4+
5+
type (
6+
mongoDB struct {
7+
database map[string]string
8+
}
9+
10+
sqlite struct {
11+
database map[string]string
12+
}
13+
14+
file struct {
15+
name string
16+
content string
17+
}
18+
19+
ntfs struct {
20+
files map[string]file
21+
}
22+
23+
ext4 struct {
24+
files map[string]file
25+
}
26+
27+
FileSystem interface {
28+
CreateFile(string)
29+
FindFile(string) file
30+
}
31+
32+
Database interface {
33+
GetData(string) string
34+
PutData(string, string)
35+
}
36+
37+
Factory func(string) interface{}
38+
)
39+
40+
func (mdb mongoDB) GetData(query string) string {
41+
if _, ok := mdb.database[query]; !ok {
42+
return ""
43+
}
44+
45+
fmt.Println("MongoDB")
46+
return mdb.database[query]
47+
}
48+
49+
func (sql sqlite) GetData(query string) string {
50+
if _, ok := sql.database[query]; !ok {
51+
return ""
52+
}
53+
54+
fmt.Println("Sqlite")
55+
return sql.database[query]
56+
}
57+
58+
func (mdb mongoDB) PutData(query string, data string) {
59+
mdb.database[query] = data
60+
}
61+
62+
func (sql sqlite) PutData(query string, data string) {
63+
sql.database[query] = data
64+
}
65+
66+
func (ntfs ntfs) CreateFile(path string) {
67+
file := file{content: "NTFS file", name: path}
68+
ntfs.files[path] = file
69+
fmt.Println("NTFS")
70+
}
71+
72+
func (ext ext4) CreateFile(path string) {
73+
file := file{content: "EXT4 file", name: path}
74+
ext.files[path] = file
75+
fmt.Println("EXT4")
76+
}
77+
78+
func (ntfs ntfs) FindFile(path string) file {
79+
if _, ok := ntfs.files[path]; !ok {
80+
return file{}
81+
}
82+
83+
return ntfs.files[path]
84+
}
85+
86+
func (ext ext4) FindFile(path string) file {
87+
if _, ok := ext.files[path]; !ok {
88+
return file{}
89+
}
90+
91+
return ext.files[path]
92+
}
93+
94+
func FilesystemFactory(env string) interface{} {
95+
switch env {
96+
case "production":
97+
return ntfs{
98+
files: make(map[string]file),
99+
}
100+
case "development":
101+
return ext4{
102+
files: make(map[string]file),
103+
}
104+
default:
105+
return nil
106+
}
107+
}
108+
109+
func DatabaseFactory(env string) interface{} {
110+
switch env {
111+
case "production":
112+
return mongoDB{
113+
database: make(map[string]string),
114+
}
115+
case "development":
116+
return sqlite{
117+
database: make(map[string]string),
118+
}
119+
default:
120+
return nil
121+
}
122+
}
123+
124+
func AbstractFactory(fact string) Factory {
125+
switch fact {
126+
case "database":
127+
return DatabaseFactory
128+
case "filesystem":
129+
return FilesystemFactory
130+
default:
131+
return nil
132+
}
133+
}

factory/main.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
"github.com/tensor-programming/pattern-tutorial/factory"
8+
)
9+
10+
func SetupConstructors(env string) (factory.Database, factory.FileSystem) {
11+
fs := factory.AbstractFactory("filesystem")
12+
db := factory.AbstractFactory("database")
13+
14+
return db(env).(factory.Database),
15+
fs(env).(factory.FileSystem)
16+
}
17+
18+
func main() {
19+
env1 := "production"
20+
env2 := "development"
21+
22+
db1, fs1 := SetupConstructors(env1)
23+
db2, fs2 := SetupConstructors(env2)
24+
25+
// db1 := factory.DatabaseFactory(env1)
26+
// db2 := factory.DatabaseFactory(env2)
27+
28+
db1.PutData("test", "this is mongodb")
29+
fmt.Println(db1.GetData("test"))
30+
31+
db2.PutData("test", "this is sqlite")
32+
fmt.Println(db2.GetData("test"))
33+
34+
fs1.CreateFile("../example/testntfs.txt")
35+
fmt.Println(fs1.FindFile("../example/testntfs.txt"))
36+
37+
fs2.CreateFile("../example/testext4.txt")
38+
fmt.Println(fs2.FindFile("../example/testext4.txt"))
39+
40+
fmt.Println(reflect.TypeOf(db1).Name())
41+
fmt.Println(reflect.TypeOf(&db1).Elem())
42+
fmt.Println(reflect.TypeOf(db2).Name())
43+
fmt.Println(reflect.TypeOf(&db2).Elem())
44+
45+
fmt.Println(reflect.TypeOf(fs1).Name())
46+
fmt.Println(reflect.TypeOf(&fs1).Elem())
47+
fmt.Println(reflect.TypeOf(fs2).Name())
48+
fmt.Println(reflect.TypeOf(&fs2).Elem())
49+
}

0 commit comments

Comments
 (0)