decorator

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package decorator implements the Decorator design pattern. In the role-playing game example, we have three character classes: Knight, Magician, and Horseman. Each character has predefined attributes represented by the Character struct. Decorators such as WithPotion, WithShield, and WithSword enhance a character's abilities. These decorators can be applied in a composable manner, allowing for dynamic stacking of enhancements without modifying the original character implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run()

Types

type Actor

type Actor interface {
	Block() string
	Hit() string
	Walk() string
}

Actor defines the interface for all of the characters' actions.

type Buff

type Buff interface {
	Effectiveness() uint8
}

Buff defines the interface for all of the characters' decorators.

type Character

type Character struct {
	Class   string
	Speed   float32
	Attack  float32
	Defense uint8
}

Character is the common struct of all RPG characters.

func NewHorseman

func NewHorseman() *Character

NewHorseman is a factory function that creates a new horseman.

func NewKnight

func NewKnight() *Character

NewKnight is a factory function that creates a new knight.

func NewMagician

func NewMagician() *Character

NewMagician is a factory function that creates a new magician.

func (*Character) Block

func (c *Character) Block() string

Block defends the character from the attack.

func (*Character) Hit

func (c *Character) Hit() string

Hit executes the attack.

func (*Character) Walk

func (c *Character) Walk() string

Walk allows the character to move toward the battlefield.

type WithPotion

type WithPotion struct {
	Actor
}

WithPotion is the character's decorator for enhanced speed.

func UsePotion

func UsePotion(a Actor) *WithPotion

UsePotion is the factory function for the WithPotion decorator.

func (*WithPotion) Effectiveness

func (d *WithPotion) Effectiveness() uint8

Effectiveness returns the effectiveness number of the Buff.

func (*WithPotion) Walk

func (d *WithPotion) Walk() string

Walk executes the enhanced walk.

type WithShield

type WithShield struct {
	Actor
}

WithShield is the character's decorator for enhanced defense.

func EquipShield

func EquipShield(a Actor) *WithShield

EquipShield is the factory function for the WithShield decorator.

func (*WithShield) Block

func (d *WithShield) Block() string

Block executes the enhanced block.

func (*WithShield) Effectiveness

func (d *WithShield) Effectiveness() uint8

Effectiveness returns the effectiveness number of the Buff.

type WithSword

type WithSword struct {
	Actor
}

WithSword is the character's decorator for enhanced attack.

func EquipSword

func EquipSword(a Actor) *WithSword

EquipSword is the factory function for the WithSword decorator.

func (*WithSword) Effectiveness

func (d *WithSword) Effectiveness() uint8

Effectiveness returns the effectiveness number of the Buff.

func (*WithSword) Hit

func (d *WithSword) Hit() string

Hit executes the enhanced attack

Jump to

Keyboard shortcuts

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