|
| 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 | +} |
0 commit comments