Created
January 18, 2015 14:32
-
-
Save rightgo09/972f76b5654ebebeea01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once "./simpleitemfactory.php"; | |
| class ItemShop { | |
| private $factory; | |
| public function __construct(SimpleItemFactory $factory) { | |
| $this->factory = $factory; | |
| } | |
| public function order_item($type) { | |
| $item = $this->factory->create_item($type); | |
| return $item; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once "./simpleitemfactory.php"; | |
| require_once "./itemshop.php"; | |
| $item = (new ItemShop(new SimpleItemFactory()))->order_item("sword"); | |
| var_dump($item); | |
| //=>object(SwordItem)#3 (0) { | |
| //=>} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class ShoesItem { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once "./sworditem.php"; | |
| require_once "./shoesitem.php"; | |
| class SimpleItemFactory { | |
| public function create_item($type) { | |
| $item = null; | |
| if ($type === "sword") { | |
| $item = new SwordItem(); | |
| } | |
| elseif ($type === "shoes") { | |
| $item = new ShoesItem(); | |
| } | |
| return $item; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class SwordItem { | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment