Jump to content

Recommended Posts

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";
   }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/51122-question-about-delegation/
Share on other sites

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.