Play-Doh: Modelling Your Objects

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

4 comments

Comments 1 - 4 of 4 previous next Post a comment

  • + dstockto David Stockton 5 months ago
    @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,
    David
  • + weierophinney Matthew Weier O'Phinney 5 months ago
    @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.
  • + dstockto David Stockton 5 months ago
    In 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,
    David
  • + guestcebb8d5 Саша Стаменковић 6 months ago
    Nice lesson about ’Modelling Your Objects’
Post a comment
Embed Video
Edit your comment Cancel

18 Favorites

Play-Doh: Modelling Your Objects - Presentation Transcript

  1. Play-Doh: Modelling your Objects Matthew Weier O'Phinney Project Lead Zend Framework CodeWorks 2009 Webinar Series 24 July 2009
  2. 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
  3. What we've learned and what we do
  4. Oooh! Let's create the schema!
  5. Write code that uses the DB
    • Plain Old Mysql (POM)
    • ActiveRecord
    • Table/Row Data Gateway
  6. 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
  7. STOP THE MADNESS!
  8. Step One
  9. Models are just classes. Create classes.
  10. Models have metadata and behavior; create properties and methods .
  11. 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 () { } }
  12. Step Two
  13. 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)
  14. CREATE TABLE person ( username VARCHAR PRIMARY KEY , password VARCHAR , email VARCHAR , ); class PersonTable extends Zend_Db_Table_Abstract { protected $_name = 'person' ; protected $_primary = 'username' ; }
  15. Step Three
  16. Map your model to your data.
    • Common approaches:
      • Transaction Script (can even use POM)
      • Table Module (often with ActiveRecord or Table Data Gateway)
      • Data Mapper / ORM
  17. class PersonMapper { public function save ( Person $person ) { $data = array ( 'username' => $person -> username , 'password' => $person -> password , 'email' => $person -> email , ); $this -> getTable ()-> save ( $data ); } public function fetch ( $username = null ); public function getTable (); public function setTable ( $table ); }
    • Some notes:
      • Data !== Relational Database. Data could come from a document database, filesystem, cache, or web service.
      • Choose an ORM that allows you to generate your schema from your entities; allows you to easily model first, and persistence comes for free.
  18. Step Four
  19. Move business and application logic to a Service Layer .
  20. Applications are like onions; they have layers. Photo © 2008, Mike Chaput-Branson
  21. The Service Layer provides application logic on top of your models
  22. Service layer in perspective Data Access Objects and Data store(s) Data Mappers Domain Models Service Layer
    • Benefits to a Service Layer:
      • Allows easy consumption of the application via your MVC layer
      • Allows easy re-use of your application via services
      • Write CLI scripts that consume the Service Layer
    • What kind of application logic?
      • Validation and filtering
      • Authentication and Authorization
      • Transactions and interactions between model entities
  23. class PersonService { public function create ( array $data ) { $person = new Person (); if (! $data = $this -> getValidator () -> isValid ( $data ) ) { throw new InvalidArgumentException (); } $person -> username = $data [ 'username' ]; $person -> password = $data [ 'password' ]; $person -> email = $data [ 'email' ]; $this -> getMapper ()-> save ( $person ); return $person ; } }
  24. Decorating for Fun and Profit
  25. Decorators allow you to add or alter functionality of an existing class.
    • Typical decorator:
      • Implements same interface(s) of class being decorated
      • Uses overloading to proxy to decorated class
      • Override specific behavior you wish to modify or enhance
      • Add new behavior that uses existing behavior in the decorated class
  26. Refactor to add caching? No! Decorate!
  27. class CachingPersonMapper { protected $_mapper ; public function __construct ( PersonMapper $mapper ) { $this -> _mapper = $mapper ; } public function fetch ( $username ) { $cache = $this -> getCache (); if (! $person = $cache -> load ( $username )) { $person = $this -> _mapper -> fetch ( $username ); $cache -> save ( $person , $username ); } return $person ; } }
  28. Refactor to provide alternate return formats? (e.g., JSON vs XML vs ...) No! Decorate!
  29. class JsonPerson { protected $_person ; public function __construct ( Person $person ) { $this -> _person = $person ; } public function __toString () { $data = array ( 'username' => $this -> _person -> username , 'email' => $this -> _person -> email , ); return json_encode ( $data ); } }
  30. Nicely formed objects
    • Rebuilding and refactoring is costly and painful
    • Good OOP and encapsulation CAN make your life easier
    • Testing is easier than debugging
    • Choose a good ORM to expedite development. (Doctrine is an excellent choice)
  31. Think beyond the DB!
  32. Thank you.

+ Matthew Weier O'PhinneyMatthew Weier O'Phinney, 6 months ago

custom

8863 views, 18 favs, 9 embeds more stats

Updated version of my tek09 uncon talk, covering th more

More info about this presentation

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

  • Total Views 8863
    • 8608 on SlideShare
    • 255 from embeds
  • Comments 4
  • Favorites 18
  • Downloads 141
Most viewed embeds
  • 173 views on http://disole.wordpress.com
  • 22 views on http://ignar.name
  • 21 views on http://shinazana.wordpress.com
  • 13 views on http://doriangray.com.ua
  • 12 views on http://bradley-holt.blogspot.com

more

All embeds
  • 173 views on http://disole.wordpress.com
  • 22 views on http://ignar.name
  • 21 views on http://shinazana.wordpress.com
  • 13 views on http://doriangray.com.ua
  • 12 views on http://bradley-holt.blogspot.com
  • 11 views on http://blog.meshin.ru
  • 1 views on http://74.125.19.132
  • 1 views on http://localhost
  • 1 views on http://blog.ignar.name

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories