Skip to content

Commit

Permalink
Add php impls of null object pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
oxnz committed Oct 4, 2013
1 parent d9d20ed commit 87ab7ba
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract-factory | [DONE](./src/abstract-factory/abstract-factory.md) | [DONE](.
factory-method | [DONE](./src/factory-method/factory-method.md) | [DONE](./src/factory-method/cpp) | [DONE](./src/factory-method/java) | [DONE](./src/factory-method/python) | [DONE](./src/factory-method/php)
chain-of-responsibility | [DONE](./src/chain-of-responsibility/chain-of-responsibility.md) | [DONE](./src/chain-of-responsibility/cpp) | [DONE](./src/chain-of-responsibility/java) | [DONE](./src/chain-of-responsibility/python)
iterator | [DONE](./src/iterator/iterator.md) | [DONE](./src/iterator/cpp) | [TODO] | [DONE](./src/iterator/python) | [DONE](./src/iterator/perl)
null-object | [DONE](./src/null-object/null-object.md) | [DONE](./src/null-object/cpp) | [TODO] | [DONE](./src/null-object/python)
null-object | [DONE](./src/null-object/null-object.md) | [DONE](./src/null-object/cpp) | [TODO] | [DONE](./src/null-object/python) | [DONE](./src/null-object/php)
object-pool | [DONE](./src/object-pool/object-pool.md) | [TODO] | [TODO] | [DONE](./src/object-pool/python)
private-class-data | [TODO] | [TODO] | [TODO] | [TODO]

Expand Down
18 changes: 18 additions & 0 deletions src/null-object/php/Logger.php
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);
}
38 changes: 38 additions & 0 deletions src/null-object/php/NullLogger.php
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
}
}
15 changes: 15 additions & 0 deletions src/null-object/php/PrintLogger.php
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;
}
}
34 changes: 34 additions & 0 deletions src/null-object/php/Service.php
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
}
}

?>

0 comments on commit 87ab7ba

Please sign in to comment.