KevinM1 Posted May 12, 2007 Share Posted May 12, 2007 I'm re-re-reading reading my copy of "PHP5: Objects, Patterns, and Practice" because, dammit, I'm going to figure out OOP (at least, as it pertains to PHP). I've come across the topic of delegation twice now: once when the author was describing a use of the __call() method, and once when he first introduces the Strategy pattern as an example of composition. Unfortunately, the author doesn't really go into why he used delegation in those two examples (at least, not explicitly). No mention of code smells, or anything along those lines. So, is there any general rule of thumb when it comes to deciding when to implement delegation? Any signs/code smells to watch for? Or is it more along the lines of "You'll know when to use it when you get to that point?" For any fellow newbie who may be lurking and reading along, delegation is more of less something like this: <?php class Example{ private $delegate; public function doSomething(Delegate $delegate){ return $this->delegate->something($this); } } class Delegate{ private $property; public function something(Example $example){ return $this->property . "blah blah blah\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51122-question-about-delegation/ Share on other sites More sharing options...
lee20 Posted May 16, 2007 Share Posted May 16, 2007 I think of delegation as a way to extend the methods of an object. This allows you to create flexible "worker" classes that you will be able to resuse with less or no modifications because the worker class only handles logic specific to itself. class Delegator { private $worker; function doSomething($param) { // Logic above the scope of worker goes here $this->worker->doSomething($param); // Logic above the scope of worker goes here } } Logic above the scope of a worker may include anything from error handling, i18n, l10n, or caching to name a few. Consider this pseudo code example: class DB { // delegator protected $db; public function Query($sql) { if (sql is cached) return cache; $res = $this->db->query($sql); if (query is successful) { add sql to cache return $res; } else show an error } } class MySQL { // worker public function Query($sql) { return mysql_query($sql); } } class PgSQL { // worker public function Query($sql) { return pg_query($sql); } } Essentially the worker's query function isn't that useful. But a complete "worker" class would be very useful and afterwards you can take your worker and plug it into many other applications that perhaps do not need caching or handle errors differenctly, etc, etc. Not to confuse the matter, but a delegator can also be a worker. But you have a tiered seperation of logic that is very flexible. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/51122-question-about-delegation/#findComment-254804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.