Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 10.07.15 | ||
* Time: 20:04 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Structural\Decorator; | ||
|
||
|
||
class Character implements IDecoratedAttack | ||
{ | ||
const DEFAULT_HP = 100; | ||
const DEFAULT_ATTACK_HP = 5; | ||
|
||
protected $hp; | ||
|
||
/** | ||
* @param int $hp | ||
*/ | ||
public function __construct($hp = self::DEFAULT_HP) | ||
{ | ||
$this->setHp($hp); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getHp() | ||
{ | ||
return $this->hp; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setHp($amount) | ||
{ | ||
$this->hp = $amount; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function attack(Character $victim) | ||
{ | ||
$victim->weaken(self::DEFAULT_ATTACK_HP); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function weaken($amount) | ||
{ | ||
$this->hp -= $amount; | ||
|
||
return $this->hp <= 0 ? 'dead' : 'alive'; | ||
} | ||
|
||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 10.07.15 | ||
* Time: 20:02 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Structural\Decorator; | ||
|
||
|
||
interface IDecoratedAttack | ||
{ | ||
/** | ||
* @param Character $victim | ||
* @return $this | ||
*/ | ||
public function attack(Character $victim); | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getHp(); | ||
|
||
/** | ||
* @param $amount | ||
* @return $this | ||
*/ | ||
public function setHp($amount); | ||
|
||
/** | ||
* @param int $amount | ||
* @return string | ||
*/ | ||
public function weaken($amount); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 10.07.15 | ||
* Time: 20:15 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Structural\Decorator; | ||
|
||
|
||
class ShootingCharacter implements IDecoratedAttack | ||
{ | ||
const SHOOT_ATTACK_HP = 20; | ||
|
||
protected $character; | ||
|
||
public function __construct(IDecoratedAttack $character) | ||
{ | ||
$this->character = $character; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function attack(Character $victim) | ||
{ | ||
//shoots target | ||
$victim->weaken(self::SHOOT_ATTACK_HP); | ||
//performs default attack as well | ||
$this->character->attack($victim); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getHp() | ||
{ | ||
return $this->character->getHp(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setHp($amount) | ||
{ | ||
$this->character->setHp($amount); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function weaken($amount) | ||
{ | ||
return $this->character->weaken($amount); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#Decorator pattern | ||
|
||
Decorator is used to add/remove functionality to object without modifying it. Decorator is very useful for adhering | ||
Single Responsibility Principle in SOLID because it allows functionality to be divided amoung classes with uniques areas | ||
of concern. | ||
|
||
Decorator can be used to add behavior to class statically or dynamically. | ||
|
||
##When to use | ||
|
||
1. When you want to add/remove class behavior without modifying it. | ||
2. When you want to wrap some object with additional functionality. | ||
|
||
##How to use | ||
|
||
See [tests/DecoratorTest](/tests/DecoratorTest.php) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 10.07.15 | ||
* Time: 20:31 | ||
*/ | ||
use \Rpodwika\Designpatterns\Structural\Decorator\Character; | ||
use \Rpodwika\Designpatterns\Structural\Decorator\ShootingCharacter; | ||
|
||
class DecoratorTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @covers \Rpodwika\Designpatterns\Structural\Decorator\Character | ||
*/ | ||
public function testCharacterAttack() | ||
{ | ||
$murderer = new Character(); | ||
$victim = new Character(90); | ||
|
||
$this->assertEquals(100, $murderer->getHp()); | ||
$this->assertEquals(90, $victim->getHp()); | ||
$murderer->weaken(10); | ||
$this->assertEquals(90, $murderer->getHp()); | ||
} | ||
|
||
/** | ||
* @covers \Rpodwika\Designpatterns\Structural\Decorator\ShootingCharacter | ||
*/ | ||
public function testShootingCharacter() | ||
{ | ||
$victim = new Character(); | ||
$murderer = new Character(); | ||
|
||
$shootingMurderer = new ShootingCharacter($murderer); | ||
$this->assertEquals($murderer->getHp(), $shootingMurderer->getHp()); | ||
$shootingMurderer->attack($victim); | ||
$this->assertEquals(75, $victim->getHp()); | ||
$murderer->attack($victim); | ||
$this->assertEquals(70, $victim->getHp()); | ||
|
||
} | ||
} |
This file contains 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