templatemethod

package
v0.0.0-...-e059a18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2023 License: MIT Imports: 1 Imported by: 0

README

模版方法模式

模板方法主要是为了解决一些方法通用性问题,将通用方法放在父类,需要单独实现的方法放在其他类中,将通用方法和变化的方法分开,各自实现分开到不同的地方,并且可以单独实现,往往是将将变化部分方法/处理器的实现延迟到子类或者其他可注入的类型中。

go 的结构对象和接口的分离做的更彻底,不需要像其他语言一样显式声明继承或者实现关系,所以在实现模板方法时候更灵活。

模板方法的特点在于,定义一好一套接口,定义基本类型结构作为父类,同时在父类中组合好调用方式,并实现好通用的方法,变化部分的接口的具体实现可以在子类或者其他地方实现。

现实生活中工作中,文件打印就是一个很好的模板方法的例子,我们打印操作一般式:

文件--->设置--->打印.

但是对于不同的打印方式对于同一个文件的打印实现可能略有不同。

比如虚拟打印到 pdf或者xps与打印打纸质文件需要的设置和打印肯定式不同的,虚拟打印你肯定需要指明输出路径,纸质打印需要可能需要设置打印质量,质量越高,越费墨。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DevicePrinter

type DevicePrinter struct {
	Printer
	// contains filtered or unexported fields
}

DevicePrinter 设备打印机

func (*DevicePrinter) Print

func (d *DevicePrinter) Print()

Print to a Paper

type IPrinter

type IPrinter interface {
	Set(mark string)
	Print()
}

IPrinter 定义打印的流程

type PDF

type PDF struct {
	Printer
	// contains filtered or unexported fields
}

PDF 虚拟打印

func (*PDF) Print

func (p *PDF) Print()

Print to a PDF

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer 定义基本结构类型

func (*Printer) DoPrintWork

func (p *Printer) DoPrintWork()

DoPrintWork 打印 DoPrintWork 定义了打印的流程

func (*Printer) LoadDrive

func (p *Printer) LoadDrive()

LoadDrive 载入驱动

func (*Printer) Print

func (p *Printer) Print()

Print 执行打印,这是变化的部分

func (*Printer) Set

func (p *Printer) Set(mark string)

Set 设置参数,这是变化的部分

func (*Printer) UnLoadDrive

func (p *Printer) UnLoadDrive()

UnLoadDrive 卸载驱动

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL