Golang 面向对象编程-非侵入式接口

Golang 面向对象编程-非侵入式接口

在go语言中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口,例如:

type File struct {
    // ...
}

func (f *File) Read(buf []byte) (n int, err error)

func (f *File) Write(buf []byte) (n int, err error)

func (f *File) Seek(off int64, whence int) (pos int64, err error)

func (f *File) Close() error

这里我们定义了一个File类,并实现了Read()、Write()、Seek()、Close()等方法。设想我们有如下接口:

type IFile interface {
    Read(buf []byte) (n int, err error)
    Write(buf []byte) (n int, err error)
    Seek(off int64, whence int) (pos int64, err error)
    Close() error
}

type IReader interface {
     Read(buf []byte) (n int, err error)
}

type IWriter interface {
    Write(buf []byte) (n int, err error)
}

type ICloser interface {
    Close() error
}

尽管File类并没有从这些接口继承,甚至可以不知道这些接口的存在,但是File类实现了这些接口,就可以进行赋值:

var file1 IFile   = new(File)
var file2 IReader = new(File)
var file3 IWriter = new(File)
var file4 ICloser = new(File)

Go语言的非侵入式接口,看似只是做了很小的文法调整,实则影响深远。

其一,Go语言的标准库,再也不需要绘制类库的继承树图,在Go中,类的继承树并无意义,你只需要知道这个类实现了哪些方法,每个方法是什么含义就足够了。

其二,实现类的时候,只需要关心自己应该提供哪些方法,不用再纠结接口需要拆的多细才合理。接口由使用方按需定制,而不用事先规划。

其三,不用为了实现一个接口而导入一个包,因为多引入一个外部包,就意味着更多的耦合。接口按使用方按自身需求来定义,使用方无需关心是否有其他模块定义过类似的接口。

你可能感兴趣的:(Golang 面向对象编程-非侵入式接口)