Head First Design Model 코드의 PHP 버전(객체 학습) 1/2페이지
<?php
/**
*
* , , ,
* 。
*/
//
interface FlyBehavior {
public function fly();
}
//
interface QuackBehavior {
public function quack();
}
//
class FlyWithWings implements FlyBehavior {
public function fly() {
echo "I'm flying!!
";
}
}
//
class FlyNoWay implements FlyBehavior {
public function fly() {
echo "I can't fly!
";
}
}
class FlyRocketPowered implements FlyBehavior {
public function fly() {
echo "I'm flying with a rocket!
";
}
}
class Qquack implements QuackBehavior {
public function quack() {
echo "Quack
";
}
}
class Squeak implements QuackBehavior {
public function quack() {
echo "Squeak
";
}
}
class MuteQuack implements QuackBehavior {
public function quack() {
echo "<< Silence >>
";
}
}
abstract class Duck {
protected $quack_obj;
protected $fly_obj;
public abstract function display();
public function performQuack() {
$this->quack_obj->quack();
}
public function performFly() {
$this->fly_obj->fly();
}
public function swim() {
echo "All ducks float, even decoys!
";
}
public function setFlyBehavior(FlyBehavior $fb) {
$this->fly_obj = $fb;
}
public function setQuackBehavior(QuackBehavior $qb) {
$this->quack_obj = $qb;
}
}
class ModelDuck extends Duck {
public function __construct() {
$this->fly_obj = new FlyNoWay();
$this->quack_obj = new MuteQuack();
}
public function display() {
echo "I'm a model duck!
";
}
}
$model = new ModelDuck();
$model->performFly();
$model->performQuack();
//
$model->setFlyBehavior(new FlyRocketPowered());
$model->setQuackBehavior(new Squeak());
$model->performFly();
$model->performQuack();
?>
단일 모드
<?php
/**
*
* , 。
*/
class MyClass {
private static $uniqueInstance;
private function __construct() {
}
public static function getInstance() {
if (self::$uniqueInstance == null) {
self::$uniqueInstance = new MyClass();
}
return self::$uniqueInstance;
}
}
$myClass = MyClass::getInstance();
var_dump($myClass);
$myClass = MyClass::getInstance();
var_dump($myClass);
?>
공장 방법 모델
<?php
abstract class PizzaStore {
public function orderPizza($type) {
$pizza = $this->createPizza($type);
$pizza->prepare();
$pizza->bake();
$pizza->cut();
$pizza->box();
return $pizza;
}
public abstract function createPizza($type);
}
class NYPizzaStore extends PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new NYStyleCheesePizza();
} elseif ($type == "veggie") {
return new NYStyleVeggiePizza();
} elseif ($type == "clam") {
return new NYStyleClamPizza();
} elseif ($type == "papperoni") {
return new NYStylePapperoniPizza();
} else {
return null;
}
}
}
class ChicagoPizzaStore extends PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new ChicagoStyleCheesePizza();
} elseif ($type == "veggie") {
return new ChicagoStyleVeggiePizza();
} elseif ($type == "clam") {
return new ChicagoStyleClamPizza();
} elseif ($type == "papperoni") {
return new ChicagoStylePapperoniPizza();
} else {
return null;
}
}
}
abstract class Pizza {
public $name;
public $dough;
public $sauce;
public $toppings = array();
public function prepare() {
echo "Preparing " . $this->name . "
";
echo "Yossing dough...
";
echo "Adding sauce...
";
echo "Adding toppings:
";
for ($i = 0; $i < count($this->toppings); $i++) {
echo " " . $this->toppings[$i] . "
";
}
}
public function bake() {
echo "Bake for 25 minutes at 350
";
}
public function cut() {
echo "Cutting the pizza into diagonal slices
";
}
public function box() {
echo "Place pizza in official PizzaStore box
";
}
public function getName() {
return $this->name;
}
}
class NYStyleCheesePizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and cheese Pizza";
$this->dough = "Thin Crust Dough";
$this->sauce = "Marinara Sauce";
$this->toppings[] = "Grated Reggiano Cheese";
}
}
class NYStyleVeggiePizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and veggie Pizza";
$this->dough = "Thin Crust Dough";
$this->sauce = "Marinara Sauce";
$this->toppings[] = "Grated Reggiano veggie";
}
}
class NYStyleClamPizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and clam Pizza";
$this->dough = "Thin Crust Dough";
$this->sauce = "Marinara Sauce";
$this->toppings[] = "Grated Reggiano clam";
}
}
class NYStylePapperoniPizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and papperoni Pizza";
$this->dough = "Thin Crust Dough";
$this->sauce = "Marinara Sauce";
$this->toppings[] = "Grated Reggiano papperoni";
}
}
class ChicagoStyleCheesePizza extends Pizza {
public function __construct() {
$this->name = "Chicago Style Deep Dish Cheese Pizza";
$this->dough = "Extra Thick Crust Dough";
$this->sauce = "Plum Tomato Sauce";
$this->toppings[] = "Shredded Mozzarella Cheese";
}
public function cut() {
echo "Cutting the pizza into square slices
";
}
}
$myStore = new NYPizzaStore();
$chicagoStore = new ChicagoPizzaStore();
$pizza = $myStore->orderPizza("cheese");
echo "Ethan ordered a " . $pizza->getName() . "
";
$pizza = $chicagoStore->orderPizza("cheese");
echo "Ethan ordered a " . $pizza->getName() . "
";
?>
공장 모델
<?php
abstract class PizzaStore {
public function orderPizza($type) {
$pizza = $this->createPizza($type);
$pizza->prepare();
$pizza->bake();
$pizza->cut();
$pizza->box();
return $pizza;
}
public abstract function createPizza($type);
}
class NYPizzaStore extends PizzaStore {
public function createPizza($type) {
$pizza = null;
$ingredientFactory = new NYPizzaIngredientFactory();
if ($type == "cheese") {
$pizza = new CheesePizza($ingredientFactory);
$pizza->setName('New York Style Cheese Pizza');
} elseif ($type == "veggie") {
$pizza = new VeggiePizza($ingredientFactory);
$pizza->setName('New York Style Veggie Pizza');
} elseif ($type == "clam") {
$pizza = new ClamPizza($ingredientFactory);
$pizza->setName('New York Style Clam Pizza');
} elseif ($type == "papperoni") {
$pizza = new PapperoniPizza($ingredientFactory);
$pizza->setName('New York Style Papperoni Pizza');
}
return $pizza;
}
}
class ChicagoPizzaStore extends PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new ChicagoStyleCheesePizza();
} elseif ($type == "veggie") {
return new ChicagoStyleVeggiePizza();
} elseif ($type == "clam") {
return new ChicagoStyleClamPizza();
} elseif ($type == "papperoni") {
return new ChicagoStylePapperoniPizza();
} else {
return null;
}
}
}
interface PizzaIngredientFactory {
public function createDough();
public function createSauce();
public function createCheese();
public function createVeggies();
public function createPepperoni();
public function createClam();
}
class NYPizzaIngredientFactory implements PizzaIngredientFactory {
public function createDough() {
return new ThinCrustDough();
}
public function createSauce() {
return new MarinaraSauce();
}
public function createCheese() {
return new ReggianoCheese();
}
public function createVeggies() {
$veggies = array(
new Garlic(),
new Onion(),
new Mushroom(),
new RedPepper(),
);
return $veggies;
}
public function createPepperoni() {
return new SlicedPepperoni();
}
public function createClam() {
return new FreshClams();
}
}
class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
public function createDough() {
return new ThickCrustDough();
}
public function createSauce() {
return new PlumTomatoSauce();
}
public function createCheese() {
return new Mozzarella();
}
public function createVeggies() {
$veggies = array(
new BlackOlives(),
new Spinach(),
new EggPlant(),
);
return $veggies;
}
public function createPepperoni() {
return new SlicedPepperoni();
}
public function createClam() {
return new FrozenClams();
}
}
abstract class Pizza {
public $name;
public $dough;
public $sauce;
public $veggies = array();
public $cheese;
public $pepperoni;
public $clam;
public abstract function prepare();
public function bake() {
echo "Bake for 25 minutes at 350
";
}
public function cut() {
echo "Cutting the pizza into diagonal slices
";
}
public function box() {
echo "Place pizza in official PizzaStore box
";
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function __toString() {
}
}
class CheesePizza extends Pizza {
public $ingredientFactory;
public function __construct(PizzaIngredientFactory $ingredientFactory) {
$this->ingredientFactory = $ingredientFactory;
}
public function prepare() {
echo "Preparing " . $this->name . "
";
$this->dough = $this->ingredientFactory->createDough();
$this->sauce = $this->ingredientFactory->createSauce();
$this->cheese = $this->ingredientFactory->createCheese();
}
}
class ClamPizza extends Pizza {
public $ingredientFactory;
public function __construct(PizzaIngredientFactory $ingredientFactory) {
$this->ingredientFactory = $ingredientFactory;
}
public function prepare() {
echo "Preparing " . $this->name . "
";
$this->dough = $this->ingredientFactory->createDough();
$this->sauce = $this->ingredientFactory->createSauce();
$this->cheese = $this->ingredientFactory->createCheese();
$clam = $this->ingredientFactory->createClam();
}
}
$nyPizzaStore = new NYPizzaStore();
$nyPizzaStore->orderPizza('cheese');
?>
일이 다음 페이지 전문을 읽다
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
laravel에 yo에서 angularJs&coffeescript를 사용할 수 있도록 한다.먼저 yo 명령을 사용할 수 있어야하므로 아래에서 설치 global에 설치한 곳에서 laravel의 프로젝트 루트로 이동. 클라이언트 코드를 관리하는 디렉토리를 만들고 이동합니다. 클라이언트 환경 만들기 이것으로 히...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.