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.
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. -
class TextTypeFactory {
-
public static function create($text_type) {
-
switch ($text_type) {
-
case 'varchar': return new VarChar; break;
-
// etc ...
-
}
-
}
-
}
4 1316
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. -
class TextTypeFactory {
-
public static function create($text_type) {
-
switch ($text_type) {
-
case 'varchar': return new VarChar; break;
-
// etc ...
-
}
-
}
-
}
does this look like a sensible implementation? - <?php
-
interface iText
-
{
-
public function getData();
-
public function getID();
-
}
-
-
class NoText implements iText
-
{
-
protected $id;
-
-
public function __construct($id)
-
{
-
$this->id = $id;
-
}
-
-
public function getID()
-
{
-
return $this->id;
-
}
-
-
public function getData()
-
{
-
return "";
-
}
-
}
-
// if I remember right, constructors are inherited
-
class BlockText extends NoText
-
{
-
public $display = "Display5";
-
-
public function getData()
-
{
-
$dbh = new DBH("-- some SQL");
-
return $dbh->execute($this->id)
-
->fetch(DBH::FETCH_CLASS, $this->display);
-
}
-
}
-
-
class RoleText extends NoText
-
{
-
public $display = "Display1";
-
-
public function getData()
-
{
-
$dbh = new DBH("-- some other SQL");
-
return $dbh->execute($this->id)
-
->fetch(DBH::FETCH_CLASS, $this->display);
-
}
-
}
-
-
abstract class aTextFactory
-
{
-
abstract public static function createTextObj($id)
-
}
-
-
class TextObj extends aTextFactory
-
{
-
public static function createTextObj($id)
-
{
-
$type = (int) self::getTextType($id);
-
switch ($type) {
-
case 1:
-
$db_obj = new RoleText($id); break;
-
case 5:
-
$db_obj = new BlockText($id); break;
-
default:
-
$db_obj = new NoText($id);
-
}
-
return $db_obj;
-
}
-
-
private static function getTextType($id)
-
{
-
$dbh = new DBH("SELECT `type` FROM entry WHERE `ID` = ? ");
-
return $dbh->execute($id)
-
->fetch(DBH::FETCH_ONE);
-
}
-
}
-
-
class TextChunk
-
{
-
private $text_obj = NULL;
-
-
public function __construct($id)
-
{
-
$this->text_obj = TextObj::createTextObj($id);
-
}
-
-
public function __toString()
-
{
-
return $this->text_obj->getData();
-
}
-
-
public function getID()
-
{
-
return $this->text_obj->getID();
-
}
-
}
-
?>
I’ll do thorough testing not before weekend, I guess. still I’m excited.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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 -...
|
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...
|
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);
};
|
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...
|
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)
...
|
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...
|
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,...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
|
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...
| |