memento

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

Documentation

Overview

Package memento implements the Memento design pattern. The Originator, WeatherMonitor, retrieves current weather forecasts from an external source (represented by RandomWeatherProvider, which implements the ForecastProvider interface) and can create a Snapshot (the Memento) of its current state by delegating to a Caretaker — the SnapshotHistory. The Caretaker is responsible for serializing and deserializing snapshots (mementos) from its internal storage and providing them back to the Originator when needed. The Snapshot remains opaque to the outside world due to base64-encoded state serialization.

Index

Constants

View Source
const (
	Clear byte = iota
	Cloudy
	Rain
	ThunderStorm
	Snow
	SnowStorm
)

Precipitation state enums.

Variables

This section is empty.

Functions

func Run

func Run()

Types

type Caretaker

type Caretaker interface {
	Save(forecast *Forecast)
	Load(index int) *Forecast
}

Caretaker defines a common interface for objects responsible for storing and restoring snapshots (mementos).

type Forecast

type Forecast struct {
	Temperature   float32
	Humidity      float32
	WindSpeed     float32
	Pressure      int
	Precipitation Precipitation
}

Forecast is the changeable state, that represents the specific weather forecast (and is the subject of memento).

func (*Forecast) String

func (f *Forecast) String() string

String implements Stringer interface for the Forecast.

type ForecastProvider

type ForecastProvider interface {
	Retrieve() *Forecast
}

ForecastProvider defines a common interface for components that can supply weather forecast data.

type Originator

type Originator interface {
	Update(*Forecast)
	Print()
	Snapshoter
	ForecastProvider
}

Originator represents an entity whose state can be saved and restored using snapshots (mementos).

type Precipitation

type Precipitation struct {
	State byte
}

Precipitation is the stringifiable part of the Forecast.

func (*Precipitation) String

func (p *Precipitation) String() string

String implements Stringer interface for the Precipitation.

type RandomWeatherProvider

type RandomWeatherProvider struct{}

RandomWeatherProvider implements ForecastProvider interface and generates random forecast.

func (*RandomWeatherProvider) Retrieve

func (r *RandomWeatherProvider) Retrieve() *Forecast

Retrieve creates the random weather Forecast.

type Snapshot

type Snapshot struct {
	Serialized string
}

Snapshot is a serialized memento capturing the state of a Forecast.

func NewSnapshot

func NewSnapshot(serialized string) *Snapshot

NewSnapshot is the Snapshot constructor.

type SnapshotHistory

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

SnapshotHistory is the concrete Caretaker that manages Snapshot mementos — aggregating, serializing, and restoring them as needed.

func (*SnapshotHistory) Load

func (s *SnapshotHistory) Load(index int) *Forecast

Load deserializes and returns the state from the specific Snapshot memento by index from the internal storage.

func (*SnapshotHistory) Save

func (s *SnapshotHistory) Save(forecast *Forecast)

Save serializes and stores the specific Forecast state into the Snapshot memento inside the internal storage.

func (*SnapshotHistory) String

func (s *SnapshotHistory) String() string

String implements Stringer interface for SnapshotHistory.

type Snapshoter

type Snapshoter interface {
	TakeSnapshot()
	RestoreSnapshot(index int)
}

Snapshoter defines the ability to create and restore snapshots of internal state.

type WeatherMonitor

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

WeatherMonitor is the specific Originator, responsable for the retrieval of the current weather Forecast from the external forecast providers. It is also allowed to Print the current Forecast, save Forecast snapshot through the Caretaker and restore the Forecast state from the snapshot.

func NewWeatherMonitor

func NewWeatherMonitor(provider ForecastProvider) *WeatherMonitor

NewWeatherMonitor constructs the new WeatherMonitor structure.

func (*WeatherMonitor) Print

func (w *WeatherMonitor) Print()

Print allows to print the current forecast state and the Caretaker state to screen.

func (*WeatherMonitor) RestoreSnapshot

func (w *WeatherMonitor) RestoreSnapshot(index int)

RestoreSnapshot updates the current Forecast from the Snapshot, retrieved from the Caretaker.

func (*WeatherMonitor) Retrieve

func (w *WeatherMonitor) Retrieve() *Forecast

Retrieve is fetching the current weather forecast from the external ForecastProvider and stores it as the current Forecast state.

func (*WeatherMonitor) TakeSnapshot

func (w *WeatherMonitor) TakeSnapshot()

TakeSnapshot saves current Forecast as the new Snapshot memento, with help of the Caretaker.

func (*WeatherMonitor) Update

func (w *WeatherMonitor) Update(forecast *Forecast)

Update mutates the current forecast in the Weather Monitor.

Jump to

Keyboard shortcuts

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