views:

41

answers:

2

Hi all,

I'm working on a controller that handles logins for a Web app. These logins will come from multiple clients but will all contain the same data. However, depending on the client, this data will be interpreted into common entities for our webapp differently.

For instance, we have a user code that gets sent in, and in one case we may use the first four digits of the code, and in another case 12 digits of the code to map to a field on a User entity.

Instead of handling this all in the controller and having big nasty if blocks of logic, I would like to use a pattern to handle how this information gets ingested into our application.

What are your opinions?

+1  A: 

It's hard to understand exactly what the problem is without knowing more about how your program currently works. However, if I understand correctly, you don't really need a 'pattern' as such.

I would simply refactor the "big nasty blocks of if logic" into a class that handles deciding what to do with the data. Perhaps something like this (PHP code):

class UserCodeHandler {

 private $userCode;
 private $client;

 public function __construct($userCode, $client) {

  $this->userCode = $userCode;
  $this->client = $client;
 }

 public function manipulateData() {

  switch ($this->client) {
   case 'clientA':
    $this->doSomething();
    break;
   case 'clientB':
    $this->doSomethingElse();
    break;
  }
 }

 private function doSomething() {
  //does stuff with first 4 digits of the user code.
 }

 private function doSomethingElse() {
  //does stuff with 12 digits of the user code.
 }
}

You can then use it like this:

$userCodeHandler = new UserCodeHandler($data, $client);
$userCodeHandler->manipulateData();

This will keep all of the conditional logic out of the controller and allow you to reuse the code in other controllers.

Pheter
Right, that's actually along the lines of my first thought. I just wanted to make an abstract handler with various implementations. I think this is the direction I will go unless anyone has any other recommendations.
stevebot
A: 

The factory pattern suits the mentioned scenario. Using the factory pattern you can implement classes for additional client types in future without modifying the existing code.

class Client
{
    public static function factory($client_type)
    {
        $classname = 'Client_' . $client_type;

        return new $classname;
    }
}

class Client_TypeA
{
    public function doSomething()
    {
        // do something specific to TypeA clients
    }
}

class Client_TypeB
{
    public function doSomething()
    {
        // do something specific to TypeB clients
    }
}

$client = Client::factory('TypeA');
$client->doSomething();
Ramesh Tabarna
This will not deal with the issue of having a chain of if ... else statements or a switch in the controller. In fact, the factory is not needed at all. You could implement classes for additional client types in the future without the factory, and it still won't require modification of the exisiting code.
Pheter