By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

which OOP Pattern to use

Dormilich
Expert Mod 5K+
P: 8,639
I’m not that good with OOP Patterns, so I better ask before I code in the wrong direction.

the scenario is quite simple, I have a DB table which holds different types of text (designated by a type field), based on that type I need a class (I usually fetch the DB result in a class) to use different output templates.

I’m pretty sure there is a Pattern for exactly that purpose, just which one? Strategy, State, Template or something other?

PS. a good reference is always appreciated.
Apr 29 '10 #1

✓ answered by Markus

Sounds like a simple factory would do. You delegate object creation to the factory, that is, you let the factory decide, on whatever criteria, which object to return.

Expand|Select|Wrap|Line Numbers
  1. class TextTypeFactory {
  2.     public static function create($text_type) {
  3.         switch ($text_type) {
  4.             case 'varchar': return new VarChar; break;
  5.             // etc ...
  6.         }
  7.     }
  8. }

Share this Question
Share on Google+
4 Replies


Markus
Expert 5K+
P: 6,050
Sounds like a simple factory would do. You delegate object creation to the factory, that is, you let the factory decide, on whatever criteria, which object to return.

Expand|Select|Wrap|Line Numbers
  1. class TextTypeFactory {
  2.     public static function create($text_type) {
  3.         switch ($text_type) {
  4.             case 'varchar': return new VarChar; break;
  5.             // etc ...
  6.         }
  7.     }
  8. }
Apr 29 '10 #2

Dormilich
Expert Mod 5K+
P: 8,639
does this look like a sensible implementation?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. interface iText
  3. {
  4.     public function getData();
  5.     public function getID();
  6. }
  7.  
  8. class NoText implements iText
  9. {
  10.     protected $id;
  11.  
  12.     public function __construct($id)
  13.     {
  14.         $this->id = $id;
  15.     }
  16.  
  17.     public function getID()
  18.     {
  19.         return $this->id;
  20.     }
  21.  
  22.     public function getData()
  23.     {
  24.         return "";
  25.     }
  26. }
  27. // if I remember right, constructors are inherited
  28. class BlockText extends NoText
  29. {
  30.     public $display = "Display5";
  31.  
  32.     public function getData()
  33.     {
  34.         $dbh = new DBH("-- some SQL");
  35.         return $dbh->execute($this->id)
  36.                    ->fetch(DBH::FETCH_CLASS, $this->display);
  37.     }
  38. }
  39.  
  40. class RoleText extends NoText
  41. {
  42.     public $display = "Display1";
  43.  
  44.     public function getData()
  45.     {
  46.         $dbh = new DBH("-- some other SQL");
  47.         return $dbh->execute($this->id)
  48.                    ->fetch(DBH::FETCH_CLASS, $this->display);
  49.     }
  50. }
  51.  
  52. abstract class aTextFactory
  53. {
  54.     abstract public static function createTextObj($id)
  55. }
  56.  
  57. class TextObj extends aTextFactory
  58. {
  59.     public static function createTextObj($id)
  60.     {
  61.         $type = (int) self::getTextType($id);
  62.         switch ($type) {
  63.             case 1: 
  64.                 $db_obj = new RoleText($id); break;
  65.             case 5: 
  66.                 $db_obj = new BlockText($id); break;
  67.             default:
  68.                 $db_obj = new NoText($id);
  69.         }
  70.         return $db_obj;
  71.     }
  72.  
  73.     private static function getTextType($id)
  74.     {
  75.         $dbh = new DBH("SELECT `type` FROM entry WHERE `ID` = ? ");
  76.         return $dbh->execute($id)
  77.                    ->fetch(DBH::FETCH_ONE);
  78.     }
  79. }
  80.  
  81. class TextChunk
  82. {
  83.     private $text_obj = NULL;
  84.  
  85.     public function __construct($id)
  86.     {
  87.         $this->text_obj = TextObj::createTextObj($id);
  88.     }
  89.  
  90.     public function __toString()
  91.     {
  92.         return $this->text_obj->getData();
  93.     }
  94.  
  95.     public function getID()
  96.     {
  97.         return $this->text_obj->getID();
  98.     }
  99. }
  100. ?>
Apr 30 '10 #3

Markus
Expert 5K+
P: 6,050
Looks fine to me :)
May 4 '10 #4

Dormilich
Expert Mod 5K+
P: 8,639
I’ll do thorough testing not before weekend, I guess. still I’m excited.
May 4 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.