Steven Mercatante
Steven Mercatante

Reputation: 25305

What design pattern is this?

For the past few days I've been writing classes that at first I thought adhered to the Command design pattern, but I've since modified them and am curious as to which pattern they really match up with (if any).

A basic example is a class I'm using to query the Facebook api to fetch a page's feed. My class looks like this:

class FetchPageFeedCommand extends Command {

    public $feed;

    private $pageId;

    public function __construct($pageId) {
        $this->pageId = $pageId;
    }

    public function execute() {
        if ($feed = Facebook::api('/page/feed') /* psuedo code */ ) {
             $this->feed = $feed;
             return true;
        } else {
             return false;
        }

    }
}

I would then use the class like this:

$com = new FetchPageFeedCommand(12345);
if ($com->execute()) {
   $feed = $com->feed;
   print_r($feed);
}

From what I understand, a Command object should take a receiver object, which mine does not. And it seems that both my client and invoker are the same. Add to that the fact that I payload data with public variables and I feel like this definitely doesn't match the Command pattern.

To further muddle things, I added some functionality to the Command superclass that let's me keep track of errors that happen. For example:

    public function execute() {
        if ($feed = Facebook::api('/page/feed') /* psuedo code */) {
             $this->feed = $feed;
             return true;
        } else {
             $this->addError('Could not fetch feed'); // Error management
             return false;
        }

I would then test for errors with $com->hasErrors() and $com->getErrors()

So far this pattern has been working quite well for me. I know that the details of a design pattern aren't always written in stone, and that it's more important to solve a problem than worry about semantics, but I'm genuinely curious about this and want to see if I can improve my code (or if I'm somehow digging myself a grave).

Upvotes: 3

Views: 295

Answers (3)

c0rnh0li0
c0rnh0li0

Reputation: 149

The command pattern is useful for delayed execution, callable with arguments that are independent of it's creation or location.

What's probably more suitable for you is an facade pattern.

Example:


class MyFacebookApi {
  function __construct(Facebook $fb)
  function getFeed()
  function sendWallpost($msg)
  function getError()
}

Upvotes: 1

Tom Carter
Tom Carter

Reputation: 2976

Looks like the Command pattern to me. The client is the one who creates the Command, the initiator is the one who calls the execute method and the receiver is the one who implements the execute.

Just because the client and initiator are the same in your instance doesn't seem to change the pattern for me. I think the important part is that you create the command passing in the parameters in the constructor or by setting properties. The command can later be executed by some other object (the intitiator) simply by calling the execute method of the command interface - it doesn't need to know the parameters at that stage.

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

It looks a tiny bit like the beginnings of a Strategy pattern to me.

Upvotes: 1

Related Questions