template_method

package
v0.0.0-...-6db54d9 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2021 License: MIT Imports: 0 Imported by: 0

README

Шаблонный метод (Template Method)

Паттерн Template Method относится к поведенческим паттернам уровня класса.

Паттерн Template Method формирует структуру алгоритма и позволяет в производных классах реализовать, перекрыть или переопределить определенные шаги алгоритма, не изменяя структуру алгоритма в целом.

Проектировщик решает, какие шаги алгоритма являются неизменными, а какие изменяемыми. Абстрактный базовый класс реализует стандартные неизменяемые шаги алгоритма и может предоставлять реализацию по умолчанию для изменяемых шагов. Изменяемые шаги могут предоставляться клиентом компонента в конкретных производных классах.

Требуется для реализации:

  1. Абстрактный класс AbstractClass, реализующий Template Method, который описывает порядок действий;
  2. Класс ConcreteClass, реализующий изменяемые действия.

[!] В описании паттерна применяются общие понятия, такие как Класс, Объект, Абстрактный класс. Применимо к языку Go, это Пользовательский Тип, Значение этого Типа и Интерфейс. Также в языке Go за место общепринятого наследования используется агрегирование и встраивание.

Т.к. в Go нет понятия "Абстрактный Класс" и знакомого нам полиморфизма на наследовании, следует использовать встравивания общего для ConcreteClass типа с реализацией Template Method.

-- THE END --

Documentation

Overview

Package template_method is an example of the Template Method Pattern. In fact, this pattern is based on Abstract Class and Polymorphism. But there’s nothing like that in Go, so the composition will be applied.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FrenchQuotes

type FrenchQuotes struct {
}

FrenchQuotes implements wrapping the string in French quotes.

func (*FrenchQuotes) Close

func (q *FrenchQuotes) Close() string

Close sets closing quotes.

func (*FrenchQuotes) Open

func (q *FrenchQuotes) Open() string

Open sets opening quotes.

type GermanQuotes

type GermanQuotes struct {
}

GermanQuotes implements wrapping the string in German quotes.

func (*GermanQuotes) Close

func (q *GermanQuotes) Close() string

Close sets closing quotes.

func (*GermanQuotes) Open

func (q *GermanQuotes) Open() string

Open sets opening quotes.

type Quotes

type Quotes struct {
	QuotesInterface
}

Quotes implements a Template Method.

func NewQuotes

func NewQuotes(qt QuotesInterface) *Quotes

NewQuotes is the Quotes constructor.

func (*Quotes) Quotes

func (q *Quotes) Quotes(str string) string

Quotes is the Template Method.

type QuotesInterface

type QuotesInterface interface {
	Open() string
	Close() string
}

QuotesInterface provides an interface for setting different quotes.

Jump to

Keyboard shortcuts

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