Updated version of my tek09 uncon talk, covering the pitfalls of standard database-driven development and ways to apply design patterns to make your code more maintainable, extensible, and flexible.
Updated version of my tek09 uncon talk, covering the pitfalls of standard database-driven development and ways to apply design patterns to make your code more maintainable, extensible, and flexible.
David Stockton, Director of Software Engineering at i3logix@Matthew Thank you for the quick response. In the slides presented, I am trying to figure out which was implied (duck typing or interface). The constructor of the decorators used type hinting of the decorated class, but that type hint was the actual class type. In that case, if I were to write another decorator for the same object, I would not be able to decorate a decorated object.
Also would the idea with the slide example be that there's a __call() implementation that sends any unrecognized calls into the decorated object?
Thanks again, David3 years ago
Are you sure you want to
Matthew Weier O'Phinney, Project Lead, Zend Framework at Zend Technologies, Ltd.@David: There are two approaches: duck typing and interfaces. Interfaces are the more classic approach, and in such an approach, the decorator would implement the interface, and in most instances simply proxy calls back to the mapper being decorated (except when modifying functionality). This approach enforces contracts -- but makes the actual implementation more verbose (no more relying on __cal()).
In duck typing, the idea is that we wouldn't type hint for the object, but instead let the language enforce the interface. In other words, if we call a method not available on the object, PHP barfs. This approach offers more flexibility and is typically more concise, but can lead to difficulties in debugging and maintenance.3 years ago
Are you sure you want to
David Stockton, Director of Software Engineering at i3logixIn the decorator examples, you're using type hinting to ensure you get a PersonMapper or Person class passed into the constructor. However the decorating classes themselves don't meet that same type hint. How would you be able to do something like give the mapper the ability to log information as well as cache the database call?
It seems like the decorators would either need to extend the base class (probably wrong), or the PersonMapper and the CachingPersonMapper would need to both implement some interface and that interface would need to be the type hint for the constructor of the decorator?
Any insight you can provide would be very appreciated. I've been showing this to lots of coworkers but we have all been tripped up by the decorator example.
Thanks, David3 years ago
Are you sure you want to
Саша СтаменковићNice lesson about 'Modelling Your Objects'3 years ago
Play-Doh: Modelling Your ObjectsPresentation Transcript
Play-Doh: Modelling your Objects Matthew Weier O'Phinney Project Lead Zend Framework CodeWorks 2009 Webinar Series 24 July 2009
Goals
Learn to recognize why old habits may be bad
Learn several design patterns that can help you write testable, maintainable code
Learn some techniques for altering existing behavior without rewriting existing code
What we've learned and what we do
Oooh! Let's create the schema!
Write code that uses the DB
Plain Old Mysql (POM)
ActiveRecord
Table/Row Data Gateway
And then …
we start lamenting about performance
we end up refactoring for every new feature (e.g. caching, logging)
we need to go to a Service Oriented Architecture, and effectively refactor twice
STOP THE MADNESS!
Step One
Models are just classes. Create classes.
Models have metadata and behavior; create properties and methods .
class Person { // Metadata protected $_email ; protected $_password ; protected $_username ; // Cheat: use overloading to provide // setters/getters public function __get ( $name ) { } public function __set ( $name , $value ) { } // Behaviors public function authenticate () { } public function logout () { } public function ban () { } }
Step Two
Now start thinking about data persistence .
Identify what data you need to persist
Identify how you'll persist the data
Write code for persisting data (Data Access Layer)
Also would the idea with the slide example be that there's a __call() implementation that sends any unrecognized calls into the decorated object?
Thanks again,
David 3 years ago
In duck typing, the idea is that we wouldn't type hint for the object, but instead let the language enforce the interface. In other words, if we call a method not available on the object, PHP barfs. This approach offers more flexibility and is typically more concise, but can lead to difficulties in debugging and maintenance. 3 years ago
It seems like the decorators would either need to extend the base class (probably wrong), or the PersonMapper and the CachingPersonMapper would need to both implement some interface and that interface would need to be the type hint for the constructor of the decorator?
Any insight you can provide would be very appreciated. I've been showing this to lots of coworkers but we have all been tripped up by the decorator example.
Thanks,
David 3 years ago