-
Notifications
You must be signed in to change notification settings - Fork 23
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
Kevin Bradwick
committed
Jun 11, 2012
1 parent
f2a6b4f
commit 3d7e6a2
Showing
14 changed files
with
177 additions
and
168 deletions.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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; | ||
} | ||
|
||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace Patterns\Strategy\Formats; | ||
|
||
use Patterns\Strategy\CD; | ||
|
||
interface FormatInterface | ||
{ | ||
/** | ||
* @abstract | ||
* @param CD $cd | ||
* @return mixed | ||
*/ | ||
public function get(CD $cd); | ||
} |
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,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); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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) |
This file was deleted.
Oops, something went wrong.
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