templatemethod

package
v0.0.0-...-eb36113 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: Apache-2.0, MIT Imports: 1 Imported by: 0

README

模版方法模式

模版方法模式使用继承机制,把通用步骤和通用方法放到父类中,把具体实现延迟到子类中实现。使得实现符合开闭原则。

如实例代码中通用步骤在父类中实现(准备下载保存收尾)下载和保存的具体实现留到子类中,并且提供 保存方法的默认实现。

因为Golang不提供继承机制,需要使用匿名组合模拟实现继承。

此处需要注意:因为父类需要调用子类方法,所以子类需要匿名组合父类的同时,父类需要持有子类的引用。

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Downloader

type Downloader interface {
	Download(uri string)
}

func NewFTPDownloader

func NewFTPDownloader() Downloader

func NewHTTPDownloader

func NewHTTPDownloader() Downloader

type FTPDownloader

type FTPDownloader struct {
	// contains filtered or unexported fields
}
Example
var downloader Downloader = NewFTPDownloader()

downloader.Download("ftp://example.com/abc.zip")
Output:

prepare downloading
download ftp://example.com/abc.zip via ftp
default save
finish downloading

func (FTPDownloader) Download

func (t FTPDownloader) Download(uri string)

type HTTPDownloader

type HTTPDownloader struct {
	// contains filtered or unexported fields
}
Example
var downloader Downloader = NewHTTPDownloader()

downloader.Download("http://example.com/abc.zip")
Output:

prepare downloading
download http://example.com/abc.zip via http
http save
finish downloading

func (HTTPDownloader) Download

func (t HTTPDownloader) Download(uri string)

Jump to

Keyboard shortcuts

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