GoLang设计模式06 - 原型模式@
原型模式也是一种创建型模式,它可以帮助我们优雅地创建对象的拷贝。在这种设计模式里面,将克隆某个对象的职责交给了要被克隆的这个对象。被克隆的对象需要提供一个clone()方法。通过这个方法可以返回该对象的拷贝。
原型模式的使用场景:
- 创建新对象的操作比较耗资源(如数据库操作)或代价比较高时。比较起从头创建新对象,克隆对象明显更加可取
 - 要被克隆的对象创建起来比较复杂时:比如对象克隆的过程中存在深度拷贝或分层拷贝时;又比如要被克隆的对象存在无法被直接访问到的私有成员时。
 
原型模式的UML类图:

在日常中我们遇到的包含新建、copy这些操作的场景最多的就是常规的文件系统操作了。所以接下来会以文件系统为例介绍下原型模式。
在文件系统中存在文件(file)和文件夹(folder)两类实体。其中文件夹中又可以包含文件和子文件夹。这里我们用一个inode接口来表示文件和文件夹。为了表示常见的复制操作,在inode接口中还定义了一个clone()函数。
1 type inode interface { 2 print(string) 3 clone() inode 4 } 5 6 type file struct { 7 name string 8 } 9 10 func (f *file) print(indentation string) { 11 fmt.Println(indentation + f.name) 12 } 13 14 func (f *file) clone() inode { 15 return &file{name: f.name + "_clone"} 16 } 17 18 type folder struct { 19 children []inode 20 name string 21 } 22 23 func (f *folder) print(indentation string) { 24 fmt.Println(indentation + f.name) 25 for _, i := range f.children { 26 i.print(indentation + indentation) 27 } 28 } 29 30 func (f *folder) clone() inode { 31 cloneFolder := &folder{name: f.name + "_clone"} 32 var tempChildren []inode 33 for _, i := range f.children { 34 copy := i.clone() 35 tempChildren = append(tempChildren, copy) 36 } 37 cloneFolder.children = tempChildren 38 return cloneFolder 39 }
file和folder两个struct都实现了print()和clone()函数,根据go语言的约定,可以认为它们都继承了inode接口,即可以认为它们是inode类型了。这两者的clone()函数会返回其各自相关的文件或文件夹的备份。为了和原有的文件/文件夹作区分,我们在复制的对象的名称上添加了“_clone”这样的标记。
1 import ( 2 "fmt" 3 ) 4 5 func main() { 6 file1 := &file{name: "File1"} 7 file2 := &file{name: "File2"} 8 file3 := &file{name: "File3"} 9 folder1 := &folder{ 10 children: []inode{file1}, 11 name: "Folder1", 12 } 13 folder2 := &folder{ 14 children: []inode{folder1, file2, file3}, 15 name: "Folder2", 16 } 17 fmt.Println("\nPrinting hierarchy for Folder2") 18 folder2.print(" ") 19 cloneFolder := folder2.clone() 20 fmt.Println("\nPrinting hierarchy for clone Folder") 21 cloneFolder.print(" ") 22 }
运行结果是:
1 Printing hierarchy for Folder2 2 Folder2 3 Folder1 4 File1 5 File2 6 File3 7 8 Printing hierarchy for clone Folder 9 Folder2_clone 10 Folder1_clone 11 File1_clone 12 File2_clone 13 File3_clone
                    
                
                
            
        
浙公网安备 33010602011771号