zoukankan      html  css  js  c++  java
  • GoLang设计模式05

    原型模式也是一种创建型模式,它可以帮助我们优雅地创建对象的拷贝。在这种设计模式里面,将克隆某个对象的职责交给了要被克隆的这个对象。被克隆的对象需要提供一个clone()方法。通过这个方法可以返回该对象的拷贝。

    原型模式的使用场景:

    1. 创建新对象的操作比较耗资源(如数据库操作)或代价比较高时。比较起从头创建新对象,克隆对象明显更加可取
    2. 要被克隆的对象创建起来比较复杂时:比如对象克隆的过程中存在深度拷贝或分层拷贝时;又比如要被克隆的对象存在无法被直接访问到的私有成员时。

    原型模式的UML类图:

    在日常中我们遇到的包含新建、copy这些操作的场景最多的就是常规的文件系统操作了。所以接下来会以文件系统为例介绍下原型模式。

    在文件系统中存在文件(file)和文件夹(folder)两类实体。其中文件夹中又可以包含文件和子文件夹。这里我们用一个inode接口来表示文件和文件夹。为了表示常见的复制操作,在inode接口中还定义了一个clone()函数。

    inode.go

    type inode interface {
    	print(string)
    	clone() inode
    }
    

    file 和 folder两个struct实现了inode接口。

    file.go

    import "fmt"
    
    type file struct {
    	name string
    }
    
    func (f *file) print(indentation string) {
    	fmt.Println(indentation + f.name)
    }
    
    func (f *file) clone() inode {
    	return &file{name: f.name + "_clone"}
    }
    

    folder.go

    import "fmt"
    
    type folder struct {
    	children []inode
    	name     string
    }
    
    func (f *folder) print(indentation string) {
    	fmt.Println(indentation + f.name)
    	for _, i := range f.children {
    		i.print(indentation + indentation)
    	}
    }
    
    func (f *folder) clone() inode {
    	cloneFolder := &folder{name: f.name + "_clone"}
    	var tempChildren []inode
    	for _, i := range f.children {
    		copy := i.clone()
    		tempChildren = append(tempChildren, copy)
    	}
    	cloneFolder.children = tempChildren
    	return cloneFolder
    }
    

    filefolder两个struct都实现了print()clone()函数,根据go语言的约定,可以认为它们都继承了inode接口,即可以认为它们是inode类型了。这两者的clone()函数会返回其各自相关的文件或文件夹的备份。为了和原有的文件/文件夹作区分,我们在复制的对象的名称上添加了“_clone”这样的标记。

    来看看main函数是怎样的,main.go:

    import (
    	"fmt"
    )
    
    func main() {
    	file1 := &file{name: "File1"}
    	file2 := &file{name: "File2"}
    	file3 := &file{name: "File3"}
    	folder1 := &folder{
    		children: []inode{file1},
    		name:     "Folder1",
    	}
    	folder2 := &folder{
    		children: []inode{folder1, file2, file3},
    		name:     "Folder2",
    	}
    	fmt.Println("
    Printing hierarchy for Folder2")
    	folder2.print("  ")
    	cloneFolder := folder2.clone()
    	fmt.Println("
    Printing hierarchy for clone Folder")
    	cloneFolder.print("  ")
    }
    

    运行结果是:

    Printing hierarchy for Folder2
      Folder2
        Folder1
            File1
        File2
        File3
    
    Printing hierarchy for clone Folder
      Folder2_clone
        Folder1_clone
            File1_clone
        File2_clone
        File3_clone
    

    代码已上传至GitHub, 地址: zhyea / go-patterns / prototype-pattern

    END!


    仅是学习笔记,难免出错,望不吝指点
  • 相关阅读:
    python3爬取咪咕音乐榜信息(附源代码)
    python3通过Beautif和XPath分别爬取“小猪短租-北京”租房信息,并对比时间效率(附源代码)
    在windows7 32ibt安装MongoDB数据库的方法及连接失败解决方案
    GoWeb开发_Iris框架讲解(一)
    Web项目开发介绍及实战项目介绍
    DAY7学习笔记
    DAY6课下作业
    DAY6学习笔记
    DAY5课下作业
    DAY5学习笔记
  • 原文地址:https://www.cnblogs.com/amunote/p/15256894.html
Copyright © 2011-2022 走看看