forked from oxnz/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add php impls of null object pattern
- Loading branch information
oxnz
committed
Oct 4, 2013
1 parent
d9d20ed
commit 87ab7ba
Showing
5 changed files
with
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace DesignPatterns\NullObject; | ||
|
||
/** | ||
* @name Logger interface is a contract for logging something | ||
* | ||
* Key feature: NullLogger MUST inherit from this interface like any other | ||
* Loggers. | ||
*/ | ||
public interface Logger { | ||
/** | ||
* @param string $str | ||
* | ||
* @return mixed | ||
*/ | ||
public function log($str); | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace DesignPatterns\NullObject; | ||
|
||
/** | ||
* NullOutput is a example of NullObject pattern. It is not formely a Design | ||
* Pattern by the GoF but it's a schema which appears frequently enough to | ||
* be a pattern. Futhermore it is a really good pattern in my opinion : | ||
* - the code in the client is simple | ||
* - it reduces the chance of null pointer exception | ||
* - less "if" => less test cases | ||
* | ||
* The purpose : every time you have a method which returns an object or null, | ||
* you should return an object or a "NullObject". With NullObject, you don't | ||
* need statement like "if (!is_null($obj)) { $obj->callSomething(); }" anymore. | ||
* | ||
* In this case, this a logger which does nothing. Other examples : | ||
* - null logger of symfony profiler | ||
* - null output in symfony/console | ||
* - null handler in a Chain of Responsiblities pattern | ||
* - null command in a Command pattern | ||
* | ||
* Performance concerns : ok there is a call for nothing but we spare an "if | ||
* is_null" | ||
* I didn't run a benchmark but I think it's equivalent. | ||
* | ||
* Key feature : of course this logger MUST implement the same interface (or | ||
* abstract) like the other loggers. | ||
*/ | ||
|
||
class NullLogger implements LoggerInterface { | ||
/** | ||
* @param string $str | ||
*/ | ||
public function log($str) { | ||
// do nothing | ||
} | ||
} |
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 DesignPatterns\NullObject; | ||
|
||
/** | ||
* PrintLogger is a logger that prints the log entry to standard output | ||
*/ | ||
class PrintLogger implements Logger { | ||
/** | ||
* @param string $str | ||
*/ | ||
public function log($str) { | ||
echo $str; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace DesignPatterns\NullObject; | ||
|
||
/** | ||
* Service is a dummy service that uses a logger | ||
*/ | ||
class Service { | ||
/** | ||
* @var Logger Interface | ||
*/ | ||
protected $logger; | ||
/** | ||
* we inject the logger in ctor and it is mandatory | ||
* | ||
* @param Logger $log | ||
*/ | ||
public function __construct(Logger $logger) { | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* do something | ||
*/ | ||
public function doSomething() { | ||
/** | ||
* no more check "if (!is_null($this->logger))..." with the NullObject | ||
*/ | ||
$this->logger->log('We are in ' . __METHOD__); | ||
// do actual action | ||
} | ||
} | ||
|
||
?> |