PHP7 NullObject

This commit is contained in:
Dominik Liebler
2016-09-22 10:43:27 +02:00
parent 01007ec5a8
commit b707bf064e
5 changed files with 10 additions and 44 deletions

View File

@@ -3,16 +3,9 @@
namespace DesignPatterns\Behavioral\NullObject; namespace DesignPatterns\Behavioral\NullObject;
/** /**
* LoggerInterface is a contract for logging something. * Key feature: NullLogger must inherit from this interface like any other loggers
*
* Key feature: NullLogger MUST inherit from this interface like any other Loggers
*/ */
interface LoggerInterface interface LoggerInterface
{ {
/** public function log(string $str);
* @param string $str
*
* @return mixed
*/
public function log($str);
} }

View File

@@ -2,19 +2,9 @@
namespace DesignPatterns\Behavioral\NullObject; namespace DesignPatterns\Behavioral\NullObject;
/**
* 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 class NullLogger implements LoggerInterface
{ {
/** public function log(string $str)
* {@inheritdoc}
*/
public function log($str)
{ {
// do nothing // do nothing
} }

View File

@@ -2,15 +2,9 @@
namespace DesignPatterns\Behavioral\NullObject; namespace DesignPatterns\Behavioral\NullObject;
/**
* PrintLogger is a logger that prints the log entry to standard output.
*/
class PrintLogger implements LoggerInterface class PrintLogger implements LoggerInterface
{ {
/** public function log(string $str)
* @param string $str
*/
public function log($str)
{ {
echo $str; echo $str;
} }

View File

@@ -2,24 +2,19 @@
namespace DesignPatterns\Behavioral\NullObject; namespace DesignPatterns\Behavioral\NullObject;
/**
* Service is dummy service that uses a logger.
*/
class Service class Service
{ {
/** /**
* @var LoggerInterface * @var LoggerInterface
*/ */
protected $logger; private $logger;
/** /**
* we inject the logger in ctor and it is mandatory. * @param LoggerInterface $logger
*
* @param LoggerInterface $log
*/ */
public function __construct(LoggerInterface $log) public function __construct(LoggerInterface $logger)
{ {
$this->logger = $log; $this->logger = $logger;
} }
/** /**
@@ -27,8 +22,7 @@ class Service
*/ */
public function doSomething() public function doSomething()
{ {
// no more check "if (!is_null($this->logger))..." with the NullObject pattern // notice here that you don't have to check if the logger is set with eg. is_null(), instead just use it
$this->logger->log('We are in '.__METHOD__); $this->logger->log('We are in '.__METHOD__);
// something to do...
} }
} }

View File

@@ -6,17 +6,12 @@ use DesignPatterns\Behavioral\NullObject\NullLogger;
use DesignPatterns\Behavioral\NullObject\PrintLogger; use DesignPatterns\Behavioral\NullObject\PrintLogger;
use DesignPatterns\Behavioral\NullObject\Service; use DesignPatterns\Behavioral\NullObject\Service;
/**
* LoggerTest tests for different loggers.
*/
class LoggerTest extends \PHPUnit_Framework_TestCase class LoggerTest extends \PHPUnit_Framework_TestCase
{ {
public function testNullObject() public function testNullObject()
{ {
// one can use a singleton for NullObjet : I don't think it's a good idea
// because the purpose behind null object is to "avoid special case".
$service = new Service(new NullLogger()); $service = new Service(new NullLogger());
$this->expectOutputString(null); // no output $this->expectOutputString(null);
$service->doSomething(); $service->doSomething();
} }