Welcome Guest, Not a member yet? Register   Sign In
Big Help implementing this Factory pattern in CodeIgniter
#1

[eluser]Lazos[/eluser]
How you can write this code in CodeIgniter?

Code:
<?php

interface IDocument
{
   function outputHeader();
   function outputBody();
   function outputFooter();
}

class Formal implements IDocument
{
   public function outputHeader() {
      $header = "<p>".date("F d, Y")."</p>";
      $header .= "<p>Dear Sir or Madam:</p>";
      return $header;
   }

   public function outputBody($body) {
      $body = str_replace(":-)","",$body);
      $body = "<p>".$body."</p>";
      return $body;
   }

   public function outputFooter($name) {
      return "Sincerely,<br />$name";
   }

}

class Casual implements IDocument
{
   public function outputHeader() {
      return "What's up!<br />";
   }

   public function outputBody($body) {
      $body = "<p>".$body."</p>";
      return $body;
   }

   public function outputFooter($name) {
      return "Cya,<br />$name";
   }

}

class DocumentMaker {

   function create($type) {
      switch($type) {
         case 'formal' :
            $doctype = new Formal();
            break;
         case 'casual' :
            $doctype = new Casual();
            break;
         default:
            $doctype = new Casual();
      }
      return $doctype;
   }
}

$docmaker = new DocumentMaker();
$docobj = $docmaker->create("formal");

echo $docobj->outputHeader();
echo $docobj->outputBody("Thank you for lunch today. I appreciate it. :-)");
echo $docobj->outputFooter("Jason");

?&gt;
#2

[eluser]crumpet[/eluser]
Code:
$style = 'formal'; // OR 'casual'
$data['body'] = "thanks for lunch";
$data['name'] = "Jason";
$this->load->view($style . '/header');
$this->load->view($style . '/body', $data);
$this->load->view($style . '/footer', $data);

//dir structure
//  views ->
//     ->formal
//         -> header.php, body.php, footer.php
//     ->casual
//         ->header.php, body.php, footer.php
Personally i'd just have a letter.php for each style which contains header, body and footer




Theme © iAndrew 2016 - Forum software by © MyBB