Skip to content

Php examples #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples-per-language/php/behavioral/Chain_Of_Responsibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use designPatternsForHumans\behavioral\Chain_Of_Responsibility\Bank;
use designPatternsForHumans\behavioral\Chain_Of_Responsibility\BitCoin;
use designPatternsForHumans\behavioral\Chain_Of_Responsibility\Paypal;

require_once __DIR__ . '/autoload.php';

// Let's prepare a chain like below
// $bank->$paypal->$bitcoin

$bank = new Bank(100);
$paypal = new Paypal(200);
$bitcoin = new BitCoin(300);

// First priority bank
// If bank can't pay then paypal
// If paypal can't pay then bit coin.
$bank->setNext($paypal);
$paypal->setNext($bitcoin);

// Let's try and pay our bill.
$bank->pay(259);
17 changes: 17 additions & 0 deletions examples-per-language/php/behavioral/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require_once __DIR__ . '/autoload.php';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use composer to set psr-4 and avoid put this line everywhere

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true, but as far as I'm concerned, I tried to make these examples as such that they are not dependent on Composer or any such 'mechanism', hence why I wrote my own little autoloader.

I might rework this to only have one autoload.php, but at the time I was thinking that maybe people wanted to test one set of examples, that is why I used the require_once on each of the examples.


use designPatternsForHumans\behavioral\Command\Bulb;
use designPatternsForHumans\behavioral\Command\RemoteControl;
use designPatternsForHumans\behavioral\Command\TurnOff;
use designPatternsForHumans\behavioral\Command\TurnOn;

$bulb = new Bulb();

$turnOn = new TurnOn($bulb);
$turnOff = new TurnOff($bulb);

$remote = new RemoteControl();
$remote->submit($turnOn);
$remote->submit($turnOff);
31 changes: 31 additions & 0 deletions examples-per-language/php/behavioral/Iterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use designPatternsForHumans\behavioral\Iterator\RadioStation;
use designPatternsForHumans\behavioral\Iterator\StationList;

require_once __DIR__ . '/autoload.php';

$stationList = new StationList();

$stationList->addStation(new RadioStation(89));
$stationList->addStation(new RadioStation(101));
$stationList->addStation(new RadioStation(102));
$stationList->addStation(new RadioStation(103.2));

/** @var RadioStation $station */
echo 'These are the known Radio Stations' . PHP_EOL;
foreach ($stationList as $station) {
echo $station->getFrequency(). PHP_EOL;
}

$stationList->removeStation(new RadioStation(89));

// To prove the station was indeed removed, we iterate through the list again.
// Small caveat : Iterators need to be rewinded after beeing Iterated!
$stationList->rewind();

/** @var RadioStation $station */
echo 'These are the known Radio Stations' . PHP_EOL;
foreach ($stationList as $station) {
echo $station->getFrequency(). PHP_EOL;
}
14 changes: 14 additions & 0 deletions examples-per-language/php/behavioral/Mediator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use designPatternsForHumans\behavioral\Mediator\ChatRoom;
use designPatternsForHumans\behavioral\Mediator\User;

require_once __DIR__ . '/autoload.php';

$mediator = new ChatRoom();

$john = new User('John Doe', $mediator);
$jane = new User('Jane Doe', $mediator);

$john->send('Hi there!');
$jane->send('Hey!');
20 changes: 20 additions & 0 deletions examples-per-language/php/behavioral/Memento.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use designPatternsForHumans\behavioral\Memento\Editor;

require_once __DIR__ . '/autoload.php';

$editor = new Editor();

$editor->type('This is the first sentence' . PHP_EOL);
$editor->type('This is the second sentence' . PHP_EOL);

$saved = $editor->save();

$editor->type('And this is the third one.' . PHP_EOL);

echo $editor->getContent();

$editor->restore($saved);
echo 'Restored $editor.' . PHP_EOL;
echo $editor->getContent();
16 changes: 16 additions & 0 deletions examples-per-language/php/behavioral/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use designPatternsForHumans\behavioral\Observer\JobPost;
use designPatternsForHumans\behavioral\Observer\JobPostings;
use designPatternsForHumans\behavioral\Observer\JobSeeker;

require_once __DIR__ . '/autoload.php';

$john = new JobSeeker('John Doe');
$jane = new JobSeeker('Jane Doe');

$jobPostings = new JobPostings();
$jobPostings->attach($john);
$jobPostings->attach($jane);

$jobPostings->add(new JobPost('Software Engineer'));
22 changes: 22 additions & 0 deletions examples-per-language/php/behavioral/State.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use designPatternsForHumans\behavioral\State\DefaultState;
use designPatternsForHumans\behavioral\State\LowerCase;
use designPatternsForHumans\behavioral\State\TextEditor;
use designPatternsForHumans\behavioral\State\UpperCase;

require_once __DIR__ . '/autoload.php';

$editor = new TextEditor(new DefaultState());

$editor->type('First line');

$editor->setState(new UpperCase());

$editor->type('Second line');
$editor->type('Third line');

$editor->setState(new LowerCase());

$editor->type('Fourth line');
$editor->type('Fifth line');
15 changes: 15 additions & 0 deletions examples-per-language/php/behavioral/Strategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use designPatternsForHumans\behavioral\Strategy\BubbleSortStrategy;
use designPatternsForHumans\behavioral\Strategy\QuickSortStrategy;
use designPatternsForHumans\behavioral\Strategy\Sorter;

require_once __DIR__ . '/autoload.php';

$dataset = [1, 5, 2, 6, 3];

$sorter = new Sorter(new BubbleSortStrategy());
$sorter->sort($dataset);

$sorter = new Sorter(new QuickSortStrategy());
$sorter->sort($dataset);
12 changes: 12 additions & 0 deletions examples-per-language/php/behavioral/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use designPatternsForHumans\behavioral\Template\AndroidBuilder;
use designPatternsForHumans\behavioral\Template\IosBuilder;

require_once __DIR__ . '/autoload.php';

$androidBuilder = new AndroidBuilder();
$androidBuilder->build();

$iosBuilder = new IosBuilder();
$iosBuilder->build();
29 changes: 29 additions & 0 deletions examples-per-language/php/behavioral/Visitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use designPatternsForHumans\behavioral\Visitor\Dolphin;
use designPatternsForHumans\behavioral\Visitor\Jump;
use designPatternsForHumans\behavioral\Visitor\Lion;
use designPatternsForHumans\behavioral\Visitor\Monkey;
use designPatternsForHumans\behavioral\Visitor\Speak;

require_once __DIR__ . '/autoload.php';

$monkey = new Monkey();
$lion = new Lion();
$dolphin = new Dolphin();

// This is the visitor.
$speak = new Speak();

$monkey->accept($speak);
$lion->accept($speak);
$dolphin->accept($speak);

// Using the Visitor pattern it becomes easy to 'add' new functionality to
// the animals. Declare functionality in a class that implements Visitor.

$jump = new Jump();

$monkey->accept($jump);
$lion->accept($jump);
$dolphin->accept($jump);
7 changes: 7 additions & 0 deletions examples-per-language/php/behavioral/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

spl_autoload_register(function($className) {
$explode = explode("\\", $className);
$slice = array_slice($explode, -2, 2);
require_once 'src' . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $slice) . '.php';
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility;

abstract class Account
{
protected $balance;
/** @var Account */
protected $successor;

public function setNext(Account $account)
{
$this->successor = $account;
}

public function pay($amountToPay)
{
if ($this->canPay($amountToPay)) {
echo sprintf('Paid %s using %s' . PHP_EOL, $amountToPay,
get_called_class());
} elseif ($this->successor) {
echo sprintf('Cannot pay using %s. Proceeding ...' . PHP_EOL,
get_called_class());
$this->successor->pay($amountToPay);
} else {
throw new \Exception('None of the accounts have enough balance');
}
}

public function canPay($amount)
{
return $this->balance >= $amount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility;


class Bank extends Account
{
// In the example the balance property is redefined, but that is not
// necessary, since we extend Account, which has that property.


public function __construct($balance)
{
$this->balance = $balance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility;


class BitCoin extends Account
{
// In the example the balance property is redefined, but that is not
// necessary, since we extend Account, which has that property.


public function __construct($balance)
{
$this->balance = $balance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility;


class Paypal extends Account
{
// In the example the balance property is redefined, but that is not
// necessary, since we extend Account, which has that property.


public function __construct($balance)
{
$this->balance = $balance;
}
}
19 changes: 19 additions & 0 deletions examples-per-language/php/behavioral/src/Command/Bulb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace designPatternsForHumans\behavioral\Command;


// This is the receiver, it has the implementation of the actions we want to perform.
class Bulb
{
public function turnOn()
{
echo 'Bulb has been lit!' . PHP_EOL;
}

public function turnOff()
{
echo 'Darkness!' . PHP_EOL;
}

}
12 changes: 12 additions & 0 deletions examples-per-language/php/behavioral/src/Command/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace designPatternsForHumans\behavioral\Command;


interface Command
{
public function execute();
public function undo();
public function redo();

}
13 changes: 13 additions & 0 deletions examples-per-language/php/behavioral/src/Command/RemoteControl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace designPatternsForHumans\behavioral\Command;

// This is the invoker, this will be used by client to interact.
class RemoteControl
{
public function submit(Command $command)
{
$command->execute();
}

}
30 changes: 30 additions & 0 deletions examples-per-language/php/behavioral/src/Command/TurnOff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace designPatternsForHumans\behavioral\Command;


class TurnOff implements Command
{

protected $bulb;

public function __construct(Bulb $bulb)
{
$this->bulb = $bulb;
}

public function execute()
{
$this->bulb->turnOff();
}

public function undo()
{
$this->bulb->turnOn();
}

public function redo()
{
$this->execute();
}
}
Loading