state

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 state implements the State design pattern. The Context interface defines the common Actioner behavior used to handle actions on a specific subject (in this case – the theatre Ticket). The State interface shares these common methods across all concrete states. Each specific State knows how to handle transitions to other states or can fall back to the default behavior described in the BaseState struct. Every Context (Ticket) delegates the execution of its actions to the current State it holds.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run()

Types

type Actioner

type Actioner interface {
	Reserve()
	Buy()
	CheckIn()
	Use()
	Return()
}

Actioner defines the common set of actions shared by Contexts and States.

type AvailableState

type AvailableState struct {
	BaseState
}

AvailableState represents the fresh new state of the Context. It could be bought or reserved.

func NewAvailableState

func NewAvailableState(subject Context) *AvailableState

NewAvailableState constructs the new AvailableState and assigns it as the current State of the provided Context.

func (*AvailableState) Buy

func (as *AvailableState) Buy()

Buy transitions the Context from Available to Paid.

func (*AvailableState) Reserve

func (as *AvailableState) Reserve()

Reserve transitions the Context from Available to Reserved.

func (*AvailableState) String

func (as *AvailableState) String() string

String returns the human-readable name of the current State.

type BaseState

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

BaseState provides the default behavior shared by all concrete States. It acts as an abstract foundation — concrete States can override only the actions they support while inheriting the default ones.

func (*BaseState) Buy

func (b *BaseState) Buy()

Buy defines the default behavior for the Buy action in all States.

func (*BaseState) CheckIn

func (b *BaseState) CheckIn()

CheckIn defines the default behavior for the CheckIn action in all States.

func (*BaseState) Reserve

func (b *BaseState) Reserve()

Reserve defines the default behavior for the Reserve action in all States.

func (*BaseState) Return

func (b *BaseState) Return()

Return defines the default behavior for the Return action in all States.

func (*BaseState) String

func (b *BaseState) String() string

String should be overridden by each concrete State to return its name.

func (*BaseState) Use

func (b *BaseState) Use()

Use defines the default behavior for the Use action in all States.

type CancelledState

type CancelledState struct {
	AvailableState
}

CancelledState represents the liminal State, that could be considered as an alias of the AvailableState.

func NewCancelledState

func NewCancelledState(subject Context) *CancelledState

NewCancelledState constructs the new CancelledState and assigns it as the current State of the provided Context.

func (*CancelledState) String

func (cs *CancelledState) String() string

String returns the human-readable name of the current State.

type Context

type Context interface {
	Stateful
	Actioner
}

Context is the common interface for all concrete Contexts that use States to perform actions.

type PaidState

type PaidState struct {
	BaseState
}

PaidState represents the purchased state of the Context. After this, it can either be checked in (used) or refunded.

func NewPaidState

func NewPaidState(subject Context) *PaidState

NewPaidState constructs the new PaidState and assigns it as the current State of the provided Context.

func (*PaidState) CheckIn

func (ps *PaidState) CheckIn()

CheckIn transitions the Context from Paid to Used.

func (*PaidState) Return

func (ps *PaidState) Return()

Return transitions the Context from Paid to Cancelled (refund).

func (*PaidState) String

func (ps *PaidState) String() string

String returns the human-readable name of the current State.

type ReservedState

type ReservedState struct {
	BaseState
}

ReservedState represents the "booked" mode for the Context. However, it is still can be returned, as well as bought.

func NewReservedState

func NewReservedState(subject Context) *ReservedState

NewReservedState constructs the new ReservedState and assigns it as the current State of the provided Context.

func (*ReservedState) Buy

func (rs *ReservedState) Buy()

Buy transitions the Context from Reserved to Paid.

func (*ReservedState) Return

func (rs *ReservedState) Return()

Return transitions the Context from Reserved to Cancelled.

func (*ReservedState) String

func (rs *ReservedState) String() string

String returns the human-readable name of the current State.

type State

type State interface {
	Actioner
	fmt.Stringer
}

State is the common interface for all concrete States that can handle actions and provide a stringified representation of themselves.

type Stateful

type Stateful interface {
	ChangeState(state Actioner)
	GetCurrentState() Actioner
}

Stateful defines the methods required to change or retrieve the current state of a structure.

type Ticket

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

Ticket is the specific Context that is able to execute actions, by delegating them to the current internal State.

func NewTicket

func NewTicket() *Ticket

NewTicket creates the new Ticket in it's default State (Available).

func (*Ticket) Buy

func (t *Ticket) Buy()

Buy delegates Buy operation to the current State.

func (*Ticket) ChangeState

func (t *Ticket) ChangeState(state Actioner)

ChangeState updates the current State of the Ticket.

func (*Ticket) CheckIn

func (t *Ticket) CheckIn()

CheckIn delegates CheckIn operation to the current State.

func (*Ticket) GetCurrentState

func (t *Ticket) GetCurrentState() Actioner

GetCurrentState prints and returns the current State of the Ticket.

func (*Ticket) Reserve

func (t *Ticket) Reserve()

Reserve delegates Reserve operation to the current State.

func (*Ticket) Return

func (t *Ticket) Return()

Return delegates Return operation to the current State.

func (*Ticket) Use

func (t *Ticket) Use()

Use delegates Use operation to the current State.

type UsedState

type UsedState struct {
	BaseState
}

UsedState represents the final irreversable State of the Context.

func NewUsedState

func NewUsedState(subject Context) *UsedState

NewUsedState constructs the new UsedState and assigns it as the current State of the provided Context.

func (*UsedState) String

func (us *UsedState) String() string

String returns the human-readable name of the current State.

Jump to

Keyboard shortcuts

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