which OOP Pattern to use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    which OOP Pattern to use

    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.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    Code:
    class TextTypeFactory {
        public static function create($text_type) {
            switch ($text_type) {
                case 'varchar': return new VarChar; break;
                // etc ...
            }
        }
    }

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      does this look like a sensible implementation?
      Code:
      <?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();
      	}
      }
      ?>

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Looks fine to me :)

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          I’ll do thorough testing not before weekend, I guess. still I’m excited.

          Comment

          Working...