Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
rpodwika committed Jul 10, 2015
2 parents 484294e + 58797a3 commit 9d02ca7
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 1 deletion.
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -19,3 +19,5 @@ pull request or message me
* [Strategy](src/Behavioral/Strategy)
3. Creational
* [Factory](src/Creational/Factory)
4. Structural
* [Decorator](src/Structural/Decorator)
2 changes: 1 addition & 1 deletion src/Behavioral/Memento/readme.md
Expand Up @@ -8,7 +8,7 @@ in PHP the easiest way is Serialisation with magic methods *__wakeup()* and *__s
You should use memento if you want revert state of object. It can be used in transactional operations. Memento protects
object encapsulation.

#How to use
##How to use

See [tests/SimpleBasketTest](/tests/SimpleBasketTest.php)

Expand Down
66 changes: 66 additions & 0 deletions src/Structural/Decorator/Character.php
@@ -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';
}


}
36 changes: 36 additions & 0 deletions src/Structural/Decorator/IDecoratedAttack.php
@@ -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);
}
60 changes: 60 additions & 0 deletions src/Structural/Decorator/ShootingCharacter.php
@@ -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);
}
}
16 changes: 16 additions & 0 deletions src/Structural/Decorator/readme.md
@@ -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)
44 changes: 44 additions & 0 deletions tests/DecoratorTest.php
@@ -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());

}
}
9 changes: 9 additions & 0 deletions tests/SingletonTest.php
Expand Up @@ -12,6 +12,9 @@

class SingletonTest extends PHPUnit_Framework_TestCase
{
/**
* @covers \Rpodwika\Designpatterns\Antipatterns\Singleton
*/
public function testSingletonReturnsObject()
{
$singletonObject = Singleton::getInstance();
Expand All @@ -20,11 +23,17 @@ public function testSingletonReturnsObject()
$this->assertInstanceOf('Rpodwika\Designpatterns\Antipatterns\Singleton', $singletonObject);
}

/**
* @covers \Rpodwika\Designpatterns\Antipatterns\Singleton
*/
public function testDoSomethingReturnsProperObject()
{
$this->assertEquals('I do something', Singleton::getInstance()->doSomething());
}

/**
* @covers \Rpodwika\Designpatterns\Antipatterns\SingletonTrait
*/
public function testSingletonTrait()
{
$object = SingletonSample::getInstance();
Expand Down

0 comments on commit 9d02ca7

Please sign in to comment.