Skip to content

Commit

Permalink
Merge branch 'release/1.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rpodwika committed Jul 13, 2015
2 parents e5101aa + 8a4a744 commit 51c3ba3
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 10 deletions.
35 changes: 29 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,35 @@ pull request or message me
##Implemented patterns

1. Antipatterns
* [Singleton](src/Antipatterns/)
* [Singleton](src/Antipatterns/) - One global instance of a class
2. Behavioral
* [Memento](src/Behavioral/Memento)
* [Observer](src/Behavioral/Observer)
* [Strategy](src/Behavioral/Strategy)
* [Memento](src/Behavioral/Memento) - Without violating encapsulation, capture and externalize an object's internal
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
* [Strategy](src/Behavioral/Strategy) - Define a family of algorithms and make them interchangable.
3. Creational
* [Factory](src/Creational/Factory)
* [Factory](src/Creational/Factory) - Define an interface for creating an object but let the subclasses decide
which class to instantiate
4. Structural
* [Decorator](src/Structural/Decorator)
* [Decorator](src/Structural/Decorator) - Attach additional object features dynamically without subclassing
or modifying source class.

##To be implemented

* Abstract factory
* Builder
* Prototype
* Bridge
* Composite
* Facade
* Flyweight
* Proxy
* Chain of responsibility
* Command
* Interpreter
* Iterator
* Mediator
* State
* Visitor

34 changes: 34 additions & 0 deletions src/Behavioral/Adapter/EmailClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 11.07.15
* Time: 20:35
*/

namespace Rpodwika\Designpatterns\Behavioral\Adapter;


class EmailClient
{
public function __construct()
{

}

/**
* @return string
*/
public function sendEmail()
{
return 'I sent email';
}

/**
* @return string
*/
public function sendUtf8Email()
{
return 'I sent UTF-8 email';
}
}
25 changes: 25 additions & 0 deletions src/Behavioral/Adapter/EmailClientOnlyUtfAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 11.07.15
* Time: 20:43
*/

namespace Rpodwika\Designpatterns\Behavioral\Adapter;


class EmailClientOnlyUtfAdapter implements OnlyUtfSender
{
private $emailClient;

function __construct(EmailClient $emailClient)
{
$this->emailClient = $emailClient;
}

public function send()
{
return $this->emailClient->sendUtf8Email();
}
}
15 changes: 15 additions & 0 deletions src/Behavioral/Adapter/OnlyUtfSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 11.07.15
* Time: 20:32
*/

namespace Rpodwika\Designpatterns\Behavioral\Adapter;


interface OnlyUtfSender
{
public function send();
}
21 changes: 21 additions & 0 deletions src/Behavioral/Adapter/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#Adapter pattern

Adapter pattern allows the interface of existing class to be used from another interface. It is often used to adapt
other classes to work with other without modifying original class source. Adapter lets classes work together despite to
incompatible interfaces.

You can compare it to real world situation. You have device A and B that sends same data but in different format for example
one sends audio in MP3 and the other in wav so you need adapter that converts audio from one device to be readable by
the other one.

##When to use

1. When you want to adapt class behavior to new conditions without modifying source of original class
2. When you want to secure some methods from original class not to be visible for example because they use large
amount of resources.
3. When you want to use existing class but its interface does not match the one you need
4.

##How to use

See [tests/DecoratorTest](/tests/DecoratorTest.php)
39 changes: 39 additions & 0 deletions tests/AdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* Created by PhpStorm.
* User: robertpodwikamac
* Date: 11.07.15
* Time: 20:47
*/

use \Rpodwika\Designpatterns\Behavioral\Adapter\EmailClient;
use \Rpodwika\Designpatterns\Behavioral\Adapter\EmailClientOnlyUtfAdapter;

class AdapterTest extends PHPUnit_Framework_TestCase
{
/**
* @covers \Rpodwika\Designpatterns\Behavioral\Adapter\EmailClient::sendUtf8Email
*/
public function testEmailClientSendsUtf8()
{
$emailClient = new EmailClient();
$this->assertEquals('I sent UTF-8 email', $emailClient->sendUtf8Email());
}

/**
* @covers \Rpodwika\Designpatterns\Behavioral\Adapter\EmailClient::sendEmail
*/
public function testEmailClientSendsPlainEmail()
{
$emailClient = new EmailClient();
$this->assertEquals('I sent email', $emailClient->sendEmail());
}

public function testEmailAdapterSendsEmail()
{
$emailClient = new EmailClient();
$emailClientUffAdapter = new EmailClientOnlyUtfAdapter($emailClient);
$this->assertEquals('I sent UTF-8 email', $emailClientUffAdapter->send());
}
}
21 changes: 17 additions & 4 deletions tests/SamsungFactoryTest.php → tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
* Time: 23:18
*/
use \Rpodwika\Designpatterns\Creational\Factory\SamsungFactory;
use \Rpodwika\Designpatterns\Creational\Factory\IphoneFactory;

class SamsungFactoryTest extends PHPUnit_Framework_TestCase
class FactoryTest extends PHPUnit_Framework_TestCase
{
/**
* @covers \Rpodwika\Designpatterns\Creational\Factory\Samsung::doGreatPhoto
* @covers \Rpodwika\Designpatterns\Creational\Factory\SamsungFactory::makePhone
* @covers \Rpodwika\Designpatterns\Creational\Factory\SamsungFactory
* @covers \Rpodwika\Designpatterns\Creational\Factory\Samsung
*/
public function testMakePhone()
public function testSamsungFactoryMakePhone()
{
$samsungFactory = new SamsungFactory();
$samsung = $samsungFactory->makePhone();
Expand All @@ -23,4 +24,16 @@ public function testMakePhone()
$this->assertEquals('great photo', $samsung->doGreatPhoto());
$this->assertInstanceOf('Rpodwika\Designpatterns\Creational\Factory\Samsung', $samsung);
}

/**
* @covers \Rpodwika\Designpatterns\Creational\Factory\Iphone
* @covers \Rpodwika\Designpatterns\Creational\Factory\IphoneFactory
*/
public function testIphoneFactoryMakePhone()
{
$iphoneFactory = new IphoneFactory();
$iphone = $iphoneFactory->makePhone();
$this->assertNotNull($iphone);
$this->assertInstanceOf('Rpodwika\Designpatterns\Creational\Factory\Iphone', $iphone);
}
}

0 comments on commit 51c3ba3

Please sign in to comment.