OOAD Thought Processing
Concept → Owner
- Behavior → Object
- Polymorphism → Virtual methods
- Extension → Subclassing
- Encapsulation → Methods inside class
Julia Thought Processing
Concept → Owner
- Behavior → Generic function
- Polymorphism → Multiple dispatch
- Extension → Add new methods
- Encapsulation → Types + modules
OO Design Thinking
- Identify entities (classes)
- Attach behavior to them
- Use inheritance for variation
Julia Design Thinking
- Identify operations (functions)
- Define data types
- Implement methods for combinations of types
Worldview Comparison
OOP worldview
The world is made of interacting objects.
Julia worldview
The world is made of operations applied to data.
The State Pattern in Pure Julia Style
Enjoy the State pattern written in Julia.
Note that this works without the context class of the original State Pattern, which is typically implemented using object-oriented languages like C++, Java, or Python.
abstract type ChickenState end
struct Uncleaned <: ChickenState end
struct Washed <: ChickenState end
struct Marinated <: ChickenState end
struct Cooked <: ChickenState end
function wash(::Uncleaned)
println("Chicken is getting cleaned → Washed")
sleep(2)
return Washed()
end
wash(s::ChickenState) = s # default: no change
function marinate(::Washed)
println("Chicken is being marinated → Marinated")
sleep(2)
return Marinated()
end
marinate(s::ChickenState) = s
function cook(::Marinated)
println("Chicken is being cooked → Cooked")
sleep(4)
return Cooked()
end
cook(s::ChickenState) = s
function serve(::Cooked)
println("Chicken is being served. Final state.")
return Cooked()
end
serve(s::ChickenState) = s
state = Uncleaned()
state = wash(state)
state = marinate(state)
state = cook(state)
state = serve(state)
Here's the Python implementation of the State Design Pattern.
Feel the difference - Pythonian way and the Julian way.

No comments:
Post a Comment