Go处理依赖抽象

Go的依赖倒置原则:代码运行依赖内部的抽象接口

package main

import "fmt"

type Note interface {
	GetType() string
}

type Lineation struct {
}

func (line Lineation) GetType() string {
	return "划线笔记"
}

// NoteObj 声明为接口类型,便于后续扩展
type Reader struct {
	NoteObj Note
}

func (read Reader) Read(){
	fmt.Println("正在学习:", read.NoteObj.GetType())	
}

func main () {

	note := Lineation{}
	reader := Reader {
		NoteObj:note,
	}
	reader.Read()
}

你可能感兴趣的:(Golang,设计模式)