473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

which OOP Pattern to use

Dormilich
8,658 Expert Mod 8TB
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. }

4 1316
Markus
6,050 Expert 4TB
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
8,658 Expert Mod 8TB
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
6,050 Expert 4TB
Looks fine to me :)
May 4 '10 #4
Dormilich
8,658 Expert Mod 8TB
I’ll do thorough testing not before weekend, I guess. still I’m excited.
May 4 '10 #5

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

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
2
by: CV | last post by:
How can I match 'n' number of neighbouring words of a pattern using regular expressions? For example, suppose I am looking for the pattern "length xyz cm" in some text. where xyz is a number -...
1
by: Sea Sharper | last post by:
Hi, C#, from a FileSystemWatcher I would like to catch all files with a *.* filter but then inside the event handler compare against a list of wildcards (eg '*.abc;*.def') Is there anywhere...
4
by: Guch Wu | last post by:
I want to design an image processing class as follow: class Image { Image Data; General Image Operations; read(filename, File_Type); write(filename, File_Type); };
1
by: ltruett | last post by:
Last week I continued my series of design patterns examples using PHP 5 with the Bridge Pattern, Flyweight Pattern, and Proxy Pattern. Here now is my 20th PHP 5 design pattern example, the...
11
by: td0g03 | last post by:
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like for(i=1;i<=size;i) ...
1
by: techstyled | last post by:
I need to create the following framework: SessionClient stores one or more SessionObjects StationClient stores one or more StationObjects which houses a list of SessionObjects (all the sessions on...
8
by: =?Utf-8?B?eWRibg==?= | last post by:
I need to write a program validate a text file in CSV format. So I will have a class DataType and a lot of of derived class for various type, e.g. IntType, StringType, FloatType, MoneyType,...
6
by: perhapscwk | last post by:
.... ... ... Both of the procedures have repeating sections of code: sending an e-mail and adding a log entry. Task: *1.Select a design pattern (substantiate your choice) that would allow...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.