Skip to content

Commit

Permalink
Merge branch 'release/1.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rpodwika committed Jul 15, 2015
2 parents e1ee5c0 + e9a95ad commit c8e08be
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 13 deletions.
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -34,6 +34,7 @@ should not force your code to fit in patterns. It should come out naturally. Fir
state
* [Observer](src/Behavioral/Observer) - Define one to many dependency between objects so that when one changes state,
all its dependents are notified and updated automatically
* [State][src/Behavioral/State] - allow object to alter its behavior depending on current internal state change.
* [Strategy](src/Behavioral/Strategy) - Define a family of algorithms and make them interchangable.
3. Creational
* [Factory](src/Creational/Factory) - Define an interface for creating an object but let the subclasses decide
Expand All @@ -57,6 +58,5 @@ should not force your code to fit in patterns. It should come out naturally. Fir
* Interpreter
* Iterator
* Mediator
* State
* Visitor

6 changes: 2 additions & 4 deletions src/Antipatterns/Singleton/Singleton.php
Expand Up @@ -13,7 +13,7 @@ class Singleton
private static $instance;

/**
* @return bool
* @return $this
*/
final public static function getInstance()
{
Expand All @@ -28,12 +28,10 @@ final private function __construct()
//default construct should not have public access
}

final private function __wakeup()
{
}

final private function __clone()
{
return static::$instance;
}

public function doSomething()
Expand Down
8 changes: 0 additions & 8 deletions src/Antipatterns/Singleton/SingletonTrait.php
Expand Up @@ -22,12 +22,4 @@ final private function __construct()
{
}

final private function __wakeup()
{
}

final private function __clone()
{
}

}
29 changes: 29 additions & 0 deletions src/Behavioral/State/ConnectionClosed.php
@@ -0,0 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 15.07.15
* Time: 00:31
*/

namespace Rpodwika\Designpatterns\Behavioral\State;


class ConnectionClosed implements ConnectionState
{
public function open()
{
return 'Opening connection';
}

public function close()
{
return 'Connection already closed';
}

public function reconnect()
{
return 'Reconnecting';
}

}
29 changes: 29 additions & 0 deletions src/Behavioral/State/ConnectionEstablished.php
@@ -0,0 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 15.07.15
* Time: 00:26
*/

namespace Rpodwika\Designpatterns\Behavioral\State;


class ConnectionEstablished implements ConnectionState
{
public function open()
{
return 'Cannot open already opened connection';
}

public function close()
{
return 'Freeing resources';
}

public function reconnect()
{
return "Waiting for reconnection";
}

}
17 changes: 17 additions & 0 deletions src/Behavioral/State/ConnectionState.php
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 15.07.15
* Time: 00:25
*/

namespace Rpodwika\Designpatterns\Behavioral\State;


interface ConnectionState
{
public function open();
public function close();
public function reconnect();
}
119 changes: 119 additions & 0 deletions src/Behavioral/State/DbConnection.php
@@ -0,0 +1,119 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 15.07.15
* Time: 00:33
*/

namespace Rpodwika\Designpatterns\Behavioral\State;


class DbConnection
{
/**
* @var ConnectionState $state
*/
private $state;
private $port;
private $uri;

public function __construct($port, $uri)
{
$this->port = $port;
$this->uri = $uri;
}


/**
* @return mixed
*/
public function getState()
{
return $this->state;
}

/**
* @param mixed $state
* @return $this
*/
public function setState(ConnectionState $state)
{
$this->state = $state;

return $this;
}

/**
* @return mixed
*/
public function getUri()
{
return $this->uri;
}

/**
* @param mixed $uri
* @return $this
*/
public function setUri($uri)
{
$this->uri = $uri;

return $this;
}

/**
* @return mixed
*/
public function getPort()
{
return $this->port;
}

/**
* @param mixed $port
* @return $this
*/
public function setPort($port)
{
$this->port = $port;

return $this;
}

/**
* @return string
*/
public function connect()
{
$this->state = new ConnectionEstablished();
$this->state->open();

return sprintf('Opening connection %s:%d', $this->getUri(), $this->getPort());
}

/**
* @return string
*/
public function close()
{
$this->state = new ConnectionClosed();
$this->state->close();

return 'Closing connection';
}

/**
* @return string
*/
public function reconnect()
{
$this->close();
$this->connect();

return 'Reconnecting';
}


}
8 changes: 8 additions & 0 deletions src/Behavioral/State/readme.md
@@ -0,0 +1,8 @@
#State

State allows oject to later its behavior when its internal state changes.
The key idea in this pattern is to create State object that represents current state of an object.

#When to use

* When object behavior depends on state and it must change behavior at runtime depending on that state
9 changes: 9 additions & 0 deletions tests/Antipatterns/Singleton/SingletonTest.php
Expand Up @@ -44,4 +44,13 @@ public function testSingletonTrait()
$this->assertEquals('I do something', $object->doSomething());

}

/**
* @covers \Rpodwika\Designpatterns\Antipatterns\Singleton\SingletonSample
*/
public function testSingletonSample()
{
$this->assertEquals('I do something', SingletonSample::getInstance()->doSomething());
}

}
41 changes: 41 additions & 0 deletions tests/Behavioral/State/ConnectionClosedTest.php
@@ -0,0 +1,41 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 15.07.15
* Time: 00:41
*/

namespace Rpodwika\Designpatterns\Tests\Behavioral\State;

use Rpodwika\Designpatterns\Behavioral\State\ConnectionClosed;

class ConnectionClosedTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionClosed::open
*/
public function testOpen()
{
$connection = new ConnectionClosed();
$this->assertEquals('Opening connection', $connection->open());
}

/**
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionClosed::close
*/
public function testClose()
{
$connection = new ConnectionClosed();
$this->assertEquals('Connection already closed', $connection->close());
}

/**
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionClosed::reconnect
*/
public function testReconnect()
{
$connection = new ConnectionClosed();
$this->assertEquals('Reconnecting', $connection->reconnect());
}
}
41 changes: 41 additions & 0 deletions tests/Behavioral/State/ConnectionEstablishedTest.php
@@ -0,0 +1,41 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 15.07.15
* Time: 00:41
*/

namespace Rpodwika\Designpatterns\Tests\Behavioral\State;

use Rpodwika\Designpatterns\Behavioral\State\ConnectionEstablished;

class ConnectionEstablishedTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionEstablished::open
*/
public function testOpen()
{
$connection = new ConnectionEstablished();
$this->assertEquals('Cannot open already opened connection', $connection->open());
}

/**
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionEstablished::close
*/
public function testClose()
{
$connection = new ConnectionEstablished();
$this->assertEquals('Freeing resources', $connection->close());
}

/**
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionEstablished::reconnect
*/
public function testReconnect()
{
$connection = new ConnectionEstablished();
$this->assertEquals('Waiting for reconnection', $connection->reconnect());
}
}

0 comments on commit c8e08be

Please sign in to comment.