🔮 JULIA ASTROMANCY DIFFICULTY: Adept Behavioral

State

Model the stellar evolution lifecycles with the State pattern in Julia.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Cosmic Alchemy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// JULIA PATTERN //
# State in Julia: Stellar Evolution
abstract type StarState end

struct Protostar <: StarState end
struct MainSequence <: StarState end
struct RedGiant <: StarState end
struct WhiteDwarf <: StarState end

mutable struct EvolvingStar
    state::StarState
    mass::Float64
end

evolve!(star::EvolvingStar, ::Protostar) = star.state = MainSequence()
evolve!(star::EvolvingStar, ::MainSequence) = star.state = RedGiant()
evolve!(star::EvolvingStar, ::RedGiant) = star.state = WhiteDwarf()
evolve!(star::EvolvingStar, ::WhiteDwarf) = println("Star is dead. Cannot evolve further.")

function time_pass!(star::EvolvingStar)
    println("Billions of years pass... Current state: ", typeof(star.state))
    evolve!(star, star.state)
end

# Usage
sun = EvolvingStar(Protostar(), 1.0)
time_pass!(sun)
time_pass!(sun)
time_pass!(sun)
time_pass!(sun)
Runic Tags: #behavioral #state #julia #astromancy