stijn0713 Posted January 13, 2013 Share Posted January 13, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/273097-oo-equivalent-of-wrapper-function-in-this-context/ Share on other sites More sharing options...
Christian F. Posted January 14, 2013 Share Posted January 14, 2013 This is one of the things factory patterns are useful for, as they allow you to automate the object generation based upon just about any criteria you might have. Quote Link to comment https://forums.phpfreaks.com/topic/273097-oo-equivalent-of-wrapper-function-in-this-context/#findComment-1405459 Share on other sites More sharing options...
KevinM1 Posted January 14, 2013 Share Posted January 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273097-oo-equivalent-of-wrapper-function-in-this-context/#findComment-1405461 Share on other sites More sharing options...
stijn0713 Posted January 14, 2013 Author Share Posted January 14, 2013 found it! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/273097-oo-equivalent-of-wrapper-function-in-this-context/#findComment-1405541 Share on other sites More sharing options...
stijn0713 Posted January 14, 2013 Author Share Posted January 14, 2013 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'. Quote Link to comment https://forums.phpfreaks.com/topic/273097-oo-equivalent-of-wrapper-function-in-this-context/#findComment-1405656 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.