mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-01 12:40:11 +02:00
README NullObject
This commit is contained in:
@@ -3,23 +3,6 @@
|
|||||||
namespace DesignPatterns\NullObject;
|
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"
|
* 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.
|
* I didn't run a benchmark but I think it's equivalent.
|
||||||
*
|
*
|
||||||
@@ -29,7 +12,7 @@ namespace DesignPatterns\NullObject;
|
|||||||
class NullLogger implements LoggerInterface
|
class NullLogger implements LoggerInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param string $str
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function log($str)
|
public function log($str)
|
||||||
{
|
{
|
||||||
|
20
NullObject/README.md
Normal file
20
NullObject/README.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# Null Object
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
NullOutput is a example of NullObject pattern. It is not formally a Design Pattern by the GoF but it's a schema which appears frequently enough to
|
||||||
|
be a pattern. Furthermore 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
|
||||||
|
|
||||||
|
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
|
||||||
|
a statement like `if (!is_null($obj)) { $obj->callSomething(); }` anymore.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
* Symfony2: null logger of profiler
|
||||||
|
* Symfony2: null output in Symfony/Console
|
||||||
|
* null handler in a Chain of Responsibilities pattern
|
||||||
|
* null command in a Command pattern
|
Reference in New Issue
Block a user