visitor

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: 2 Imported by: 0

Documentation

Overview

Package visitor implements the Visitor design pattern. The Encounterer interface defines the common interface for all visitors (heroes) that can interact with various elements (monsters) that implement Confronter interface. The double dispatch mechanism is achieved through the Confront method, allowing each hero and monster combination to define unique interaction logic. In this implementation, different types of heroes (Encounterers) enter a haunted house and confront various supernatural creatures (Confronters).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run()

Types

type Confronter

type Confronter interface {
	Confront(v Encounterer)
	Hit(strength int)
	IsAlive() bool
	fmt.Stringer
}

Confronter defines the common interface for all elements that can be confronted by Encounterers (visitors). It provides methods for engaging in combat, taking damage, reporting their state, and checking whether they are still alive.

type Demon

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

Demon represents a powerful infernal creature that thrives on chaos and fire. Highly resistant to physical attacks; faith-based assaults are most effective.

func (*Demon) Confront

func (d *Demon) Confront(v Encounterer)

Confront allows the Demon to face an Encounterer through double dispatch.

func (*Demon) Hit

func (d *Demon) Hit(strength int)

Hit reduces the Demon’s life by the given strength value.

func (*Demon) IsAlive

func (d *Demon) IsAlive() bool

IsAlive returns true if the Demon still has life points.

func (*Demon) String

func (d *Demon) String() string

String returns a textual representation of the Demon’s current condition.

type Encounterer

type Encounterer interface {
	FaceGhost(v Confronter)
	FaceDemon(v Confronter)
	FaceVampire(v Confronter)
	FaceZombie(v Confronter)
	FaceWitch(v Confronter)
	fmt.Stringer
}

Encounterer defines the common interface for all visitors that can interact with different Confronters using double dispatch.

type Extendable

type Extendable interface {
	Add(participant any) Extendable
}

Extendable defines the ability to add new participants into a composite structure (Encounterers and Confronters).

type Ghost

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

Ghost represents an ethereal, floating spirit with limited ectoplasmic energy. Immune to bullets and blades, but weak to scientific gadgets and faith.

func (*Ghost) Confront

func (g *Ghost) Confront(v Encounterer)

Confront allows the Ghost to face an Encounterer through double dispatch.

func (*Ghost) Hit

func (g *Ghost) Hit(strength int)

Hit reduces the Ghost’s ectoplasmic energy by the given strength value.

func (*Ghost) IsAlive

func (g *Ghost) IsAlive() bool

IsAlive returns true if the Ghost still has ectoplasmic energy.

func (*Ghost) String

func (g *Ghost) String() string

String returns a textual representation of the Ghost’s current state.

type GhostBuster

type GhostBuster struct{}

GhostBuster is a modern hero who relies on gadgets and proton technology. Extremely effective against Ghosts; less so against physical foes.

func (*GhostBuster) FaceDemon

func (g *GhostBuster) FaceDemon(v Confronter)

FaceDemon allows the GhostBuster to interact with the Demon Confronter.

func (*GhostBuster) FaceGhost

func (g *GhostBuster) FaceGhost(v Confronter)

FaceGhost allows the GhostBuster to interact with the Ghost Confronter.

func (*GhostBuster) FaceVampire

func (g *GhostBuster) FaceVampire(v Confronter)

FaceVampire allows the GhostBuster to interact with the Vampire Confronter.

func (*GhostBuster) FaceWitch

func (g *GhostBuster) FaceWitch(v Confronter)

FaceWitch allows the GhostBuster to interact with the Witch Confronter.

func (*GhostBuster) FaceZombie

func (g *GhostBuster) FaceZombie(v Confronter)

FaceZombie allows the GhostBuster to interact with the Zombie Confronter.

func (*GhostBuster) String

func (g *GhostBuster) String() string

String returns the stringified name of the GhostBuster.

type HauntedHouse

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

HauntedHouse represents a collection of Confronters—monsters that can be encountered by a Party of Encounters. It provides methods for adding Confronters, initiating encounters, and displaying the results of battles.

func NewHauntedHouse

func NewHauntedHouse() *HauntedHouse

NewHauntedHouse creates the new Haunted House with Confronters.

func (*HauntedHouse) Add

func (h *HauntedHouse) Add(enemy any) Extendable

Add allows to add the Confronter in the House.

func (*HauntedHouse) Visit

func (h *HauntedHouse) Visit(party *Party)

Visit allows a Party of Encounterers to confront all Confronters within the HauntedHouse. Each hero sequentially faces the enemies, and the final battle results are displayed afterward.

type Party

type Party struct {
	Heroes []Encounterer
}

Party is the composite for the Encounterers party, with the ability of adding to party and printing the information about it.

func NewParty

func NewParty() *Party

NewParty creates the new Party of Encounterers.

func (*Party) Add

func (p *Party) Add(hero any) Extendable

Add allows to add the Encounterers to the Party.

func (*Party) String

func (p *Party) String() string

String returns a formatted string describing all heroes in the Party.

type Priest

type Priest struct{}

Priest represents a holy Encounterer who uses faith and ritual to fight evil. His abilities are most effective against Demons and Vampires, less so against Zombies or Witches.

func (*Priest) FaceDemon

func (p *Priest) FaceDemon(v Confronter)

FaceDemon allows the Priest to interact with the Demon Confronter.

func (*Priest) FaceGhost

func (p *Priest) FaceGhost(v Confronter)

FaceGhost allows the Priest to interact with the Ghost Confronter.

func (*Priest) FaceVampire

func (p *Priest) FaceVampire(v Confronter)

FaceVampire allows the Priest to interact with the Vampire Confronter.

func (*Priest) FaceWitch

func (p *Priest) FaceWitch(v Confronter)

FaceWitch allows the Priest to interact with the Witch Confronter.

func (*Priest) FaceZombie

func (p *Priest) FaceZombie(v Confronter)

FaceZombie allows the Priest to interact with the Zombie Confronter.

func (*Priest) String

func (p *Priest) String() string

String returns the stringified name of the Priest.

type Soldier

type Soldier struct{}

Soldier represents a disciplined fighter with raw firepower and tactical efficiency. Best suited for physical threats; struggles against ethereal beings.

func (*Soldier) FaceDemon

func (s *Soldier) FaceDemon(v Confronter)

FaceDemon allows the Soldier to interact with the Demon Confronter.

func (*Soldier) FaceGhost

func (s *Soldier) FaceGhost(v Confronter)

FaceGhost allows the Soldier to interact with the Ghost Confronter.

func (*Soldier) FaceVampire

func (s *Soldier) FaceVampire(v Confronter)

FaceVampire allows the Soldier to interact with the Vampire Confronter.

func (*Soldier) FaceWitch

func (s *Soldier) FaceWitch(v Confronter)

FaceWitch allows the Soldier to interact with the Witch Confronter.

func (*Soldier) FaceZombie

func (s *Soldier) FaceZombie(v Confronter)

FaceZombie allows the Soldier to interact with the Zombie Confronter.

func (*Soldier) String

func (s *Soldier) String() string

String returns the stringified name of the Soldier.

type Vampire

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

Vampire represents an undead predator who feeds on the living. Weak against holy items, sunlight, and anyone carrying garlic.

func (*Vampire) Confront

func (v *Vampire) Confront(hv Encounterer)

Confront allows the Vampire to face an Encounterer through double dispatch.

func (*Vampire) Hit

func (v *Vampire) Hit(strength int)

Hit reduces the Vampire’s life by the given strength value.

func (*Vampire) IsAlive

func (v *Vampire) IsAlive() bool

IsAlive returns true if the Vampire still has life points.

func (*Vampire) String

func (v *Vampire) String() string

String returns a textual representation of the Vampire’s current condition.

type VampireHunter

type VampireHunter struct{}

VampireHunter specializes in exterminating vampires, but takes on other foes with variable success.

func (*VampireHunter) FaceDemon

func (vh *VampireHunter) FaceDemon(v Confronter)

FaceDemon allows the VampireHunter to interact with the Demon Confronter.

func (*VampireHunter) FaceGhost

func (vh *VampireHunter) FaceGhost(v Confronter)

FaceGhost allows the VampireHunter to interact with the Ghost Confronter.

func (*VampireHunter) FaceVampire

func (vh *VampireHunter) FaceVampire(v Confronter)

FaceVampire allows the VampireHunter to interact with the Vampire Confronter.

func (*VampireHunter) FaceWitch

func (vh *VampireHunter) FaceWitch(v Confronter)

FaceWitch allows the VampireHunter to interact with the Witch Confronter.

func (*VampireHunter) FaceZombie

func (vh *VampireHunter) FaceZombie(v Confronter)

FaceZombie allows the VampireHunter to interact with the Zombie Confronter.

func (*VampireHunter) String

func (v *VampireHunter) String() string

String returns the stringified name of the VampireHunter.

type Witch

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

Witch represents a cunning spellcaster skilled in curses and mischief. Difficult to kill, but vulnerable to precision and disbelief.

func (*Witch) Confront

func (w *Witch) Confront(v Encounterer)

Confront allows the Witch to face an Encounterer through double dispatch.

func (*Witch) Hit

func (w *Witch) Hit(strength int)

Hit reduces the Witch’s life by the given strength value.

func (*Witch) IsAlive

func (w *Witch) IsAlive() bool

IsAlive returns true if the Witch still has life points.

func (*Witch) String

func (w *Witch) String() string

String returns a textual representation of the Witch’s current condition.

type Zombie

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

Zombie represents a reanimated corpse driven by hunger and confusion. Easy to kill, but is very interested in brains.

func (*Zombie) Confront

func (z *Zombie) Confront(v Encounterer)

Confront allows the Zombie to face an Encounterer through double dispatch.

func (*Zombie) Hit

func (z *Zombie) Hit(strength int)

Hit reduces the Zombie’s strength by the given amount.

func (*Zombie) IsAlive

func (z *Zombie) IsAlive() bool

IsAlive returns true if the Zombie still has remaining strength.

func (*Zombie) String

func (z *Zombie) String() string

String returns a textual representation of the Zombie’s current condition.

Jump to

Keyboard shortcuts

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