go-pattern-examples/behavior/05_template_method/template_method.go

80 lines
1.4 KiB
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package templatemethod
import "fmt"
2020-05-03 16:08:56 +03:00
////////////////////////////////
//使用打印的例子
2020-04-21 17:50:21 +03:00
2020-05-03 16:08:56 +03:00
//IPrinter 定义打印的流程
type IPrinter interface {
Set(mark string)
Print()
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//Printer 定义基本结构类型
type Printer struct {
workerMark string
printer IPrinter //指项实际工作的类型
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//LoadDrive 载入驱动
func (p *Printer) LoadDrive() {
fmt.Print("init print drive\n")
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//UnLoadDrive 卸载驱动
func (p *Printer) UnLoadDrive() {
fmt.Print("unload drive\n")
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//Set 设置参数,这是变化的部分
func (p *Printer) Set(mark string) {
p.workerMark = mark
//调用实现
if p.printer != nil {
p.printer.Set(mark)
}
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//Print 执行打印,这是变化的部分
func (p *Printer) Print() {
//调用实现
fmt.Print("print with task mark: ", p.workerMark, "\n")
if p.printer != nil {
p.printer.Print()
}
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//DoPrintWork 打印
//DoPrintWork 定义了打印的流程
func (p *Printer) DoPrintWork() {
p.LoadDrive()
p.Set(p.workerMark)
p.Print()
p.UnLoadDrive()
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//PDF 虚拟打印
type PDF struct {
Printer
output string
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//Print to a PDF
func (p *PDF) Print() {
fmt.Print("print to PDF ,save to ", p.output, "\n")
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//DevicePrinter 设备打印机
type DevicePrinter struct {
Printer
quality int //1,2,3表示打印高中低
2020-04-21 17:50:21 +03:00
}
2020-05-03 16:08:56 +03:00
//Print to a Paper
func (d *DevicePrinter) Print() {
fmt.Print("print to Paper ,with quality: ", d.quality, "\n")
2020-04-21 17:50:21 +03:00
}