Jump to content

OO-equivalent of wrapper function in this context


stijn0713

Recommended Posts

Hello,

 

Consider a function that handles a single task, i.e. it generates a random password string. This function can evolve over time, i.e. multiple versions of this task can exist. As soon as more than 1 version exists, i want to create a wrapper function that wraps the different versions for the sake of evolvability. Doing this, the previous calling functions would not have to be changed, like so:

 

function makePW($version = 'latest'){

 switch($version) {
  case 'v1':
   return makePW1();
  break;

  case 'v2':
   return makePW2();
  break;

  case 'v3':
   return makePW3();
  break;

  default:
   if(isset($version)&& $version != 'latest')
   echo 'No such version: "<i>'.$version.'</i>" found, default version V2 used';
   return makePW2();
  break;
 } 
}

 

What would be an OO equivalent implementation, using polymorphism?

Link to comment
Share on other sites

It really depends on whether you want different kinds of objects, or a single kind of object that can exhibit different kinds of behavior.

 

In the first case, Christian's suggestion of the Factory Method design pattern is what you should look at. In the second case, look at the Strategy Pattern.

Link to comment
Share on other sites

I don't know if my use of the factory pattern embodies the goal of the existence of the factory class. Thought, in the light of evolvability (as equivalent of a wrapper function in procedural programming) is suits my goal :

 

class Article { 

   public $title; 
   public $author; 
   public $datum; 
   public $category; 

   public function  __construct($title, $author, $date, $category = 0) { 

    $this->title = $title; 
    $this->author = $author; 
    $this->datum = $date; 
    $this->category = $category; 
   } 

   public function write(uitprinten $writer) { 
  return $writer->printuit($this); 
   }  
}
interface uitprinten { 
   public function printuit(Article $obj); 
} 
class uitprinten_XML implements uitprinten { 

    public function printuit(Article $obj) { 

	    $content = '<article>'; 
	    $content .= '<title>' . $obj->title . '</title>'; 
	    $content .= '<author>' . $obj->author . '</author>'; 
	    $content .= '<date>' . $obj->datum . '</date>'; 
	    $content .= '<category>' . $obj->category . '</category>'; 
	    $content .= '</article>'; 

    return $content; 
   } 
} 

class uitprinten_JSON implements uitprinten { 

    public function printuit(Article $obj) { 

	    $array = array('article' => $obj); 

    return json_encode($array);  
   } 
}

class uitprinten_Factory
{
 public static function GetWriter($type='JSON')
  {
    $class = 'uitprinten_' . $type; 
  if(class_exists($class)) { 

   return new $class(); 
    } 

    throw new Exception('Unsupported format'); 
    }
}



$article = new Article('Een titel', 'Stijn Wuyts', time(), 0); 
$aWriter = uitprinten_Factory::GetWriter();
var_dump($article->write($aWriter)); 

 

 

The use of the factory class will allow a change in version (e.g. from format XML to JSON), not having to rewrite all the calling functions

uitprinten_Factory::GetWriter();

.

 

My question now, is this the goal of the factory pattern or is it just 'a use of it'.

Link to comment
Share on other sites

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.