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 ¶
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.
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 ¶
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 ¶
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.
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 ¶
State is the common interface for all concrete States that can handle actions and provide a stringified representation of themselves.
type Stateful ¶
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) ChangeState ¶
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 ¶
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.
type UsedState ¶
type UsedState struct {
BaseState
}
UsedState represents the final irreversable State of the Context.
func NewUsedState ¶
NewUsedState constructs the new UsedState and assigns it as the current State of the provided Context.