Skip to content

Instantly share code, notes, and snippets.

@jbrains

jbrains/gist:185020

Created Sep 11, 2009
Embed
What would you like to do?
Controller.initialize() {
parachute = Parachute.new(lander)
detachment_system = DetachmentSystem.new(parachute)
accelerometer = Accelerometer.new()
lander = Lander.new(accelerometer, Altimeter.new())
accelerometer.add_observer(detachment_system)
}
Parachute {
needs a lander
open() {
lander.decelerate()
}
detach() {
if (lander.has_landed == false)
raise "You broke the lander, idiot."
}
}
AccelerationObserver is a role {
handle_acceleration_report(acceleration) {
raise "Subclass responsibility"
}
}
DetachmentSystem acts as AccelerationObserver {
needs a parachute
handle_acceleration_report(acceleration) {}
if (acceleration <= -50.ms2) {
parachute.detach()
}
}
}
Accelerometer acts as Observable {
manages many acceleration_observers
report_acceleration(acceleration) {
acceleration_observers.each() {
each.handle_acceleration_report(acceleration)
}
}
}
Lander {
needs an accelerometer
needs an altimeter
decelerate() {
// I know how much to decelerate by
accelerometer.report_acceleration(how_much)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment