Skip to content

Commit

Permalink
新增 空物件模式 Null Object Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Kantai235 committed Sep 22, 2020
1 parent fd10fd0 commit aa54fcc
Show file tree
Hide file tree
Showing 10 changed files with 2,368 additions and 3 deletions.
27 changes: 27 additions & 0 deletions DesignPatterns/Behavioral/NullObjectPattern/DaisyMae.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace DesignPatterns\Behavioral\NullObjectPattern;

/**
* Class DaisyMae.
*/
class DaisyMae implements NPC
{
/**
* @param int $price
* @param int $count
*/
public function buyTurnips(int $price, int $count)
{
echo "[曹賣] 今天的價格是 1 棵 $price 鈴錢,要現在買嗎?";
}

/**
* @param int $price
* @param int $count
*/
public function sellTurnips(int $price, int $count)
{
echo "[曹賣] 我是曹賣,你不能把大頭菜賣給我。";
}
}
32 changes: 32 additions & 0 deletions DesignPatterns/Behavioral/NullObjectPattern/Mamekichi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace DesignPatterns\Behavioral\NullObjectPattern;

/**
* Class Mamekichi.
*/
class Mamekichi implements NPC
{
/**
* @param int $price
* @param int $count
*/
public function buyTurnips(int $price, int $count)
{
echo "[豆狸] 我是豆狸,我沒有在賣大頭菜狸。";
echo "[粒狸] 沒有在賣。";
}

/**
* @param int $price
* @param int $count
*/
public function sellTurnips(int $price, int $count)
{
$total = $price * $count;
echo "[豆狸] 現在的大頭菜價格是 1 棵 $price 鈴錢!";
echo "[粒狸] 鈴錢!";
echo "[豆狸] 好了!那麼,一共是 $total 鈴錢,確定要賣掉嗎?";
echo "[粒狸] 賣掉嗎?";
}
}
21 changes: 21 additions & 0 deletions DesignPatterns/Behavioral/NullObjectPattern/NPC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace DesignPatterns\Behavioral\NullObjectPattern;

/**
* Interface NPC.
*/
interface NPC
{
/**
* @param int $price
* @param int $count
*/
public function buyTurnips(int $price, int $count);

/**
* @param int $price
* @param int $count
*/
public function sellTurnips(int $price, int $count);
}
49 changes: 49 additions & 0 deletions DesignPatterns/Behavioral/NullObjectPattern/Player.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace DesignPatterns\Behavioral\NullObjectPattern;

/**
* Class Player
*/
class Player
{
/**
* @var NPC
*/
protected NPC $npc;

/**
*
* @param NPC $npc
*/
public function __construct(NPC $npc)
{
$this->setNPC($npc);
}

/**
* @param NPC $npc
*/
public function setNPC(NPC $npc)
{
$this->npc = $npc;
}

/**
* @param int $price
* @param int $count
*/
public function buy(int $price, int $count)
{
$this->npc->buyTurnips($price, $count);
}

/**
* @param int $price
* @param int $count
*/
public function sell(int $price, int $count)
{
$this->npc->sellTurnips($price, $count);
}
}
Loading

0 comments on commit aa54fcc

Please sign in to comment.