티스토리 뷰

웹개발/Php

추상 팩토리 패턴

yaku 2010. 12. 20. 15:54


//interface
interface Ingredient
{
 function createSugar();
 function createCream();
 function createMilk();
}


// ingredient
class SeoulIngredient implements Ingredient
{

 function createSugar() {
  return new blackSugar();
 }

 function createCream() {
  return new Cream();
 }

 function createMilk() {
  return new Milk();
 }
}

class JejuIngredient implements Ingredient {

 function createSugar() {
  echo "Jeu Sugar <br/>";
 }

 function createCream() {
  echo "Jeu Cream <br/>";
 }

 function createMilk() {
  echo "Jeu Milk <br/>";
 }
}

// Sugar
abstract class Sugar {

}

class boldSugar extends Sugar  {

 function __construct() {
  echo " boldSugar .. ";
 }
}

class blackSugar extends Sugar {

 function __construct() {
  echo " blackSugar .. ";
 }
}

// Cream
class Cream {
 function Cream() {
  echo "Cream <br/>";
 }
}


// Milk
class Milk {
 function Cream() {
  echo "Milk <br/>";
 }
}


//Product

abstract class Coffee {

 public $_sugar;
 public $_cream;
 public $_milk;

 function buy() {
  echo " Thank You Very Much ..";
 }

 abstract function prepare();
}

class AmericalCoffee extends Coffee {
 public $_coffeeIngredient;

 function __construct($ingredient) {
  $this->_coffeeIngredient = $ingredient;
 }

 function prepare() {
  echo "PreParing AmericalCoffee ...<br/> ";
  $this->_sugar = $this->_coffeeIngredient->createSugar();
  $this->_cream = $this->_coffeeIngredient->createCream();
 }
}

class MilkCoffee extends Coffee {
 public $_coffeeIngredient;

 function __construct($ingredient) {
  $this->_coffeeIngredient = $ingredient;
 }

 function prepare() {
  echo "PreParing MilkCoffee ...<br/>";
  $this->_sugar = $this->_coffeeIngredient->createSugar();
  $this->_cream = $this->_coffeeIngredient->createCream();
  $this->_milk  = $this->_coffeeIngredient->createMilk();
 }
}

 

//Concrete

abstract class CoffeeStore  {

 function orderCoffee($name) {
  echo "Coffee Order <br/> ";
  $coffee = $this->createCoffee($name);
  $coffee->prepare();
  $coffee->buy();

 }

 abstract function createCoffee($name);
}


class StarStore extends CoffeeStore {

 function createCoffee($name) {
  $coffee = null;
  $classz = $name ."Ingredient";
  $coffeeIngredientFactory = new $classz();
  $coffee = new MilkCoffee($coffeeIngredientFactory);

  return $coffee;
 }
}


// Run..
$coffeeStore = new StarStore();
$coffeeStore->orderCoffee('Jeju');

'웹개발 > Php' 카테고리의 다른 글

csv 파일 저장, 읽기  (0) 2011.03.24
php 날짜 계산 strtotime 이용  (0) 2011.02.18
추상 팩토리 패턴  (0) 2010.12.20
Adapter 패턴  (0) 2010.12.20
PHP 참조 (PHP References) - 2 (고급편)  (0) 2010.12.20
PHP 최적화 40가지  (0) 2010.12.20
댓글
댓글쓰기 폼