[tsmith] Observer is one of the more complex patterns.
[tsmith] You use it when you want one or more objects (the Observers) to be aware of events occuring in another object they actively observe (the Subject).
[tsmith] It’s a form of event handling, actually.
[tsmith] I don’t exactly know how to describe it further, but if you’re interested, I could show you an example and you could ask any questions.
[Arth] it could be used for watching user input
[tsmith] Maybe.
[tsmith] Most PHP apps simply are not complex enough to truly benefit.
[tsmith] One real-world place I used it was in SpeedFC’s shopping cart app.
[tsmith] Whenever the user would complete the checkout process, the Shipping, PrintReceipt, EmailReceipt, and Warehouse classes would observe it and do their parts occordingly, without the end-dev having to implictly instruct them to do something.
[tsmith] In situations like that, hwere one or more parts should care what another part is doing, the Observer pattern can *greatly* add in the Architectural Encapsulation of complex interactions.
[tsmith] Which usually makes complex apps, less complex and more maintainable and extensible; e.g. elegant, if not beautiful.
[Arth] ha automate the automation
[tsmith] Precisely.
[Arth] friggin brillliant
[tsmith] A good metaphor for a mechanical implementation of the observer pattern is the modern car.
[tsmith] You have an engine that the drivetrain must observe (in a loose sense); to speed up when the engine revs, slow down when it slows, etc.
[tsmith] You have a drive train that also observes the steering wheel, adjusting as the wheel spins, right?
[Arth] yea
[tsmith] Various parts have to observe the gearshaft…
[tsmith] Because of this, I developed the driving app to exemplify all this stuff.
[tsmith] I thought it’d take me a weekend to make but it ended up taking more like 5 weeks (part time)!
[tsmith] creating a car simulator is more complicated than i thought
[Arth] damn
[tsmith] Open https://github.com/phpexpertsinc/driving_app/blob/master/lib/Cars/scaffolds/CarPartSubject.abstract.php in one window
[tsmith] Then open up these, in separate windows/tabs:
[tsmith] https://github.com/phpexpertsinc/driving_app/blob/master/lib/Cars/misc/CarFactory.inc.php
[tsmith] https://github.com/phpexpertsinc/driving_app/blob/master/lib/Cars/parts/GearShaft.part.php
[tsmith] https://github.com/phpexpertsinc/driving_app/blob/master/lib/Cars/scaffolds/DriveTrain.abstract.php
[tsmith] Let me know when you’re ready.
[Arth] k
[tsmith] Look at DriveTrain first.
[Arth] k
[tsmith] The only important function is line 130: public function update()
[tsmith] This is where the various objects it is observing update it as to their situation.
[tsmith] Via this mechanism, you can see that DriveTrain always keeps abreast of the status of GearShaft, see?
[tsmith] It also changes the states of its attached wheels whenever it is notified of a change in the engine’s status.
[Arth] yea
[tsmith] Now bounce on over to GearShaft.
[tsmith] look at the first function: changeGear()
[tsmith] After it successfully changes a gear, see how it notify()s its observers?
[tsmith] The code for that is actually ridiculously simple and is located at:
[tsmith] https://github.com/phpexpertsinc/driving_app/blob/master/lib/Cars/scaffolds/CarPartSubject.abstract.php#L58
[Arth] line 58 on carpartsubject
[Arth] wow
[tsmith] It looks so simple until you play around with it and realize it’s so powerful.
[Arth] this has a front end as well ?
[tsmith] what do you mean?
[Arth] interaction with a user
[tsmith] Using the observer pattern allows you to create largely atomic systems of interoperating classes, without there needing to be some master chief who orchestrates them all.
[tsmith] In the driving app, all you have to do is tell the car to drive, and it just works.
[tsmith] here let me show you
[tsmith] https://github.com/phpexpertsinc/driving_app/blob/master/index.php
[tsmith]
[tsmith] $car = CarFactory::loadCar($_GET[‘car’]);
[tsmith] 26
[tsmith] $car-]downShift();
[tsmith] 27
[tsmith] $car-]turnOn();
[tsmith]
[tsmith] / Get into park.
[tsmith] while ($car-]upShift() != GearShaft::GEAR_PARK);
[tsmith]
[tsmith] $car-]refuel();
[tsmith] / Get into drive.
[tsmith] while ($car-]downShift() != GearShaft::GEAR_DRIVE);
[Arth] wow
[tsmith] $car-]accelerate(1.0, 60);
[tsmith] / Drive for 60 minutes.
[tsmith] $car-]drive(60, 0.0);
[tsmith] / Get into park.
[tsmith] while ($car-]upShift() != GearShaft::GEAR_PARK);
[tsmith] echo “Miles driven: ” . Car::formatStat($car-]getMileage()) . ” miles.\n”;
[tsmith] echo “Fuel remaining: ” . Car::formatStat($car-]getFuelRemaining()) . ” gallons.\n”;
[tsmith] $car-]turnOff();
[Arth] omg you even have all that holy …. you could do so much with this and the posabilities of jsut this app alone for car manufactures