Skip to content

Instantly share code, notes, and snippets.

@rightgo09
Created January 20, 2015 15:42
Show Gist options
  • Select an option

  • Save rightgo09/467492d32338cd7c1eb3 to your computer and use it in GitHub Desktop.

Select an option

Save rightgo09/467492d32338cd7c1eb3 to your computer and use it in GitHub Desktop.
<?php
abstract class ItemShop {
public function order_item($type) {
//$item = $this->factory->create_item($type);
$item = $this->create_item($type);
if ($item->is_expired()) return;
if ($item->is_sold_out()) return;
if ($item->on_sale()) {
$item->sale_price();
}
return $item;
}
abstract public function create_item($type);
}
class TokyoItemShop extends ItemShop {
public function create_item($type) {
$item = null;
if ($type === "katana") {
$item = new KatanaItem();
}
elseif ($type === "shuriken") {
$item = new ShurikenItem();
}
return $item;
}
}
class MythlogyItemShop extends ItemShop {
public function create_item($type) {
$item = null;
if ($type === "gungnir") {
$item = new GungnirItem();
}
elseif ($type === "laevatein") {
$item = new LaevateinItem();
}
return $item;
}
}
abstract class Item {
public $price;
public function is_expired() {
return time() > 1521767460;
}
public function is_sold_out() {
return false; // infinite!
}
public function on_sale() {
return false; // normal...
}
public function sale_price() {
$this->price *= 0.5; // half price!
}
}
class ShurikenItem extends Item {
public function __construct() {
$this->price = 100;
}
}
class GungnirItem extends Item {
public function __construct() {
$this->price = 100000;
}
}
$tokyo_item_shop = new TokyoItemShop();
$item = $tokyo_item_shop->order_item("shuriken");
echo "shuriken price: ".$item->price.PHP_EOL;
$mythology_item_shop = new MythlogyItemShop();
$item = $mythology_item_shop->order_item("gungnir");
echo "gungnir price: ".$item->price.PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment