Skip to content

Commit

Permalink
updated strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Bradwick committed Jun 11, 2012
1 parent f2a6b4f commit 3d7e6a2
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 168 deletions.
5 changes: 3 additions & 2 deletions Patterns/Decorator/Decorators/ReverseDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Patterns\Decorator\Decorators;

use Patterns\Decorator\GreetingInterface;
use Patterns\Decorator\GreetingInterface,
Patterns\Decorator\Greeting;

/**
* ReverseDecorator
Expand All @@ -22,7 +23,7 @@ class ReverseDecorator implements GreetingInterface
*
* @param GreetingInterface $greeting the greeting
*
* @return UpperClassDecorator
* @return UpperCaseDecorator
*/
public function __construct(GreetingInterface $greeting)
{
Expand Down
3 changes: 2 additions & 1 deletion Patterns/Decorator/Decorators/UpperCaseDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Patterns\Decorator\Decorators;

use Patterns\Decorator\GreetingInterface;
use Patterns\Decorator\GreetingInterface,
Patterns\Decorator\Greeting;

/**
* UpperClassDecorator
Expand Down
5 changes: 4 additions & 1 deletion Patterns/Singleton/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ use Patterns\Singleton;
$obj1 = Singleton\Singleton::getInstance();
$obj2 = Singleton\Singleton::getInstance();
assert($obj1 === $obj2);
```
```

### PHP Design Patterns
[Back to all patterns](https://github.com/kevbradwick/php-design-patterns)
61 changes: 0 additions & 61 deletions Patterns/Strategy/AnimalContext.php

This file was deleted.

25 changes: 0 additions & 25 deletions Patterns/Strategy/Animals/AnimalStrategyInterface.php

This file was deleted.

33 changes: 0 additions & 33 deletions Patterns/Strategy/Animals/Cat.php

This file was deleted.

33 changes: 0 additions & 33 deletions Patterns/Strategy/Animals/Horse.php

This file was deleted.

76 changes: 76 additions & 0 deletions Patterns/Strategy/CD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Patterns\Strategy;

/**
* This CD class implements the strategy pattern. A strategy context must be
* set in order to get the string formatted CD.
*
* @author Kevin Bradwick <kbradwick@gmail.com>
*/
class CD
{
/**
* @var string
*/
protected $artist;

/**
* @var string
*/
protected $title;

/**
* @var Formats\FormatInterface
*/
protected $strategy;

/**
* Create a new CD object
*
* @param string $artist
* @param string $title
*/
public function __construct($artist, $title)
{
$this->artist = $artist;
$this->title = $title;
}

/**
* Set the strategy context
*
* @param Formats\FormatInterface $context
*/
public function setStrategyContext(Formats\FormatInterface $context)
{
$this->strategy = $context;
}

/**
* Get the cd using the context
*
* @return string
*/
public function get()
{
return $this->strategy->get($this);
}

/**
* @return string
*/
public function getArtist()
{
return $this->artist;
}

/**
* @return string
*/
public function getTitle()
{
return $this->title;
}

}
15 changes: 15 additions & 0 deletions Patterns/Strategy/Formats/FormatInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Patterns\Strategy\Formats;

use Patterns\Strategy\CD;

interface FormatInterface
{
/**
* @abstract
* @param CD $cd
* @return mixed
*/
public function get(CD $cd);
}
27 changes: 27 additions & 0 deletions Patterns/Strategy/Formats/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Patterns\Strategy\Formats;

use Patterns\Strategy\CD;

/**
* Json Formatter
*
* @author Kevin Bradwick <kbradwick@gmail.com>
*/
class Json implements FormatInterface
{
/**
* @param CD $cd
* @return mixed
*/
public function get(CD $cd)
{
$output['cd'] = array(
'artist' => $cd->getArtist(),
'title' => $cd->getTitle(),
);

return json_encode($output);
}
}
30 changes: 30 additions & 0 deletions Patterns/Strategy/Formats/Xml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Patterns\Strategy\Formats;

use Patterns\Strategy\CD;

/**
* XML Formatter
*
* @author Kevin Bradwick <kbradwick@gmail.com>
*/
class Xml implements FormatInterface
{
/**
* @param CD $cd
* @return mixed|void
*/
public function get(CD $cd)
{
$document = new \DOMDocument();
$root = $document->createElement('CD');
$root = $document->appendChild($root);
$title = $document->createElement('TITLE', $cd->getTitle());
$root->appendChild($title);
$band = $document->createElement('ARTIST', $cd->getArtist());
$root->appendChild($band);

return $document->saveXML();
}
}
18 changes: 18 additions & 0 deletions Patterns/Strategy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Strategy

A strategy is useful when traditional inheritance by extending the base class
is not an option as you want to add sometimes complex logic to the object. You
can usually tell when a strategy pattern would help if there are a lot of
if/else conditional statements in your code.


```
use Patterns\Strategy;
$cd = new Strategy\CD('Muse', 'Black Holes');
$cd->setStrategyContext(new Strategy\Formats\Json());
echo $cd->get();
```

### PHP Design Patterns
[Back to all patterns](https://github.com/kevbradwick/php-design-patterns)
11 changes: 0 additions & 11 deletions Patterns/Strategy/example.php

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ These are example design patterns that can be used to solve common problems.
* [Builder](https://github.com/kevbradwick/php-design-patterns/tree/master/Patterns/Builder)
* [Decorator](https://github.com/kevbradwick/php-design-patterns/tree/master/Patterns/Decorator)
* [Delegate](https://github.com/kevbradwick/php-design-patterns/tree/master/Patterns/Delegate)
* [Singleton](https://github.com/kevbradwick/php-design-patterns/tree/master/Patterns/Singleton)
* [Singleton](https://github.com/kevbradwick/php-design-patterns/tree/master/Patterns/Singleton)
* [Strategy](https://github.com/kevbradwick/php-design-patterns/tree/master/Patterns/Strategy)

0 comments on commit 3d7e6a2

Please sign in to comment.