Application 2023-08-20

About Template Method and Strategy Patterns

Master Template Method and Strategy patterns—two behavioral design patterns for controlling process flow and runtime method selection.

Read in: ja
About Template Method and Strategy Patterns

Overview

This post summarizes the Template Method and Strategy patterns, which are behavioral patterns from the GoF.

What is the Template Method Pattern?

The Template Method pattern is a design pattern where the overall process is defined in a higher-level class, and the specific flow of processing is delegated to lower-level classes.

package main

type Game interface {
	Init()
	Start()
	End()
}

type BaseBall struct{}

func (b *BaseBall) Init() {
	println("BaseBall Init")
}

func (b *BaseBall) Start() {
	println("BaseBall Start")
}

func (b *BaseBall) End() {
	println("BaseBall End")
}

func (b *BaseBall) Play() {
	b.Init()
	b.Start()
	for i := 0; i < 9; i++ {
		println("Top & Bottom")
	}
	b.End()
}

func main() {
	b := &BaseBall{}
	b.Play()
}

What is the Strategy Pattern?

The Strategy pattern is a design pattern that allows the selection of processing at runtime.

While similar to the Template pattern, the Strategy pattern is structured to switch all processing collectively, whereas the Template pattern has fixed specific processes with others being variable.

package main

type PaymentStrategy interface {
	Pay(amount int)
}

type CreditCard struct{}

func (cc *CreditCard) Pay(amount int) {
	println("CreditCard Pay")
}

type Cash struct{}

func (c *Cash) Pay(amount int) {
	println("Cash Pay")
}

type Cart struct {
	paymentMethod PaymentStrategy
}

func (c *Cart) Checkout(amount int) {
	c.paymentMethod.Pay(amount)
}

func main() {
	cc := &CreditCard{}
	c := &Cash{}

	cart := &Cart{paymentMethod: cc}
	cart.Checkout(100)

	cart.paymentMethod = c
	cart.Checkout(100)
}

Both the Template Method and Strategy patterns allow for the use of inheritance or delegation, so there is no strict requirement to use one over the other.

References

Tags: Strategy Template Method
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles