mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-09 09:34:46 +02:00
removed/moved newly added patterns
This commit is contained in:
@ -1,11 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Bridge;
|
|
||||||
|
|
||||||
class Assemble implements Workshop {
|
|
||||||
|
|
||||||
public function work() {
|
|
||||||
print 'Assembled';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Bridge;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Refined Abstraction
|
|
||||||
*/
|
|
||||||
class Car extends Vehicle {
|
|
||||||
|
|
||||||
public function __construct(Workshop $workShop1, Workshop $workShop2) {
|
|
||||||
parent::__construct($workShop1, $workShop2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function manufacture() {
|
|
||||||
print 'Car ';
|
|
||||||
$this->workShop1->work();
|
|
||||||
$this->workShop2->work();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Bridge;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Refined Abstraction
|
|
||||||
*/
|
|
||||||
class Motorcycle extends Vehicle {
|
|
||||||
|
|
||||||
public function __construct(Workshop $workShop1, Workshop $workShop2) {
|
|
||||||
parent::__construct($workShop1, $workShop2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function manufacture() {
|
|
||||||
print 'Motorcycle ';
|
|
||||||
$this->workShop1->work();
|
|
||||||
$this->workShop2->work();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Bridge;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Concrete Implementation
|
|
||||||
*/
|
|
||||||
class Produce implements Workshop {
|
|
||||||
|
|
||||||
public function work() {
|
|
||||||
print 'Produced ';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
## Purpose
|
|
||||||
|
|
||||||
Decouple an abstraction from its implementation so that the two can vary independently (http://en.wikipedia.org/wiki/Bridge_pattern)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Bridge;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstraction
|
|
||||||
*/
|
|
||||||
abstract class Vehicle {
|
|
||||||
|
|
||||||
protected $workShop1;
|
|
||||||
protected $workShop2;
|
|
||||||
|
|
||||||
protected function __construct(Workshop $workShop1, Workshop $workShop2) {
|
|
||||||
$this->workShop1 = $workShop1;
|
|
||||||
$this->workShop2 = $workShop2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function manufacture() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Bridge;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implementer
|
|
||||||
*/
|
|
||||||
interface Workshop {
|
|
||||||
|
|
||||||
public function work();
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Command;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Client is responsible for creating a ConcreteCommand and setting its a Receiver
|
|
||||||
*/
|
|
||||||
class Client
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Creates a ConcreteCommand object, sets its receiver and test invoker
|
|
||||||
*/
|
|
||||||
public function run()
|
|
||||||
{
|
|
||||||
$receiver = new Receiver();
|
|
||||||
$helloCommand = new HelloCommand($receiver);
|
|
||||||
|
|
||||||
$invoker = new Invoker();
|
|
||||||
$invoker->setCommand($helloCommand);
|
|
||||||
$invoker->run();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Memento;
|
|
||||||
|
|
||||||
class Caretaker
|
|
||||||
{
|
|
||||||
public static function run()
|
|
||||||
{
|
|
||||||
/* @var $savedStates Memento[] */
|
|
||||||
|
|
||||||
$savedStates = array();
|
|
||||||
|
|
||||||
$originator = new Originator();
|
|
||||||
|
|
||||||
//Setting state to State1
|
|
||||||
$originator->setState("State1");
|
|
||||||
//Setting state to State2
|
|
||||||
$originator->setState("State2");
|
|
||||||
//Saving State2 to Memento
|
|
||||||
$savedStates[] = $originator->saveToMemento();
|
|
||||||
//Setting state to State3
|
|
||||||
$originator->setState("State3");
|
|
||||||
|
|
||||||
// We can request multiple mementos, and choose which one to roll back to.
|
|
||||||
// Saving State3 to Memento
|
|
||||||
$savedStates[] = $originator->saveToMemento();
|
|
||||||
//Setting state to State4
|
|
||||||
$originator->setState("State4");
|
|
||||||
|
|
||||||
$originator->restoreFromMemento($savedStates[1]);
|
|
||||||
//State after restoring from Memento: State3
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Memento;
|
|
||||||
|
|
||||||
class Memento
|
|
||||||
{
|
|
||||||
/* @var mixed */
|
|
||||||
private $state;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $stateToSave
|
|
||||||
*/
|
|
||||||
public function __construct($stateToSave)
|
|
||||||
{
|
|
||||||
$this->state = $stateToSave;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getState()
|
|
||||||
{
|
|
||||||
return $this->state;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Memento;
|
|
||||||
|
|
||||||
class Originator
|
|
||||||
{
|
|
||||||
/* @var mixed */
|
|
||||||
private $state;
|
|
||||||
|
|
||||||
// The class could also contain additional data that is not part of the
|
|
||||||
// state saved in the memento..
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $state
|
|
||||||
*/
|
|
||||||
public function setState($state)
|
|
||||||
{
|
|
||||||
$this->state = $state;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Memento
|
|
||||||
*/
|
|
||||||
public function saveToMemento()
|
|
||||||
{
|
|
||||||
$state = is_object($this->state) ? clone $this->state : $this->state;
|
|
||||||
|
|
||||||
return new Memento($state);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function restoreFromMemento(Memento $memento)
|
|
||||||
{
|
|
||||||
$this->state = $memento->getState();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
# Memento
|
|
||||||
|
|
||||||
## Purpose
|
|
||||||
|
|
||||||
Provide the ability to restore an object to its previous state (undo via rollback).
|
|
||||||
|
|
||||||
The memento pattern is implemented with three objects: the originator, a caretaker and a memento.
|
|
||||||
The originator is some object that has an internal state.
|
|
||||||
The caretaker is going to do something to the originator, but wants to be able to undo the change.
|
|
||||||
The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do.
|
|
||||||
To roll back to the state before the operations, it returns the memento object to the originator.
|
|
||||||
The memento object itself is an opaque object (one which the caretaker cannot, or should not, change).
|
|
||||||
When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
* The seed of a pseudorandom number generator
|
|
||||||
* The state in a finite state machine
|
|
@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Tests\Bridge;
|
|
||||||
|
|
||||||
use DesignPatterns\Bridge\Assemble;
|
|
||||||
use DesignPatterns\Bridge\Car;
|
|
||||||
use DesignPatterns\Bridge\Motorcycle;
|
|
||||||
use DesignPatterns\Bridge\Produce;
|
|
||||||
use DesignPatterns\Bridge\Vehicle;
|
|
||||||
use DesignPatterns\Bridge\Workshop;
|
|
||||||
|
|
||||||
class BridgeTest extends \PHPUnit_Framework_TestCase {
|
|
||||||
|
|
||||||
public function testCar() {
|
|
||||||
$vehicle = new Car(new Produce(), new Assemble());
|
|
||||||
$this->expectOutputString('Car Produced Assembled');
|
|
||||||
$vehicle->manufacture();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testMotorcycle() {
|
|
||||||
$vehicle = new Motorcycle(new Produce(), new Assemble());
|
|
||||||
$this->expectOutputString('Motorcycle Produced Assembled');
|
|
||||||
$vehicle->manufacture();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Tests\Memento;
|
|
||||||
|
|
||||||
use DesignPatterns\Memento;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MementoTest tests the memento pattern
|
|
||||||
*/
|
|
||||||
class MementoTest extends \PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
|
|
||||||
public function testStringState()
|
|
||||||
{
|
|
||||||
$originator = new Memento\Originator();
|
|
||||||
$originator->setState("State1");
|
|
||||||
|
|
||||||
$this->assertAttributeEquals("State1", "state", $originator);
|
|
||||||
|
|
||||||
$originator->setState("State2");
|
|
||||||
|
|
||||||
$this->assertAttributeEquals("State2", "state", $originator);
|
|
||||||
|
|
||||||
$savedState = $originator->saveToMemento();
|
|
||||||
|
|
||||||
$this->assertAttributeEquals("State2", "state", $savedState);
|
|
||||||
|
|
||||||
$originator->setState("State3");
|
|
||||||
|
|
||||||
$this->assertAttributeEquals("State3", "state", $originator);
|
|
||||||
|
|
||||||
$originator->restoreFromMemento($savedState);
|
|
||||||
|
|
||||||
$this->assertAttributeEquals("State2", "state", $originator);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testObjectState()
|
|
||||||
{
|
|
||||||
$originator = new Memento\Originator();
|
|
||||||
|
|
||||||
$foo = new \stdClass();
|
|
||||||
$foo->data = "foo";
|
|
||||||
|
|
||||||
$originator->setState($foo);
|
|
||||||
|
|
||||||
$this->assertAttributeEquals($foo, "state", $originator);
|
|
||||||
|
|
||||||
$savedState = $originator->saveToMemento();
|
|
||||||
|
|
||||||
$this->assertAttributeEquals($foo, "state", $savedState);
|
|
||||||
|
|
||||||
$bar = new \stdClass();
|
|
||||||
$bar->data = "bar";
|
|
||||||
|
|
||||||
$originator->setState($bar);
|
|
||||||
|
|
||||||
$this->assertAttributeEquals($bar, "state", $originator);
|
|
||||||
|
|
||||||
$originator->restoreFromMemento($savedState);
|
|
||||||
|
|
||||||
$this->assertAttributeEquals($foo, "state", $originator);
|
|
||||||
|
|
||||||
$foo->data = null;
|
|
||||||
|
|
||||||
$this->assertAttributeNotEquals($foo, "state", $savedState);
|
|
||||||
|
|
||||||
$this->assertAttributeNotEquals($foo, "state", $originator);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user