├── .gitignore ├── Structural ├── DependencyInjection │ ├── Tests │ │ ├── config.php │ │ └── DependencyInjectionTest.php │ ├── uml │ │ └── uml.png │ ├── AbstractConfig.php │ ├── Parameters.php │ ├── ArrayConfig.php │ └── Connection.php ├── Adapter │ ├── uml │ │ └── uml.png │ ├── Book.php │ ├── Kindle.php │ ├── EBookInterface.php │ ├── PaperBookInterface.php │ ├── EBookAdapter.php │ ├── Tests │ │ └── AdapterTest.php │ └── README.rst ├── Bridge │ ├── uml │ │ └── uml.png │ ├── Workshop.php │ ├── Assemble.php │ ├── Produce.php │ ├── Vehicle.php │ ├── Car.php │ ├── Motorcycle.php │ ├── Tests │ │ └── BridgeTest.php │ └── README.rst ├── Facade │ ├── uml │ │ ├── uml.png │ │ └── Facade.uml │ ├── OsInterface.php │ ├── BiosInterface.php │ ├── Facade.php │ └── Tests │ │ └── FacadeTest.php ├── Proxy │ ├── uml │ │ ├── uml.png │ │ └── Proxy.uml │ ├── README.rst │ ├── Record.php │ └── RecordProxy.php ├── Composite │ ├── uml │ │ ├── uml.png │ │ └── uml.txt │ ├── FormElement.php │ ├── TextElement.php │ ├── InputElement.php │ ├── Form.php │ ├── Tests │ │ └── CompositeTest.php │ └── README.rst ├── Decorator │ ├── uml │ │ └── uml.png │ ├── RendererInterface.php │ ├── RenderInJson.php │ ├── Webservice.php │ ├── RenderInXml.php │ ├── Decorator.php │ └── README.rst ├── Registry │ ├── uml │ │ ├── uml.png │ │ └── Registry.uml │ ├── Tests │ │ └── RegistryTest.php │ ├── Registry.php │ └── README.rst ├── DataMapper │ └── uml │ │ ├── uml.png │ │ └── DataMapper.uml ├── FluentInterface │ ├── uml │ │ ├── uml.png │ │ └── FluentInterface.uml │ ├── Tests │ │ └── FluentInterfaceTest.php │ └── README.rst ├── README.rst └── README.md ├── Creational ├── Pool │ ├── uml │ │ ├── uml.png │ │ └── Pool.uml │ ├── Tests │ │ ├── TestWorker.php │ │ └── PoolTest.php │ ├── Worker.php │ ├── Pool.php │ └── Processor.php ├── Builder │ ├── uml │ │ └── uml.png │ ├── Parts │ │ ├── Door.php │ │ ├── Wheel.php │ │ ├── Engine.php │ │ ├── Car.php │ │ ├── Bike.php │ │ └── Vehicle.php │ ├── BuilderInterface.php │ ├── Director.php │ ├── BikeBuilder.php │ ├── Tests │ │ └── DirectorTest.php │ └── CarBuilder.php ├── Multiton │ ├── uml │ │ ├── uml.png │ │ └── Multiton.uml │ ├── README.rst │ └── Multiton.php ├── Prototype │ ├── uml │ │ ├── uml.png │ │ └── Prototype.uml │ ├── FooBookPrototype.php │ ├── BarBookPrototype.php │ ├── index.php │ ├── BookPrototype.php │ └── README.rst ├── Singleton │ ├── uml │ │ ├── uml.png │ │ └── Singleton.uml │ ├── Tests │ │ └── SingletonTest.php │ ├── Singleton.php │ └── README.rst ├── FactoryMethod │ ├── uml │ │ └── uml.png │ ├── VehicleInterface.php │ ├── Ferrari.php │ ├── Bicycle.php │ ├── Porsche.php │ ├── ItalianFactory.php │ ├── GermanFactory.php │ ├── FactoryMethod.php │ └── Tests │ │ └── FactoryMethodTest.php ├── SimpleFactory │ ├── uml │ │ └── uml.png │ ├── Scooter.php │ ├── VehicleInterface.php │ ├── Bicycle.php │ ├── Tests │ │ └── SimpleFactoryTest.php │ ├── ConcreteFactory.php │ └── README.rst ├── StaticFactory │ ├── uml │ │ └── uml.png │ ├── FormatterInterface.php │ ├── FormatNumber.php │ ├── FormatString.php │ ├── StaticFactory.php │ ├── Tests │ │ └── StaticFactoryTest.php │ └── README.rst ├── AbstractFactory │ ├── uml │ │ └── uml.png │ ├── Text.php │ ├── MediaInterface.php │ ├── Html │ │ ├── Text.php │ │ └── Picture.php │ ├── Json │ │ ├── Text.php │ │ └── Picture.php │ ├── Picture.php │ ├── HtmlFactory.php │ ├── JsonFactory.php │ ├── AbstractFactory.php │ └── Tests │ │ └── AbstractFactoryTest.php ├── README.rst └── README.md ├── More ├── Delegation │ ├── uml │ │ ├── uml.png │ │ └── Delegation.uml │ ├── Usage.php │ ├── JuniorDeveloper.php │ ├── Tests │ │ └── DelegationTest.php │ ├── TeamLead.php │ └── README.rst ├── Repository │ ├── uml │ │ ├── uml.png │ │ └── Repository.uml │ ├── Storage.php │ ├── MemoryStorage.php │ └── README.rst ├── ServiceLocator │ ├── uml │ │ └── uml.png │ ├── LogServiceInterface.php │ ├── LogService.php │ ├── DatabaseServiceInterface.php │ ├── DatabaseService.php │ └── ServiceLocatorInterface.php ├── README.rst └── README.md ├── Behavioral ├── Command │ ├── uml │ │ ├── uml.png │ │ └── Command.uml │ ├── Receiver.php │ ├── CommandInterface.php │ ├── Invoker.php │ ├── Tests │ │ └── CommandTest.php │ └── HelloCommand.php ├── Memento │ ├── uml │ │ ├── uml.png │ │ └── Momento.uml │ ├── Memento.php │ ├── Originator.php │ └── Caretaker.php ├── State │ ├── uml │ │ └── uml.png │ ├── OrderInterface.php │ ├── OrderController.php │ ├── OrderFactory.php │ ├── CreateOrder.php │ ├── ShippingOrder.php │ └── README.rst ├── Visitor │ ├── uml │ │ └── uml.png │ ├── Group.php │ ├── RolePrintVisitor.php │ ├── User.php │ ├── RoleVisitorInterface.php │ ├── Role.php │ ├── Tests │ │ └── VisitorTest.php │ └── README.rst ├── Iterator │ ├── uml │ │ ├── uml.png │ │ └── Iterator.uml │ ├── BookListReverseIterator.php │ ├── Book.php │ ├── BookList.php │ └── README.rst ├── Mediator │ ├── uml │ │ └── uml.png │ ├── Subsystem │ │ ├── Database.php │ │ ├── Server.php │ │ └── Client.php │ ├── MediatorInterface.php │ ├── Colleague.php │ ├── Tests │ │ └── MediatorTest.php │ └── Mediator.php ├── Observer │ ├── uml │ │ ├── uml.png │ │ └── Observer.uml │ ├── UserObserver.php │ └── README.rst ├── Strategy │ ├── uml │ │ ├── uml.png │ │ └── Strategy.uml │ ├── ComparatorInterface.php │ ├── IdComparator.php │ ├── DateComparator.php │ ├── ObjectCollection.php │ └── README.rst ├── NullObject │ ├── uml │ │ ├── uml.png │ │ └── NullObject.uml │ ├── PrintLogger.php │ ├── LoggerInterface.php │ ├── NullLogger.php │ ├── Service.php │ └── Tests │ │ └── LoggerTest.php ├── Specification │ ├── uml │ │ └── uml.png │ ├── Item.php │ ├── Not.php │ ├── SpecificationInterface.php │ ├── Plus.php │ ├── Either.php │ ├── PriceSpecification.php │ └── AbstractSpecification.php ├── TemplateMethod │ ├── uml │ │ ├── uml.png │ │ └── TemplateMethod.uml │ ├── BeachJourney.php │ ├── CityJourney.php │ ├── Tests │ │ └── JourneyTest.php │ └── README.rst ├── ChainOfResponsibilities │ ├── uml │ │ └── uml.png │ ├── Request.php │ └── Responsible │ │ ├── FastStorage.php │ │ └── SlowStorage.php ├── README.rst └── README.md ├── .travis.yml ├── locale ├── ca │ └── LC_MESSAGES │ │ ├── More │ │ ├── README.po │ │ └── Delegation │ │ │ └── README.po │ │ ├── Structural │ │ └── README.po │ │ ├── Behavioral │ │ └── README.po │ │ └── Creational │ │ └── README.po ├── es │ └── LC_MESSAGES │ │ ├── More │ │ ├── README.po │ │ └── Delegation │ │ │ └── README.po │ │ ├── Structural │ │ └── README.po │ │ ├── Behavioral │ │ └── README.po │ │ └── Creational │ │ └── README.po ├── ru │ └── LC_MESSAGES │ │ ├── More │ │ └── README.po │ │ ├── Structural │ │ └── README.po │ │ ├── Behavioral │ │ └── README.po │ │ └── Creational │ │ └── README.po ├── zh_CN │ └── LC_MESSAGES │ │ ├── More │ │ ├── README.po │ │ └── Delegation │ │ │ └── README.po │ │ ├── Structural │ │ └── README.po │ │ ├── Behavioral │ │ └── README.po │ │ └── Creational │ │ └── README.po └── pt_BR │ └── LC_MESSAGES │ ├── More │ ├── README.po │ └── Delegation │ │ └── README.po │ ├── Structural │ └── README.po │ ├── Behavioral │ └── README.po │ └── Creational │ └── README.po ├── composer.json ├── phpunit.xml.dist └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | # common 2 | .idea 3 | /nbproject 4 | /vendor/ 5 | _build/ 6 | *.mo 7 | -------------------------------------------------------------------------------- /Structural/DependencyInjection/Tests/config.php: -------------------------------------------------------------------------------- 1 | 'github.com'); 4 | -------------------------------------------------------------------------------- /Creational/Pool/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/Pool/uml/uml.png -------------------------------------------------------------------------------- /More/Delegation/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/More/Delegation/uml/uml.png -------------------------------------------------------------------------------- /More/Repository/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/More/Repository/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/Command/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/Command/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/Memento/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/Memento/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/State/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/State/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/Visitor/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/Visitor/uml/uml.png -------------------------------------------------------------------------------- /Creational/Builder/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/Builder/uml/uml.png -------------------------------------------------------------------------------- /Structural/Adapter/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/Adapter/uml/uml.png -------------------------------------------------------------------------------- /Structural/Bridge/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/Bridge/uml/uml.png -------------------------------------------------------------------------------- /Structural/Facade/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/Facade/uml/uml.png -------------------------------------------------------------------------------- /Structural/Proxy/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/Proxy/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/Iterator/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/Iterator/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/Mediator/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/Mediator/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/Observer/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/Observer/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/Strategy/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/Strategy/uml/uml.png -------------------------------------------------------------------------------- /Creational/Multiton/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/Multiton/uml/uml.png -------------------------------------------------------------------------------- /Creational/Prototype/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/Prototype/uml/uml.png -------------------------------------------------------------------------------- /Creational/Singleton/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/Singleton/uml/uml.png -------------------------------------------------------------------------------- /More/ServiceLocator/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/More/ServiceLocator/uml/uml.png -------------------------------------------------------------------------------- /Structural/Composite/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/Composite/uml/uml.png -------------------------------------------------------------------------------- /Structural/Decorator/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/Decorator/uml/uml.png -------------------------------------------------------------------------------- /Structural/Registry/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/Registry/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/NullObject/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/NullObject/uml/uml.png -------------------------------------------------------------------------------- /Structural/DataMapper/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/DataMapper/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/Specification/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/Specification/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/TemplateMethod/uml/uml.png -------------------------------------------------------------------------------- /Creational/FactoryMethod/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/FactoryMethod/uml/uml.png -------------------------------------------------------------------------------- /Creational/SimpleFactory/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/SimpleFactory/uml/uml.png -------------------------------------------------------------------------------- /Creational/StaticFactory/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/StaticFactory/uml/uml.png -------------------------------------------------------------------------------- /Creational/AbstractFactory/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Creational/AbstractFactory/uml/uml.png -------------------------------------------------------------------------------- /Structural/FluentInterface/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/FluentInterface/uml/uml.png -------------------------------------------------------------------------------- /Structural/DependencyInjection/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Structural/DependencyInjection/uml/uml.png -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibilities/uml/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creocoder/DesignPatternsPHP/HEAD/Behavioral/ChainOfResponsibilities/uml/uml.png -------------------------------------------------------------------------------- /More/ServiceLocator/LogServiceInterface.php: -------------------------------------------------------------------------------- 1 | writeCode(); 10 | -------------------------------------------------------------------------------- /Structural/Decorator/RendererInterface.php: -------------------------------------------------------------------------------- 1 | storage = $storage; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/CityJourney.php: -------------------------------------------------------------------------------- 1 | text = (string) $text; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/Ferrari.php: -------------------------------------------------------------------------------- 1 | color = $rgb; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Structural/Decorator/RenderInJson.php: -------------------------------------------------------------------------------- 1 | wrapped->renderData(); 18 | 19 | return json_encode($output); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Behavioral/Strategy/IdComparator.php: -------------------------------------------------------------------------------- 1 | data[$key] = $value; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Structural/Composite/InputElement.php: -------------------------------------------------------------------------------- 1 | '; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Behavioral/Mediator/Subsystem/Server.php: -------------------------------------------------------------------------------- 1 | getMediator()->queryDb(); 18 | $this->getMediator()->sendResponse("Hello $data"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/Bicycle.php: -------------------------------------------------------------------------------- 1 | color = $rgb; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Structural/Bridge/Vehicle.php: -------------------------------------------------------------------------------- 1 | workShop1 = $workShop1; 17 | $this->workShop2 = $workShop2; 18 | } 19 | 20 | public function manufacture() 21 | { 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Structural/Registry/Tests/RegistryTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('StdClass', $logger); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Behavioral/Memento/Memento.php: -------------------------------------------------------------------------------- 1 | state = $stateToSave; 16 | } 17 | 18 | /** 19 | * @return mixed 20 | */ 21 | public function getState() 22 | { 23 | return $this->state; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Structural/Bridge/Car.php: -------------------------------------------------------------------------------- 1 | workShop1->work(); 20 | $this->workShop2->work(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/MediaInterface.php: -------------------------------------------------------------------------------- 1 | setTitle('Foo Book No ' . $i); 12 | } 13 | 14 | for ($i = 0; $i < 5000; $i++) { 15 | $book = clone $barPrototype; 16 | $book->setTitle('Bar Book No ' . $i); 17 | } 18 | -------------------------------------------------------------------------------- /Structural/Composite/uml/uml.txt: -------------------------------------------------------------------------------- 1 | @startuml 2 | class Form { 3 | #elements : array|FormElement[] 4 | +render($indent = 0 : int) 5 | +addElement(FormElement $element) 6 | } 7 | 8 | abstract class FormElement { 9 | +render($indent = 0 : int) 10 | } 11 | 12 | class InputElement { 13 | +render($indent = 0 : int) 14 | } 15 | 16 | class TextElement { 17 | +render($indent = 0 : int) 18 | } 19 | 20 | FormElement <|.. TextElement 21 | FormElement <|.. InputElement 22 | FormElement <|.. Form 23 | @enduml -------------------------------------------------------------------------------- /Creational/Pool/Worker.php: -------------------------------------------------------------------------------- 1 | \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../More/README.rst:2 15 | msgid "More" 16 | msgstr "" 17 | -------------------------------------------------------------------------------- /locale/es/LC_MESSAGES/More/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../More/README.rst:2 15 | msgid "More" 16 | msgstr "" 17 | -------------------------------------------------------------------------------- /locale/ru/LC_MESSAGES/More/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: Eugene Glotov \n" 9 | "MIME-Version: 1.0\n" 10 | "Content-Type: text/plain; charset=UTF-8\n" 11 | "Content-Transfer-Encoding: 8bit\n" 12 | "Language: ru\n" 13 | 14 | #: ../../More/README.rst:2 15 | msgid "More" 16 | msgstr "Дополнительно" 17 | -------------------------------------------------------------------------------- /Structural/Bridge/Motorcycle.php: -------------------------------------------------------------------------------- 1 | workShop1->work(); 20 | $this->workShop2->work(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /locale/zh_CN/LC_MESSAGES/More/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../More/README.rst:2 15 | msgid "More" 16 | msgstr "" 17 | -------------------------------------------------------------------------------- /locale/pt_BR/LC_MESSAGES/More/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../More/README.rst:2 15 | msgid "More" 16 | msgstr "Outros" 17 | -------------------------------------------------------------------------------- /Behavioral/Observer/UserObserver.php: -------------------------------------------------------------------------------- 1 | assertEquals($junior->writeBadCode(), $teamLead->writeCode()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Behavioral/Iterator/BookListReverseIterator.php: -------------------------------------------------------------------------------- 1 | bookList = $bookList; 11 | $this->currentBook = $this->bookList->count() - 1; 12 | } 13 | 14 | public function next() 15 | { 16 | $this->currentBook--; 17 | } 18 | 19 | public function valid() 20 | { 21 | return 0 <= $this->currentBook; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/Html/Text.php: -------------------------------------------------------------------------------- 1 | ' . htmlspecialchars($this->text) . ''; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/Json/Text.php: -------------------------------------------------------------------------------- 1 | $this->text)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /More/ServiceLocator/ServiceLocatorInterface.php: -------------------------------------------------------------------------------- 1 | price = $price; 19 | } 20 | 21 | /** 22 | * Get the items price 23 | * 24 | * @return int 25 | */ 26 | public function getPrice() 27 | { 28 | return $this->price; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Behavioral/Visitor/Group.php: -------------------------------------------------------------------------------- 1 | name = (string) $name; 21 | } 22 | 23 | /** 24 | * @return string 25 | */ 26 | public function getName() 27 | { 28 | return "Group: " . $this->name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Structural/Decorator/Webservice.php: -------------------------------------------------------------------------------- 1 | data = $data; 21 | } 22 | 23 | /** 24 | * @return string 25 | */ 26 | public function renderData() 27 | { 28 | return $this->data; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Structural/README.rst: -------------------------------------------------------------------------------- 1 | `Structural`__ 2 | ============== 3 | 4 | In Software Engineering, Structural Design Patterns are Design Patterns 5 | that ease the design by identifying a simple way to realize 6 | relationships between entities. 7 | 8 | .. toctree:: 9 | :titlesonly: 10 | 11 | Adapter/README 12 | Bridge/README 13 | Composite/README 14 | DataMapper/README 15 | Decorator/README 16 | DependencyInjection/README 17 | Facade/README 18 | FluentInterface/README 19 | Proxy/README 20 | Registry/README 21 | 22 | .. __: http://en.wikipedia.org/wiki/Structural_pattern -------------------------------------------------------------------------------- /Behavioral/NullObject/NullLogger.php: -------------------------------------------------------------------------------- 1 | ', $this->path, $this->name); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/Json/Picture.php: -------------------------------------------------------------------------------- 1 | $this->name, 'path' => $this->path)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Behavioral/Visitor/RolePrintVisitor.php: -------------------------------------------------------------------------------- 1 | getName(); 18 | } 19 | 20 | /** 21 | * {@inheritdoc} 22 | */ 23 | public function visitUser(User $role) 24 | { 25 | echo "Role: " . $role->getName(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Creational/Pool/Pool.php: -------------------------------------------------------------------------------- 1 | class = $class; 14 | } 15 | 16 | public function get() 17 | { 18 | if (count($this->instances) > 0) { 19 | return array_pop($this->instances); 20 | } 21 | 22 | return new $this->class(); 23 | } 24 | 25 | public function dispose($instance) 26 | { 27 | $this->instances[] = $instance; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Structural/Facade/BiosInterface.php: -------------------------------------------------------------------------------- 1 | name = (string) $name; 28 | $this->path = (string) $path; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Creational/Pool/Tests/PoolTest.php: -------------------------------------------------------------------------------- 1 | get(); 13 | 14 | $this->assertEquals(1, $worker->id); 15 | 16 | $worker->id = 5; 17 | $pool->dispose($worker); 18 | 19 | $this->assertEquals(5, $pool->get()->id); 20 | $this->assertEquals(1, $pool->get()->id); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Behavioral/Mediator/MediatorInterface.php: -------------------------------------------------------------------------------- 1 | getMediator()->makeRequest(); 18 | } 19 | 20 | /** 21 | * output content 22 | * 23 | * @param string $content 24 | */ 25 | public function output($content) 26 | { 27 | echo $content; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Behavioral/Visitor/User.php: -------------------------------------------------------------------------------- 1 | name = (string) $name; 23 | } 24 | 25 | /** 26 | * @return string 27 | */ 28 | public function getName() 29 | { 30 | return "User " . $this->name; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/Porsche.php: -------------------------------------------------------------------------------- 1 | color = $rgb; 21 | } 22 | 23 | /** 24 | * although tuning by AMG is only offered for Mercedes Cars, 25 | * this is a valid coding example ... 26 | */ 27 | public function addTuningAMG() 28 | { 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Behavioral/Iterator/Book.php: -------------------------------------------------------------------------------- 1 | author = $author; 15 | $this->title = $title; 16 | } 17 | 18 | public function getAuthor() 19 | { 20 | return $this->author; 21 | } 22 | 23 | public function getTitle() 24 | { 25 | return $this->title; 26 | } 27 | 28 | public function getAuthorAndTitle() 29 | { 30 | return $this->getTitle() . ' by ' . $this->getAuthor(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Behavioral/*/Tests 7 | Creational/*/Tests 8 | More/*/Tests 9 | Structural/*/Tests 10 | 11 | 12 | 13 | 14 | ./vendor 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Structural/Decorator/RenderInXml.php: -------------------------------------------------------------------------------- 1 | wrapped->renderData(); 18 | 19 | // do some fancy conversion to xml from array ... 20 | 21 | $doc = new \DOMDocument(); 22 | 23 | foreach ($output as $key => $val) { 24 | $doc->appendChild($doc->createElement($key, $val)); 25 | } 26 | 27 | return $doc->saveXML(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Creational/Multiton/uml/Multiton.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Creational\Multiton\Multiton 5 | 6 | \DesignPatterns\Creational\Multiton\Multiton 7 | 8 | 9 | 10 | 11 | 12 | 13 | Fields 14 | Constants 15 | Constructors 16 | Methods 17 | 18 | private 19 | 20 | 21 | -------------------------------------------------------------------------------- /Creational/Singleton/uml/Singleton.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Creational\Singleton\Singleton 5 | 6 | \DesignPatterns\Creational\Singleton\Singleton 7 | 8 | 9 | 10 | 11 | 12 | 13 | Fields 14 | Constants 15 | Constructors 16 | Methods 17 | 18 | private 19 | 20 | 21 | -------------------------------------------------------------------------------- /Structural/FluentInterface/Tests/FluentInterfaceTest.php: -------------------------------------------------------------------------------- 1 | select(array('foo', 'bar')) 17 | ->from('foobar', 'f') 18 | ->where('f.bar = ?') 19 | ->getQuery(); 20 | 21 | $this->assertEquals('SELECT foo,bar FROM foobar AS f WHERE f.bar = ?', $query); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/ItalianFactory.php: -------------------------------------------------------------------------------- 1 | title; 32 | } 33 | 34 | /** 35 | * @param string $title 36 | */ 37 | public function setTitle($title) 38 | { 39 | $this->title = $title; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /More/Delegation/TeamLead.php: -------------------------------------------------------------------------------- 1 | slave = $junior; 22 | } 23 | 24 | /** 25 | * TeamLead drink coffee, junior work 26 | * @return mixed 27 | */ 28 | public function writeCode() 29 | { 30 | return $this->slave->writeBadCode(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Creational/README.rst: -------------------------------------------------------------------------------- 1 | `Creational`__ 2 | ============== 3 | 4 | In software engineering, creational design patterns are design patterns 5 | that deal with object creation mechanisms, trying to create objects in a 6 | manner suitable to the situation. The basic form of object creation 7 | could result in design problems or added complexity to the design. 8 | Creational design patterns solve this problem by somehow controlling 9 | this object creation. 10 | 11 | .. toctree:: 12 | :titlesonly: 13 | 14 | AbstractFactory/README 15 | Builder/README 16 | FactoryMethod/README 17 | Multiton/README 18 | Pool/README 19 | Prototype/README 20 | SimpleFactory/README 21 | Singleton/README 22 | StaticFactory/README 23 | 24 | .. __: http://en.wikipedia.org/wiki/Creational_pattern 25 | -------------------------------------------------------------------------------- /More/Delegation/uml/Delegation.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\More\Delegation\JuniorDeveloper 5 | 6 | \DesignPatterns\More\Delegation\JuniorDeveloper 7 | \DesignPatterns\More\Delegation\TeamLead 8 | 9 | 10 | 11 | 12 | 13 | 14 | Fields 15 | Constants 16 | Constructors 17 | Methods 18 | 19 | private 20 | 21 | 22 | -------------------------------------------------------------------------------- /Structural/Registry/uml/Registry.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Structural\Registry\Registry 5 | 6 | \DesignPatterns\Structural\Registry\Registry 7 | 8 | 9 | 10 | 11 | 12 | \DesignPatterns\Structural\Registry\Registry 13 | 14 | 15 | Fields 16 | Constants 17 | Constructors 18 | Methods 19 | 20 | private 21 | 22 | 23 | -------------------------------------------------------------------------------- /locale/ca/LC_MESSAGES/Structural/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Structural/README.rst:2 15 | msgid "`Structural`__" 16 | msgstr "" 17 | 18 | #: ../../Structural/README.rst:4 19 | msgid "" 20 | "In Software Engineering, Structural Design Patterns are Design Patterns that" 21 | " ease the design by identifying a simple way to realize relationships " 22 | "between entities." 23 | msgstr "" 24 | -------------------------------------------------------------------------------- /locale/es/LC_MESSAGES/Structural/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Structural/README.rst:2 15 | msgid "`Structural`__" 16 | msgstr "" 17 | 18 | #: ../../Structural/README.rst:4 19 | msgid "" 20 | "In Software Engineering, Structural Design Patterns are Design Patterns that" 21 | " ease the design by identifying a simple way to realize relationships " 22 | "between entities." 23 | msgstr "" 24 | -------------------------------------------------------------------------------- /locale/pt_BR/LC_MESSAGES/Structural/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Structural/README.rst:2 15 | msgid "`Structural`__" 16 | msgstr "" 17 | 18 | #: ../../Structural/README.rst:4 19 | msgid "" 20 | "In Software Engineering, Structural Design Patterns are Design Patterns that" 21 | " ease the design by identifying a simple way to realize relationships " 22 | "between entities." 23 | msgstr "" 24 | -------------------------------------------------------------------------------- /Behavioral/Specification/Not.php: -------------------------------------------------------------------------------- 1 | spec = $spec; 20 | } 21 | 22 | /** 23 | * Returns the negated result of the wrapped specification 24 | * 25 | * @param Item $item 26 | * 27 | * @return bool 28 | */ 29 | public function isSatisfiedBy(Item $item) 30 | { 31 | return !$this->spec->isSatisfiedBy($item); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Structural/FluentInterface/uml/FluentInterface.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Structural\FluentInterface\Sql 5 | 6 | \DesignPatterns\Structural\FluentInterface\Sql 7 | 8 | 9 | 10 | 11 | 12 | \DesignPatterns\Structural\FluentInterface\Sql 13 | 14 | 15 | Fields 16 | Constants 17 | Constructors 18 | Methods 19 | 20 | private 21 | 22 | 23 | -------------------------------------------------------------------------------- /Behavioral/Mediator/Colleague.php: -------------------------------------------------------------------------------- 1 | mediator = $medium; 25 | } 26 | 27 | // for subclasses 28 | protected function getMediator() 29 | { 30 | return $this->mediator; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Structural/DependencyInjection/Tests/DependencyInjectionTest.php: -------------------------------------------------------------------------------- 1 | source = include 'config.php'; 16 | $this->config = new ArrayConfig($this->source); 17 | } 18 | 19 | public function testDependencyInjection() 20 | { 21 | $connection = new Connection($this->config); 22 | $connection->connect(); 23 | $this->assertEquals($this->source['host'], $connection->getHost()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Behavioral/NullObject/Service.php: -------------------------------------------------------------------------------- 1 | logger = $log; 23 | } 24 | 25 | /** 26 | * do something ... 27 | */ 28 | public function doSomething() 29 | { 30 | // no more check "if (!is_null($this->logger))..." with the NullObject pattern 31 | $this->logger->log('We are in ' . __METHOD__); 32 | // something to do... 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Creational/Builder/Director.php: -------------------------------------------------------------------------------- 1 | createVehicle(); 24 | $builder->addDoors(); 25 | $builder->addEngine(); 26 | $builder->addWheel(); 27 | 28 | return $builder->getVehicle(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Behavioral/Memento/Originator.php: -------------------------------------------------------------------------------- 1 | state = $state; 19 | } 20 | 21 | /** 22 | * @return Memento 23 | */ 24 | public function saveToMemento() 25 | { 26 | $state = is_object($this->state) ? clone $this->state : $this->state; 27 | 28 | return new Memento($state); 29 | } 30 | 31 | public function restoreFromMemento(Memento $memento) 32 | { 33 | $this->state = $memento->getState(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Structural/Bridge/Tests/BridgeTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString('Car Produced Assembled'); 17 | $vehicle->manufacture(); 18 | } 19 | 20 | public function testMotorcycle() 21 | { 22 | $vehicle = new Motorcycle(new Produce(), new Assemble()); 23 | $this->expectOutputString('Motorcycle Produced Assembled'); 24 | $vehicle->manufacture(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Structural/Decorator/Decorator.php: -------------------------------------------------------------------------------- 1 | wrapped = $wrappable; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Creational/Pool/uml/Pool.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Creational\Pool\Processor 5 | 6 | \DesignPatterns\Creational\Pool\Pool 7 | \DesignPatterns\Creational\Pool\Processor 8 | \DesignPatterns\Creational\Pool\Worker 9 | 10 | 11 | 12 | 13 | 14 | 15 | Fields 16 | Constants 17 | Constructors 18 | Methods 19 | 20 | private 21 | 22 | 23 | -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibilities/Request.php: -------------------------------------------------------------------------------- 1 | shipOrder(); 18 | } catch (Exception $e) { 19 | //handle error! 20 | } 21 | // response to browser 22 | } 23 | 24 | /** 25 | * @param int $id 26 | */ 27 | public function completeAction($id) 28 | { 29 | $order = OrderFactory::getOrder($id); 30 | try { 31 | $order->completeOrder(); 32 | } catch (Exception $e) { 33 | //handle error! 34 | } 35 | // response to browser 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/HtmlFactory.php: -------------------------------------------------------------------------------- 1 | \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Structural/README.rst:2 15 | msgid "`Structural`__" 16 | msgstr "`结构型设计模式`__" 17 | 18 | #: ../../Structural/README.rst:4 19 | msgid "" 20 | "In Software Engineering, Structural Design Patterns are Design Patterns that" 21 | " ease the design by identifying a simple way to realize relationships " 22 | "between entities." 23 | msgstr "" 24 | "在软件工程中,结构型设计模式集是用来抽象真实程序中的对象实体之间的关系,并使" 25 | "这种关系可被描述,概括和具体化。" -------------------------------------------------------------------------------- /Behavioral/Memento/uml/Momento.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Behavioral\Memento\Caretaker 5 | 6 | \DesignPatterns\Behavioral\Memento\Caretaker 7 | \DesignPatterns\Behavioral\Memento\Originator 8 | \DesignPatterns\Behavioral\Memento\Memento 9 | 10 | 11 | 12 | 13 | 14 | 15 | Fields 16 | Constants 17 | Constructors 18 | Methods 19 | 20 | private 21 | 22 | 23 | -------------------------------------------------------------------------------- /Creational/StaticFactory/StaticFactory.php: -------------------------------------------------------------------------------- 1 | global => evil 7 | * Note2: Cannot be subclassed or mock-upped or have multiple different instances 8 | */ 9 | class StaticFactory 10 | { 11 | /** 12 | * the parametrized function to get create an instance 13 | * 14 | * @param string $type 15 | * 16 | * @static 17 | * 18 | * @throws \InvalidArgumentException 19 | * @return FormatterInterface 20 | */ 21 | public static function factory($type) 22 | { 23 | $className = __NAMESPACE__ . '\Format' . ucfirst($type); 24 | 25 | if (!class_exists($className)) { 26 | throw new \InvalidArgumentException('Missing format class.'); 27 | } 28 | 29 | return new $className(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /locale/ca/LC_MESSAGES/Behavioral/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Behavioral/README.rst:2 15 | msgid "`Behavioral`__" 16 | msgstr "" 17 | 18 | #: ../../Behavioral/README.rst:4 19 | msgid "" 20 | "In software engineering, behavioral design patterns are design patterns that" 21 | " identify common communication patterns between objects and realize these " 22 | "patterns. By doing so, these patterns increase flexibility in carrying out " 23 | "this communication." 24 | msgstr "" 25 | -------------------------------------------------------------------------------- /locale/es/LC_MESSAGES/Behavioral/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Behavioral/README.rst:2 15 | msgid "`Behavioral`__" 16 | msgstr "" 17 | 18 | #: ../../Behavioral/README.rst:4 19 | msgid "" 20 | "In software engineering, behavioral design patterns are design patterns that" 21 | " identify common communication patterns between objects and realize these " 22 | "patterns. By doing so, these patterns increase flexibility in carrying out " 23 | "this communication." 24 | msgstr "" 25 | -------------------------------------------------------------------------------- /Structural/DependencyInjection/ArrayConfig.php: -------------------------------------------------------------------------------- 1 | storage[$key])) { 22 | return $this->storage[$key]; 23 | } 24 | return $default; 25 | } 26 | 27 | /** 28 | * Set parameter 29 | * 30 | * @param string|int $key 31 | * @param mixed $value 32 | */ 33 | public function set($key, $value) 34 | { 35 | $this->storage[$key] = $value; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /locale/pt_BR/LC_MESSAGES/Behavioral/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Behavioral/README.rst:2 15 | msgid "`Behavioral`__" 16 | msgstr "" 17 | 18 | #: ../../Behavioral/README.rst:4 19 | msgid "" 20 | "In software engineering, behavioral design patterns are design patterns that" 21 | " identify common communication patterns between objects and realize these " 22 | "patterns. By doing so, these patterns increase flexibility in carrying out " 23 | "this communication." 24 | msgstr "" 25 | -------------------------------------------------------------------------------- /locale/zh_CN/LC_MESSAGES/Behavioral/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Behavioral/README.rst:2 15 | msgid "`Behavioral`__" 16 | msgstr "" 17 | 18 | #: ../../Behavioral/README.rst:4 19 | msgid "" 20 | "In software engineering, behavioral design patterns are design patterns that" 21 | " identify common communication patterns between objects and realize these " 22 | "patterns. By doing so, these patterns increase flexibility in carrying out " 23 | "this communication." 24 | msgstr "" 25 | -------------------------------------------------------------------------------- /Structural/DataMapper/uml/DataMapper.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Structural\DataMapper\User 5 | 6 | \DesignPatterns\Structural\DataMapper\User 7 | \DesignPatterns\Structural\DataMapper\UserMapper 8 | 9 | 10 | 11 | 12 | 13 | \DesignPatterns\Structural\DataMapper\User 14 | 15 | 16 | Fields 17 | Constants 18 | Constructors 19 | Methods 20 | 21 | private 22 | 23 | 24 | -------------------------------------------------------------------------------- /Behavioral/Visitor/RoleVisitorInterface.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall); 17 | $secondCall = Singleton::getInstance(); 18 | $this->assertSame($firstCall, $secondCall); 19 | } 20 | 21 | public function testNoConstructor() 22 | { 23 | $obj = Singleton::getInstance(); 24 | 25 | $refl = new \ReflectionObject($obj); 26 | $meth = $refl->getMethod('__construct'); 27 | $this->assertTrue($meth->isPrivate()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/JsonFactory.php: -------------------------------------------------------------------------------- 1 | command = $cmd; 25 | } 26 | 27 | /** 28 | * executes the command 29 | */ 30 | public function run() 31 | { 32 | // here is a key feature of the invoker 33 | // the invoker is the same whatever is the command 34 | $this->command->execute(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Creational/Multiton/README.rst: -------------------------------------------------------------------------------- 1 | Multiton 2 | ======== 3 | 4 | **THIS IS CONSIDERED TO BE AN ANTI-PATTERN! FOR BETTER TESTABILITY AND 5 | MAINTAINABILITY USE DEPENDENCY INJECTION!** 6 | 7 | Purpose 8 | ------- 9 | 10 | To have only a list of named instances that are used, like a singleton 11 | but with n instances. 12 | 13 | Examples 14 | -------- 15 | 16 | - 2 DB Connectors, e.g. one for MySQL, the other for SQLite 17 | - multiple Loggers (one for debug messages, one for errors) 18 | 19 | UML Diagram 20 | ----------- 21 | 22 | .. image:: uml/uml.png 23 | :alt: Alt Multiton UML Diagram 24 | :align: center 25 | 26 | Code 27 | ---- 28 | 29 | You can also find these code on `GitHub`_ 30 | 31 | Multiton.php 32 | 33 | .. literalinclude:: Multiton.php 34 | :language: php 35 | :linenos: 36 | 37 | Test 38 | ---- 39 | 40 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Multiton 41 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/GermanFactory.php: -------------------------------------------------------------------------------- 1 | addTuningAMG(); 24 | 25 | return $obj; 26 | break; 27 | default: 28 | throw new \InvalidArgumentException("$type is not a valid vehicle"); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/FactoryMethod.php: -------------------------------------------------------------------------------- 1 | createVehicle($type); 35 | $obj->setColor("#f00"); 36 | 37 | return $obj; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Behavioral/Specification/SpecificationInterface.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Structural\Facade\BiosInterface 5 | 6 | \DesignPatterns\Structural\Facade\BiosInterface 7 | \DesignPatterns\Structural\Facade\OsInterface 8 | \DesignPatterns\Structural\Facade\Facade 9 | 10 | 11 | 12 | 13 | 14 | \DesignPatterns\Structural\Facade\BiosInterface 15 | 16 | 17 | Fields 18 | Constants 19 | Constructors 20 | Methods 21 | 22 | private 23 | 24 | 25 | -------------------------------------------------------------------------------- /Behavioral/Specification/Plus.php: -------------------------------------------------------------------------------- 1 | left = $left; 22 | $this->right = $right; 23 | } 24 | 25 | /** 26 | * Checks if the composite AND of specifications passes 27 | * 28 | * @param Item $item 29 | * 30 | * @return bool 31 | */ 32 | public function isSatisfiedBy(Item $item) 33 | { 34 | return $this->left->isSatisfiedBy($item) && $this->right->isSatisfiedBy($item); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Behavioral/Command/Tests/CommandTest.php: -------------------------------------------------------------------------------- 1 | invoker = new Invoker(); 28 | $this->receiver = new Receiver(); 29 | } 30 | 31 | public function testInvocation() 32 | { 33 | $this->invoker->setCommand(new HelloCommand($this->receiver)); 34 | $this->expectOutputString('Hello World'); 35 | $this->invoker->run(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Creational/StaticFactory/Tests/StaticFactoryTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('DesignPatterns\Creational\StaticFactory\FormatterInterface', $obj); 29 | } 30 | 31 | /** 32 | * @expectedException InvalidArgumentException 33 | */ 34 | public function testException() 35 | { 36 | StaticFactory::factory(""); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Behavioral/Specification/Either.php: -------------------------------------------------------------------------------- 1 | left = $left; 22 | $this->right = $right; 23 | } 24 | 25 | /** 26 | * Returns the evaluation of both wrapped specifications as a logical OR 27 | * 28 | * @param Item $item 29 | * 30 | * @return bool 31 | */ 32 | public function isSatisfiedBy(Item $item) 33 | { 34 | return $this->left->isSatisfiedBy($item) || $this->right->isSatisfiedBy($item); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Structural/Proxy/README.rst: -------------------------------------------------------------------------------- 1 | `Proxy`__ 2 | ========= 3 | 4 | Purpose 5 | ------- 6 | 7 | To interface to anything that is expensive or impossible to duplicate. 8 | 9 | Examples 10 | -------- 11 | 12 | - Doctrine2 uses proxies to implement framework magic (e.g. lazy 13 | initialization) in them, while the user still works with his own 14 | entity classes and will never use nor touch the proxies 15 | 16 | UML Diagram 17 | ----------- 18 | 19 | .. image:: uml/uml.png 20 | :alt: Alt Proxy UML Diagram 21 | :align: center 22 | 23 | Code 24 | ---- 25 | 26 | You can also find these code on `GitHub`_ 27 | 28 | Record.php 29 | 30 | .. literalinclude:: Record.php 31 | :language: php 32 | :linenos: 33 | 34 | RecordProxy.php 35 | 36 | .. literalinclude:: RecordProxy.php 37 | :language: php 38 | :linenos: 39 | 40 | Test 41 | ---- 42 | 43 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/Proxy 44 | .. __: http://en.wikipedia.org/wiki/Proxy_pattern 45 | -------------------------------------------------------------------------------- /Behavioral/Command/HelloCommand.php: -------------------------------------------------------------------------------- 1 | output = $console; 25 | } 26 | 27 | /** 28 | * execute and output "Hello World" 29 | */ 30 | public function execute() 31 | { 32 | // sometimes, there is no receiver and this is the command which 33 | // does all the work 34 | $this->output->write('Hello World'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Structural/Registry/Registry.php: -------------------------------------------------------------------------------- 1 | \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Creational/README.rst:2 15 | msgid "`Creational`__" 16 | msgstr "" 17 | 18 | #: ../../Creational/README.rst:4 19 | msgid "" 20 | "In software engineering, creational design patterns are design patterns that" 21 | " deal with object creation mechanisms, trying to create objects in a manner " 22 | "suitable to the situation. The basic form of object creation could result in" 23 | " design problems or added complexity to the design. Creational design " 24 | "patterns solve this problem by somehow controlling this object creation." 25 | msgstr "" 26 | -------------------------------------------------------------------------------- /locale/es/LC_MESSAGES/Creational/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Creational/README.rst:2 15 | msgid "`Creational`__" 16 | msgstr "" 17 | 18 | #: ../../Creational/README.rst:4 19 | msgid "" 20 | "In software engineering, creational design patterns are design patterns that" 21 | " deal with object creation mechanisms, trying to create objects in a manner " 22 | "suitable to the situation. The basic form of object creation could result in" 23 | " design problems or added complexity to the design. Creational design " 24 | "patterns solve this problem by somehow controlling this object creation." 25 | msgstr "" 26 | -------------------------------------------------------------------------------- /Behavioral/Observer/uml/Observer.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Behavioral\Observer\User 5 | 6 | \DesignPatterns\Behavioral\Observer\UserObserver 7 | \DesignPatterns\Behavioral\Observer\User 8 | \SplSubject 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | Fields 21 | Constants 22 | Constructors 23 | Methods 24 | 25 | private 26 | 27 | 28 | -------------------------------------------------------------------------------- /Behavioral/Iterator/BookList.php: -------------------------------------------------------------------------------- 1 | count()) { 13 | return $this->books[$bookNumberToGet]; 14 | } 15 | 16 | return null; 17 | } 18 | 19 | public function addBook(Book $book) 20 | { 21 | $this->books[] = $book; 22 | 23 | return $this->count(); 24 | } 25 | 26 | public function removeBook(Book $bookToRemove) 27 | { 28 | foreach ($this->books as $key => $book) { 29 | /** @var Book $book */ 30 | if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) { 31 | unset($this->books[$key]); 32 | } 33 | } 34 | 35 | return $this->count(); 36 | } 37 | 38 | public function count() 39 | { 40 | return count($this->books); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Dominik Liebler 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Structural/Proxy/Record.php: -------------------------------------------------------------------------------- 1 | data = (array) $data; 21 | } 22 | 23 | /** 24 | * magic setter 25 | * 26 | * @param string $name 27 | * @param mixed $value 28 | * 29 | * @return void 30 | */ 31 | public function __set($name, $value) 32 | { 33 | $this->data[(string) $name] = $value; 34 | } 35 | 36 | /** 37 | * magic getter 38 | * 39 | * @param string $name 40 | * 41 | * @return mixed|null 42 | */ 43 | public function __get($name) 44 | { 45 | if (array_key_exists($name, $this->data)) { 46 | return $this->data[(string) $name]; 47 | } else { 48 | return null; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Behavioral/Memento/Caretaker.php: -------------------------------------------------------------------------------- 1 | setState("State1"); 17 | //Setting state to State2 18 | $originator->setState("State2"); 19 | //Saving State2 to Memento 20 | $savedStates[] = $originator->saveToMemento(); 21 | //Setting state to State3 22 | $originator->setState("State3"); 23 | 24 | // We can request multiple mementos, and choose which one to roll back to. 25 | // Saving State3 to Memento 26 | $savedStates[] = $originator->saveToMemento(); 27 | //Setting state to State4 28 | $originator->setState("State4"); 29 | 30 | $originator->restoreFromMemento($savedStates[1]); 31 | //State after restoring from Memento: State3 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Behavioral/NullObject/Tests/LoggerTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString(null); // no output 21 | $service->doSomething(); 22 | } 23 | 24 | public function testStandardLogger() 25 | { 26 | $service = new Service(new PrintLogger()); 27 | $this->expectOutputString('We are in DesignPatterns\Behavioral\NullObject\Service::doSomething'); 28 | $service->doSomething(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Structural/DependencyInjection/Connection.php: -------------------------------------------------------------------------------- 1 | configuration = $config; 26 | } 27 | 28 | /** 29 | * connection using the injected config 30 | */ 31 | public function connect() 32 | { 33 | $host = $this->configuration->get('host'); 34 | // connection to host, authentication etc... 35 | 36 | //if connected 37 | $this->host = $host; 38 | } 39 | 40 | /* 41 | * Get currently connected host 42 | * 43 | * @return string 44 | */ 45 | public function getHost() 46 | { 47 | return $this->host; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Structural/Proxy/uml/Proxy.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Structural\Proxy\Record 5 | 6 | \DesignPatterns\Structural\Proxy\Record 7 | \DesignPatterns\Structural\Proxy\RecordProxy 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | \DesignPatterns\Structural\Proxy\Record 19 | 20 | 21 | Fields 22 | Constants 23 | Constructors 24 | Methods 25 | 26 | private 27 | 28 | 29 | -------------------------------------------------------------------------------- /locale/ru/LC_MESSAGES/Structural/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: 2015-05-30 23:27+0300\n" 8 | "Last-Translator: Eugene Glotov \n" 9 | "MIME-Version: 1.0\n" 10 | "Content-Type: text/plain; charset=UTF-8\n" 11 | "Content-Transfer-Encoding: 8bit\n" 12 | "Language: ru\n" 13 | 14 | #: ../../Structural/README.rst:2 15 | msgid "`Structural`__" 16 | msgstr "" 17 | "`Структурные шаблоны проектирования `_ (`Structural`__)" 19 | 20 | #: ../../Structural/README.rst:4 21 | msgid "" 22 | "In Software Engineering, Structural Design Patterns are Design Patterns that" 23 | " ease the design by identifying a simple way to realize relationships " 24 | "between entities." 25 | msgstr "" 26 | "При разработке программного обеспечения, Структурные шаблоны проектирования " 27 | "упрощают проектирование путем выявления простого способа реализовать " 28 | "отношения между субъектами." 29 | -------------------------------------------------------------------------------- /Structural/FluentInterface/README.rst: -------------------------------------------------------------------------------- 1 | `Fluent Interface`__ 2 | ==================== 3 | 4 | Purpose 5 | ------- 6 | 7 | To write code that is easy readable just like sentences in a natural 8 | language (like English). 9 | 10 | Examples 11 | -------- 12 | 13 | - Doctrine2's QueryBuilder works something like that example class 14 | below 15 | - PHPUnit uses fluent interfaces to build mock objects 16 | - Yii Framework: CDbCommand and CActiveRecord use this pattern, too 17 | 18 | UML Diagram 19 | ----------- 20 | 21 | .. image:: uml/uml.png 22 | :alt: Alt FluentInterface UML Diagram 23 | :align: center 24 | 25 | Code 26 | ---- 27 | 28 | You can also find these code on `GitHub`_ 29 | 30 | Sql.php 31 | 32 | .. literalinclude:: Sql.php 33 | :language: php 34 | :linenos: 35 | 36 | Test 37 | ---- 38 | 39 | Tests/FluentInterfaceTest.php 40 | 41 | .. literalinclude:: Tests/FluentInterfaceTest.php 42 | :language: php 43 | :linenos: 44 | 45 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/FluentInterface 46 | .. __: http://en.wikipedia.org/wiki/Fluent_interface -------------------------------------------------------------------------------- /Structural/README.md: -------------------------------------------------------------------------------- 1 | # Structural 2 | 3 | In Software Engineering, Structural Design Patterns are Design Patterns that 4 | ease the design by identifying a simple way to realize relationships between 5 | entities. 6 | 7 | * [Adapter](Adapter) [:notebook:](http://en.wikipedia.org/wiki/Adapter_pattern) 8 | * [Bridge](Bridge) [:notebook:](http://en.wikipedia.org/wiki/Bridge_pattern) 9 | * [Composite](Composite) [:notebook:](http://en.wikipedia.org/wiki/Composite_pattern) 10 | * [DataMapper](DataMapper) [:notebook:](http://en.wikipedia.org/wiki/Data_mapper_pattern) 11 | * [Decorator](Decorator) [:notebook:](http://en.wikipedia.org/wiki/Decorator_pattern) 12 | * [DependencyInjection](DependencyInjection) [:notebook:](http://en.wikipedia.org/wiki/Dependency_injection) 13 | * [Facade](Facade) [:notebook:](http://en.wikipedia.org/wiki/Facade_pattern) 14 | * [FluentInterface](FluentInterface) [:notebook:](http://en.wikipedia.org/wiki/Fluent_interface) 15 | * [Proxy](Proxy) [:notebook:](http://en.wikipedia.org/wiki/Proxy_pattern) 16 | * [Registry](Registry) [:notebook:](http://en.wikipedia.org/wiki/Service_locator_pattern) 17 | -------------------------------------------------------------------------------- /More/Repository/Storage.php: -------------------------------------------------------------------------------- 1 | data = array(); 18 | $this->lastId = 0; 19 | } 20 | 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function persist($data) 25 | { 26 | $this->data[++$this->lastId] = $data; 27 | return $this->lastId; 28 | } 29 | 30 | /** 31 | * {@inheritdoc} 32 | */ 33 | public function retrieve($id) 34 | { 35 | return isset($this->data[$id]) ? $this->data[$id] : null; 36 | } 37 | 38 | /** 39 | * {@inheritdoc} 40 | */ 41 | public function delete($id) 42 | { 43 | if (!isset($this->data[$id])) { 44 | return false; 45 | } 46 | 47 | $this->data[$id] = null; 48 | unset($this->data[$id]); 49 | 50 | return true; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Structural/Adapter/EBookAdapter.php: -------------------------------------------------------------------------------- 1 | eBook = $ebook; 26 | } 27 | 28 | /** 29 | * This class makes the proper translation from one interface to another 30 | */ 31 | public function open() 32 | { 33 | $this->eBook->pressStart(); 34 | } 35 | 36 | /** 37 | * turns pages 38 | */ 39 | public function turnPage() 40 | { 41 | $this->eBook->pressNext(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Creational/Builder/BikeBuilder.php: -------------------------------------------------------------------------------- 1 | bike->setPart('engine', new Parts\Engine()); 28 | } 29 | 30 | /** 31 | * {@inheritdoc} 32 | */ 33 | public function addWheel() 34 | { 35 | $this->bike->setPart('forwardWheel', new Parts\Wheel()); 36 | $this->bike->setPart('rearWheel', new Parts\Wheel()); 37 | } 38 | 39 | /** 40 | * {@inheritdoc} 41 | */ 42 | public function createVehicle() 43 | { 44 | $this->bike = new Parts\Bike(); 45 | } 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | public function getVehicle() 51 | { 52 | return $this->bike; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Structural/Facade/Facade.php: -------------------------------------------------------------------------------- 1 | bios = $bios; 31 | $this->os = $os; 32 | } 33 | 34 | /** 35 | * turn on the system 36 | */ 37 | public function turnOn() 38 | { 39 | $this->bios->execute(); 40 | $this->bios->waitForKeyPress(); 41 | $this->bios->launch($this->os); 42 | } 43 | 44 | /** 45 | * turn off the system 46 | */ 47 | public function turnOff() 48 | { 49 | $this->os->halt(); 50 | $this->bios->powerDown(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Creational/SimpleFactory/Tests/SimpleFactoryTest.php: -------------------------------------------------------------------------------- 1 | factory = new ConcreteFactory(); 18 | } 19 | 20 | public function getType() 21 | { 22 | return array( 23 | array('bicycle'), 24 | array('other') 25 | ); 26 | } 27 | 28 | /** 29 | * @dataProvider getType 30 | */ 31 | public function testCreation($type) 32 | { 33 | $obj = $this->factory->createVehicle($type); 34 | $this->assertInstanceOf('DesignPatterns\Creational\SimpleFactory\VehicleInterface', $obj); 35 | } 36 | 37 | /** 38 | * @expectedException \InvalidArgumentException 39 | */ 40 | public function testBadType() 41 | { 42 | $this->factory->createVehicle('car'); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /locale/zh_CN/LC_MESSAGES/Creational/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Creational/README.rst:2 15 | msgid "`Creational`__" 16 | msgstr "`创建型设计模式`__" 17 | 18 | #: ../../Creational/README.rst:4 19 | msgid "" 20 | "In software engineering, creational design patterns are design patterns that" 21 | " deal with object creation mechanisms, trying to create objects in a manner " 22 | "suitable to the situation. The basic form of object creation could result in" 23 | " design problems or added complexity to the design. Creational design " 24 | "patterns solve this problem by somehow controlling this object creation." 25 | msgstr "" 26 | "在软件工程中,创建型设计模式承担着对象创建的职责,尝试创建" 27 | "适合程序上下文的对象,对象创建设计模式的产生是由于软件工程" 28 | "设计的问题,具体说是向设计中增加复杂度,创建型设计模式解决" 29 | "了程序设计中对象创建的问题。" 30 | -------------------------------------------------------------------------------- /Creational/Singleton/Singleton.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Repository\PostRepository 5 | 6 | \DesignPatterns\Repository\Storage 7 | \DesignPatterns\Repository\MemoryStorage 8 | \DesignPatterns\Repository\Post 9 | \DesignPatterns\Repository\PostRepository 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | Fields 22 | Constants 23 | Constructors 24 | Methods 25 | 26 | private 27 | 28 | 29 | -------------------------------------------------------------------------------- /Creational/README.md: -------------------------------------------------------------------------------- 1 | # Creational 2 | 3 | In software engineering, creational design patterns are design patterns that 4 | deal with object creation mechanisms, trying to create objects in a manner 5 | suitable to the situation. The basic form of object creation could result in 6 | design problems or added complexity to the design. Creational design patterns 7 | solve this problem by somehow controlling this object creation. 8 | 9 | * [AbstractFactory](AbstractFactory) [:notebook:](http://en.wikipedia.org/wiki/Abstract_factory_pattern) 10 | * [Builder](Builder) [:notebook:](http://en.wikipedia.org/wiki/Builder_pattern) 11 | * [FactoryMethod](FactoryMethod) [:notebook:](http://en.wikipedia.org/wiki/Factory_method_pattern) 12 | * [Multiton](Multiton) (is considered an anti-pattern! :no_entry:) 13 | * [Pool](Pool) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern) 14 | * [Prototype](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern) 15 | * [SimpleFactory](SimpleFactory) 16 | * [Singleton](Singleton) [:notebook:](http://en.wikipedia.org/wiki/Singleton_pattern) (is considered an anti-pattern! :no_entry:) 17 | * [StaticFactory](StaticFactory) 18 | -------------------------------------------------------------------------------- /Structural/Composite/Form.php: -------------------------------------------------------------------------------- 1 | elements as $element) { 31 | $formCode .= $element->render($indent + 1) . PHP_EOL; 32 | } 33 | 34 | return $formCode; 35 | } 36 | 37 | /** 38 | * @param FormElement $element 39 | */ 40 | public function addElement(FormElement $element) 41 | { 42 | $this->elements[] = $element; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Behavioral/Strategy/ObjectCollection.php: -------------------------------------------------------------------------------- 1 | elements = $elements; 26 | } 27 | 28 | /** 29 | * @return array 30 | */ 31 | public function sort() 32 | { 33 | if (!$this->comparator) { 34 | throw new \LogicException("Comparator is not set"); 35 | } 36 | 37 | $callback = array($this->comparator, 'compare'); 38 | uasort($this->elements, $callback); 39 | 40 | return $this->elements; 41 | } 42 | 43 | /** 44 | * @param ComparatorInterface $comparator 45 | * 46 | * @return void 47 | */ 48 | public function setComparator(ComparatorInterface $comparator) 49 | { 50 | $this->comparator = $comparator; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Creational/SimpleFactory/ConcreteFactory.php: -------------------------------------------------------------------------------- 1 | typeList = array( 22 | 'bicycle' => __NAMESPACE__ . '\Bicycle', 23 | 'other' => __NAMESPACE__ . '\Scooter' 24 | ); 25 | } 26 | 27 | /** 28 | * Creates a vehicle 29 | * 30 | * @param string $type a known type key 31 | * 32 | * @return VehicleInterface a new instance of VehicleInterface 33 | * @throws \InvalidArgumentException 34 | */ 35 | public function createVehicle($type) 36 | { 37 | if (!array_key_exists($type, $this->typeList)) { 38 | throw new \InvalidArgumentException("$type is not valid vehicle"); 39 | } 40 | $className = $this->typeList[$type]; 41 | 42 | return new $className(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Creational/Singleton/README.rst: -------------------------------------------------------------------------------- 1 | `Singleton`__ 2 | ============= 3 | 4 | **THIS IS CONSIDERED TO BE AN ANTI-PATTERN! FOR BETTER TESTABILITY AND 5 | MAINTAINABILITY USE DEPENDENCY INJECTION!** 6 | 7 | Purpose 8 | ------- 9 | 10 | To have only one instance of this object in the application that will 11 | handle all calls. 12 | 13 | Examples 14 | -------- 15 | 16 | - DB Connector 17 | - Logger (may also be a Multiton if there are many log files for 18 | several purposes) 19 | - Lock file for the application (there is only one in the filesystem 20 | ...) 21 | 22 | UML Diagram 23 | ----------- 24 | 25 | .. image:: uml/uml.png 26 | :alt: Alt Singleton UML Diagram 27 | :align: center 28 | 29 | Code 30 | ---- 31 | 32 | You can also find these code on `GitHub`_ 33 | 34 | Singleton.php 35 | 36 | .. literalinclude:: Singleton.php 37 | :language: php 38 | :linenos: 39 | 40 | Test 41 | ---- 42 | 43 | Tests/SingletonTest.php 44 | 45 | .. literalinclude:: Tests/SingletonTest.php 46 | :language: php 47 | :linenos: 48 | 49 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Singleton 50 | .. __: http://en.wikipedia.org/wiki/Singleton_pattern 51 | -------------------------------------------------------------------------------- /Behavioral/Mediator/Tests/MediatorTest.php: -------------------------------------------------------------------------------- 1 | client = new Client($media); 22 | $media->setColleague(new Database($media), $this->client, new Server($media)); 23 | } 24 | 25 | public function testOutputHelloWorld() 26 | { 27 | // testing if Hello World is output : 28 | $this->expectOutputString('Hello World'); 29 | // as you see, the 3 components Client, Server and Database are totally decoupled 30 | $this->client->request(); 31 | // Anyway, it remains complexity in the Mediator that's why the pattern 32 | // Observer is preferable in mnay situations. 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Structural/Registry/README.rst: -------------------------------------------------------------------------------- 1 | `Registry`__ 2 | ============ 3 | 4 | Purpose 5 | ------- 6 | 7 | To implement a central storage for objects often used throughout the 8 | application, is typically implemented using an abstract class with only 9 | static methods (or using the Singleton pattern) 10 | 11 | Examples 12 | -------- 13 | 14 | - Zend Framework 1: ``Zend_Registry`` holds the application's logger 15 | object, front controller etc. 16 | - Yii Framework: ``CWebApplication`` holds all the application 17 | components, such as ``CWebUser``, ``CUrlManager``, etc. 18 | 19 | UML Diagram 20 | ----------- 21 | 22 | .. image:: uml/uml.png 23 | :alt: Alt Registry UML Diagram 24 | :align: center 25 | 26 | Code 27 | ---- 28 | 29 | You can also find these code on `GitHub`_ 30 | 31 | Registry.php 32 | 33 | .. literalinclude:: Registry.php 34 | :language: php 35 | :linenos: 36 | 37 | Test 38 | ---- 39 | 40 | Tests/RegistryTest.php 41 | 42 | .. literalinclude:: Tests/RegistryTest.php 43 | :language: php 44 | :linenos: 45 | 46 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/Registry 47 | .. __: http://en.wikipedia.org/wiki/Service_locator_pattern 48 | -------------------------------------------------------------------------------- /Creational/Prototype/README.rst: -------------------------------------------------------------------------------- 1 | `Prototype`__ 2 | ============= 3 | 4 | Purpose 5 | ------- 6 | 7 | To avoid the cost of creating objects the standard way (new Foo()) and 8 | instead create a prototype and clone it. 9 | 10 | Examples 11 | -------- 12 | 13 | - Large amounts of data (e.g. create 1,000,000 rows in a database at 14 | once via a ORM). 15 | 16 | UML Diagram 17 | ----------- 18 | 19 | .. image:: uml/uml.png 20 | :alt: Alt Prototype UML Diagram 21 | :align: center 22 | 23 | Code 24 | ---- 25 | 26 | You can also find these code on `GitHub`_ 27 | 28 | index.php 29 | 30 | .. literalinclude:: index.php 31 | :language: php 32 | :linenos: 33 | 34 | BookPrototype.php 35 | 36 | .. literalinclude:: BookPrototype.php 37 | :language: php 38 | :linenos: 39 | 40 | BarBookPrototype.php 41 | 42 | .. literalinclude:: BarBookPrototype.php 43 | :language: php 44 | :linenos: 45 | 46 | FooBookPrototype.php 47 | 48 | .. literalinclude:: FooBookPrototype.php 49 | :language: php 50 | :linenos: 51 | 52 | Test 53 | ---- 54 | 55 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Prototype 56 | .. __: http://en.wikipedia.org/wiki/Prototype_pattern 57 | -------------------------------------------------------------------------------- /Behavioral/Command/uml/Command.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Behavioral\Command\HelloCommand 5 | 6 | \DesignPatterns\Behavioral\Command\Invoker 7 | \DesignPatterns\Behavioral\Command\HelloCommand 8 | \DesignPatterns\Behavioral\Command\Receiver 9 | \DesignPatterns\Behavioral\Command\CommandInterface 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | Fields 22 | Constants 23 | Constructors 24 | Methods 25 | 26 | private 27 | 28 | 29 | -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibilities/Responsible/FastStorage.php: -------------------------------------------------------------------------------- 1 | data = $data; 24 | } 25 | 26 | protected function processing(Request $req) 27 | { 28 | if ('get' === $req->verb) { 29 | if (array_key_exists($req->key, $this->data)) { 30 | // the handler IS responsible and then processes the request 31 | $req->response = $this->data[$req->key]; 32 | // instead of returning true, I could return the value but it proves 33 | // to be a bad idea. What if the value IS "false" ? 34 | return true; 35 | } 36 | } 37 | 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Behavioral/Visitor/Role.php: -------------------------------------------------------------------------------- 1 | isInitialized = true; 33 | $this->isDirty = true; 34 | } 35 | } 36 | 37 | /** 38 | * magic setter 39 | * 40 | * @param string $name 41 | * @param mixed $value 42 | * 43 | * @return void 44 | */ 45 | public function __set($name, $value) 46 | { 47 | $this->isDirty = true; 48 | parent::__set($name, $value); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Behavioral/State/CreateOrder.php: -------------------------------------------------------------------------------- 1 | order = $order; 26 | } 27 | 28 | /** 29 | * @return mixed 30 | */ 31 | public function shipOrder() 32 | { 33 | $this->order['status'] = 'shipping'; 34 | $this->order['updatedTime'] = time(); 35 | 36 | // Setting the new order status into database; 37 | return $this->updateOrder($this->order); 38 | } 39 | 40 | /** 41 | * @return mixed|void 42 | * @throws \Exception 43 | */ 44 | public function completeOrder() 45 | { 46 | //Can not complete the order which status is created, throw exception; 47 | throw new \Exception('Can not complete the order which status is created!'); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Behavioral/State/ShippingOrder.php: -------------------------------------------------------------------------------- 1 | order = $order; 26 | } 27 | 28 | /** 29 | * @return mixed|void 30 | * @throws \Exception 31 | */ 32 | public function shipOrder() 33 | { 34 | //Can not ship the order which status is shipping, throw exception; 35 | throw new \Exception('Can not ship the order which status is shipping!'); 36 | } 37 | 38 | /** 39 | * @return mixed 40 | */ 41 | public function completeOrder() 42 | { 43 | $this->order['status'] = 'completed'; 44 | $this->order['updatedTime'] = time(); 45 | 46 | // Setting the new order status into database; 47 | return $this->updateOrder($this->order); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Creational/Builder/Tests/DirectorTest.php: -------------------------------------------------------------------------------- 1 | director = new Director(); 21 | } 22 | 23 | public function getBuilder() 24 | { 25 | return array( 26 | array(new CarBuilder()), 27 | array(new BikeBuilder()) 28 | ); 29 | } 30 | 31 | /** 32 | * Here we test the build process. Notice that the client don't know 33 | * anything about the concrete builder. 34 | * 35 | * @dataProvider getBuilder 36 | */ 37 | public function testBuild(BuilderInterface $builder) 38 | { 39 | $newVehicle = $this->director->build($builder); 40 | $this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Vehicle', $newVehicle); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Behavioral/Iterator/uml/Iterator.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Behavioral\Iterator\Book 5 | 6 | \DesignPatterns\Behavioral\Iterator\BookListReverseIterator 7 | \DesignPatterns\Behavioral\Iterator\BookList 8 | \DesignPatterns\Behavioral\Iterator\BookListIterator 9 | \DesignPatterns\Behavioral\Iterator\Book 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | Fields 22 | Constants 23 | Constructors 24 | Methods 25 | 26 | private 27 | 28 | 29 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/AbstractFactory.php: -------------------------------------------------------------------------------- 1 | \n" 9 | "MIME-Version: 1.0\n" 10 | "Content-Type: text/plain; charset=UTF-8\n" 11 | "Content-Transfer-Encoding: 8bit\n" 12 | "Language: ru\n" 13 | 14 | #: ../../Behavioral/README.rst:2 15 | msgid "`Behavioral`__" 16 | msgstr "`Поведенческие шаблоны проектирования `_ (`Behavioral`__)" 17 | 18 | #: ../../Behavioral/README.rst:4 19 | msgid "" 20 | "In software engineering, behavioral design patterns are design patterns that" 21 | " identify common communication patterns between objects and realize these " 22 | "patterns. By doing so, these patterns increase flexibility in carrying out " 23 | "this communication." 24 | msgstr "" 25 | "Поведенческие шаблоны проектирования определяют общие закономерности связей " 26 | "между объектами, реализующими данные паттерны. Следование этим шаблонам " 27 | "уменьшает связность системы и облегчает коммуникацию между объектами, что " 28 | "улучшает гибкость программного продукта." 29 | -------------------------------------------------------------------------------- /Behavioral/Visitor/Tests/VisitorTest.php: -------------------------------------------------------------------------------- 1 | visitor = new Visitor\RolePrintVisitor(); 18 | } 19 | 20 | public function getRole() 21 | { 22 | return array( 23 | array(new Visitor\User("Dominik"), 'Role: User Dominik'), 24 | array(new Visitor\Group("Administrators"), 'Role: Group: Administrators') 25 | ); 26 | } 27 | 28 | /** 29 | * @dataProvider getRole 30 | */ 31 | public function testVisitSomeRole(Visitor\Role $role, $expect) 32 | { 33 | $this->expectOutputString($expect); 34 | $role->accept($this->visitor); 35 | } 36 | 37 | /** 38 | * @expectedException \InvalidArgumentException 39 | * @expectedExceptionMessage Mock 40 | */ 41 | public function testUnknownObject() 42 | { 43 | $mock = $this->getMockForAbstractClass('DesignPatterns\Behavioral\Visitor\Role'); 44 | $mock->accept($this->visitor); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Structural/Adapter/Tests/AdapterTest.php: -------------------------------------------------------------------------------- 1 | open(); 39 | $book->turnPage(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Behavioral/Specification/PriceSpecification.php: -------------------------------------------------------------------------------- 1 | maxPrice = $maxPrice; 20 | } 21 | 22 | /** 23 | * Sets the optional minimum price 24 | * 25 | * @param int $minPrice 26 | */ 27 | public function setMinPrice($minPrice) 28 | { 29 | $this->minPrice = $minPrice; 30 | } 31 | 32 | /** 33 | * Checks if Item price falls between bounds 34 | * 35 | * @param Item $item 36 | * 37 | * @return bool 38 | */ 39 | public function isSatisfiedBy(Item $item) 40 | { 41 | if (!empty($this->maxPrice) && $item->getPrice() > $this->maxPrice) { 42 | return false; 43 | } 44 | if (!empty($this->minPrice) && $item->getPrice() < $this->minPrice) { 45 | return false; 46 | } 47 | 48 | return true; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Structural/Composite/Tests/CompositeTest.php: -------------------------------------------------------------------------------- 1 | addElement(new Composite\TextElement()); 17 | $form->addElement(new Composite\InputElement()); 18 | $embed = new Composite\Form(); 19 | $embed->addElement(new Composite\TextElement()); 20 | $embed->addElement(new Composite\InputElement()); 21 | $form->addElement($embed); // here we have a embedded form (like SF2 does) 22 | 23 | $this->assertRegExp('#^\s{4}#m', $form->render()); 24 | } 25 | 26 | /** 27 | * The all point of this pattern, a Composite must inherit from the node 28 | * if you want to builld trees 29 | */ 30 | public function testFormImplementsFormEelement() 31 | { 32 | $className = 'DesignPatterns\Structural\Composite\Form'; 33 | $abstractName = 'DesignPatterns\Structural\Composite\FormElement'; 34 | $this->assertTrue(is_subclass_of($className, $abstractName)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Creational/SimpleFactory/README.rst: -------------------------------------------------------------------------------- 1 | Simple Factory 2 | ============== 3 | 4 | Purpose 5 | ------- 6 | 7 | ConcreteFactory is a simple factory pattern. 8 | 9 | It differs from the static factory because it is NOT static and as you 10 | know: static => global => evil! 11 | 12 | Therefore, you can have multiple factories, differently parametrized, 13 | you can subclass it and you can mock-up it. 14 | 15 | UML Diagram 16 | ----------- 17 | 18 | .. image:: uml/uml.png 19 | :alt: Alt SimpleFactory UML Diagram 20 | :align: center 21 | 22 | Code 23 | ---- 24 | 25 | You can also find these code on `GitHub`_ 26 | 27 | ConcreteFactory.php 28 | 29 | .. literalinclude:: ConcreteFactory.php 30 | :language: php 31 | :linenos: 32 | 33 | VehicleInterface.php 34 | 35 | .. literalinclude:: VehicleInterface.php 36 | :language: php 37 | :linenos: 38 | 39 | Bicycle.php 40 | 41 | .. literalinclude:: Bicycle.php 42 | :language: php 43 | :linenos: 44 | 45 | Scooter.php 46 | 47 | .. literalinclude:: Scooter.php 48 | :language: php 49 | :linenos: 50 | 51 | Test 52 | ---- 53 | 54 | Tests/SimpleFactoryTest.php 55 | 56 | .. literalinclude:: Tests/SimpleFactoryTest.php 57 | :language: php 58 | :linenos: 59 | 60 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/SimpleFactory 61 | -------------------------------------------------------------------------------- /Behavioral/Observer/README.rst: -------------------------------------------------------------------------------- 1 | `Observer`__ 2 | ============ 3 | 4 | Purpose 5 | ------- 6 | 7 | To implement a publish/subscribe behaviour to an object, whenever a 8 | "Subject" object changes it's state, the attached "Observers" will be 9 | notified. It is used to shorten the amount of coupled objects and uses 10 | loose coupling instead. 11 | 12 | Examples 13 | -------- 14 | 15 | - a message queue system is observed to show the progress of a job in a 16 | GUI 17 | 18 | Note 19 | ---- 20 | 21 | PHP already defines two interfaces that can help to implement this 22 | pattern: SplObserver and SplSubject. 23 | 24 | UML Diagram 25 | ----------- 26 | 27 | .. image:: uml/uml.png 28 | :alt: Alt Observer UML Diagram 29 | :align: center 30 | 31 | Code 32 | ---- 33 | 34 | You can also find these code on `GitHub`_ 35 | 36 | User.php 37 | 38 | .. literalinclude:: User.php 39 | :language: php 40 | :linenos: 41 | 42 | UserObserver.php 43 | 44 | .. literalinclude:: UserObserver.php 45 | :language: php 46 | :linenos: 47 | 48 | Test 49 | ---- 50 | 51 | Tests/ObserverTest.php 52 | 53 | .. literalinclude:: Tests/ObserverTest.php 54 | :language: php 55 | :linenos: 56 | 57 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Observer 58 | .. __: http://en.wikipedia.org/wiki/Observer_pattern -------------------------------------------------------------------------------- /Creational/Pool/Processor.php: -------------------------------------------------------------------------------- 1 | pool = $pool; 16 | } 17 | 18 | public function process($image) 19 | { 20 | if ($this->processing++ < $this->maxProcesses) { 21 | $this->createWorker($image); 22 | } else { 23 | $this->pushToWaitingQueue($image); 24 | } 25 | } 26 | 27 | private function createWorker($image) 28 | { 29 | $worker = $this->pool->get(); 30 | $worker->run($image, array($this, 'processDone')); 31 | } 32 | 33 | public function processDone($worker) 34 | { 35 | $this->processing--; 36 | $this->pool->dispose($worker); 37 | 38 | if (count($this->waitingQueue) > 0) { 39 | $this->createWorker($this->popFromWaitingQueue()); 40 | } 41 | } 42 | 43 | private function pushToWaitingQueue($image) 44 | { 45 | $this->waitingQueue[] = $image; 46 | } 47 | 48 | private function popFromWaitingQueue() 49 | { 50 | return array_pop($this->waitingQueue); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/Tests/JourneyTest.php: -------------------------------------------------------------------------------- 1 | expectOutputRegex('#sun-bathing#'); 17 | $journey->takeATrip(); 18 | } 19 | 20 | public function testCity() 21 | { 22 | $journey = new TemplateMethod\CityJourney(); 23 | $this->expectOutputRegex('#drink#'); 24 | $journey->takeATrip(); 25 | } 26 | 27 | /** 28 | * How to test an abstract template method with PHPUnit 29 | */ 30 | public function testLasVegas() 31 | { 32 | $journey = $this->getMockForAbstractClass('DesignPatterns\Behavioral\TemplateMethod\Journey'); 33 | $journey->expects($this->once()) 34 | ->method('enjoyVacation') 35 | ->will($this->returnCallback(array($this, 'mockUpVacation'))); 36 | $this->expectOutputRegex('#Las Vegas#'); 37 | $journey->takeATrip(); 38 | } 39 | 40 | public function mockUpVacation() 41 | { 42 | echo "Fear and loathing in Las Vegas\n"; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Creational/Builder/CarBuilder.php: -------------------------------------------------------------------------------- 1 | car->setPart('rightdoor', new Parts\Door()); 21 | $this->car->setPart('leftDoor', new Parts\Door()); 22 | } 23 | 24 | /** 25 | * @return void 26 | */ 27 | public function addEngine() 28 | { 29 | $this->car->setPart('engine', new Parts\Engine()); 30 | } 31 | 32 | /** 33 | * @return void 34 | */ 35 | public function addWheel() 36 | { 37 | $this->car->setPart('wheelLF', new Parts\Wheel()); 38 | $this->car->setPart('wheelRF', new Parts\Wheel()); 39 | $this->car->setPart('wheelLR', new Parts\Wheel()); 40 | $this->car->setPart('wheelRR', new Parts\Wheel()); 41 | } 42 | 43 | /** 44 | * @return void 45 | */ 46 | public function createVehicle() 47 | { 48 | $this->car = new Parts\Car(); 49 | } 50 | 51 | /** 52 | * @return Parts\Car 53 | */ 54 | public function getVehicle() 55 | { 56 | return $this->car; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Behavioral/Specification/AbstractSpecification.php: -------------------------------------------------------------------------------- 1 | data = $data; 31 | } 32 | 33 | protected function processing(Request $req) 34 | { 35 | if ('get' === $req->verb) { 36 | if (array_key_exists($req->key, $this->data)) { 37 | $req->response = $this->data[$req->key]; 38 | 39 | return true; 40 | } 41 | } 42 | 43 | return false; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /locale/pt_BR/LC_MESSAGES/Creational/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../Creational/README.rst:2 15 | msgid "`Creational`__" 16 | msgstr "`Criacional`__" 17 | 18 | #: ../../Creational/README.rst:4 19 | msgid "" 20 | "In software engineering, creational design patterns are design patterns that" 21 | " deal with object creation mechanisms, trying to create objects in a manner " 22 | "suitable to the situation. The basic form of object creation could result in" 23 | " design problems or added complexity to the design. Creational design " 24 | "patterns solve this problem by somehow controlling this object creation." 25 | msgstr "" 26 | "Em engenharia de software, padrões de projeto do tipo criacional são padrões que" 27 | " trabalham com mecanismos de criação de objetos, criando objetos de maneira " 28 | "adequada às situações. A forma básica para criação de objetos pode resultar em" 29 | " problemas de design ou adicionar complexidade ao mesmo. Padrões de Criação " 30 | "resolvem este problema mantendo a criação do objeto sob controle." 31 | -------------------------------------------------------------------------------- /Structural/Bridge/README.rst: -------------------------------------------------------------------------------- 1 | `Bridge`__ 2 | ========== 3 | 4 | Purpose 5 | ------- 6 | 7 | Decouple an abstraction from its implementation so that the two can vary 8 | independently. 9 | 10 | Sample: 11 | ^^^^^^^ 12 | 13 | - `Symfony 14 | DoctrineBridge `__ 15 | 16 | UML Diagram 17 | ----------- 18 | 19 | .. image:: uml/uml.png 20 | :alt: Alt Bridge UML Diagram 21 | :align: center 22 | 23 | Code 24 | ---- 25 | 26 | You can also find these code on `GitHub`_ 27 | 28 | Workshop.php 29 | 30 | .. literalinclude:: Workshop.php 31 | :language: php 32 | :linenos: 33 | 34 | Assemble.php 35 | 36 | .. literalinclude:: Assemble.php 37 | :language: php 38 | :linenos: 39 | 40 | Produce.php 41 | 42 | .. literalinclude:: Produce.php 43 | :language: php 44 | :linenos: 45 | 46 | Vehicle.php 47 | 48 | .. literalinclude:: Vehicle.php 49 | :language: php 50 | :linenos: 51 | 52 | Motorcycle.php 53 | 54 | .. literalinclude:: Motorcycle.php 55 | :language: php 56 | :linenos: 57 | 58 | Car.php 59 | 60 | .. literalinclude:: Car.php 61 | :language: php 62 | :linenos: 63 | 64 | Test 65 | ---- 66 | 67 | Tests/BridgeTest.php 68 | 69 | .. literalinclude:: Tests/BridgeTest.php 70 | :language: php 71 | :linenos: 72 | 73 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/Bridge 74 | .. __: http://en.wikipedia.org/wiki/Bridge_pattern 75 | -------------------------------------------------------------------------------- /More/Delegation/README.rst: -------------------------------------------------------------------------------- 1 | `Delegation`__ 2 | ============== 3 | 4 | Purpose 5 | ------- 6 | 7 | Demonstrate the Delegator pattern, where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. In this case TeamLead professes to writeCode and Usage uses this, while TeamLead delegates writeCode to JuniorDeveloper's writeBadCode function. This inverts the responsibility so that Usage is unknowingly executing writeBadCode. 8 | 9 | Examples 10 | -------- 11 | 12 | Please review JuniorDeveloper.php, TeamLead.php, and then Usage.php to see it all tied together. 13 | 14 | UML Diagram 15 | ----------- 16 | 17 | .. image:: uml/uml.png 18 | :alt: Alt Delegation UML Diagram 19 | :align: center 20 | 21 | Code 22 | ---- 23 | 24 | You can also find these code on `GitHub`_ 25 | 26 | Usage.php 27 | 28 | .. literalinclude:: Usage.php 29 | :language: php 30 | :linenos: 31 | 32 | TeamLead.php 33 | 34 | .. literalinclude:: TeamLead.php 35 | :language: php 36 | :linenos: 37 | 38 | JuniorDeveloper.php 39 | 40 | .. literalinclude:: JuniorDeveloper.php 41 | :language: php 42 | :linenos: 43 | 44 | Test 45 | ---- 46 | 47 | Tests/DelegationTest.php 48 | 49 | .. literalinclude:: Tests/DelegationTest.php 50 | :language: php 51 | :linenos: 52 | 53 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/Delegation 54 | .. __: http://en.wikipedia.org/wiki/Delegation_pattern 55 | -------------------------------------------------------------------------------- /Structural/Decorator/README.rst: -------------------------------------------------------------------------------- 1 | `Decorator`__ 2 | ============= 3 | 4 | Purpose 5 | ------- 6 | 7 | To dynamically add new functionality to class instances. 8 | 9 | Examples 10 | -------- 11 | 12 | - Zend Framework: decorators for ``Zend_Form_Element`` instances 13 | - Web Service Layer: Decorators JSON and XML for a REST service (in 14 | this case, only one of these should be allowed of course) 15 | 16 | UML Diagram 17 | ----------- 18 | 19 | .. image:: uml/uml.png 20 | :alt: Alt Decorator UML Diagram 21 | :align: center 22 | 23 | Code 24 | ---- 25 | 26 | You can also find these code on `GitHub`_ 27 | 28 | RendererInterface.php 29 | 30 | .. literalinclude:: RendererInterface.php 31 | :language: php 32 | :linenos: 33 | 34 | Webservice.php 35 | 36 | .. literalinclude:: Webservice.php 37 | :language: php 38 | :linenos: 39 | 40 | Decorator.php 41 | 42 | .. literalinclude:: Decorator.php 43 | :language: php 44 | :linenos: 45 | 46 | RenderInXml.php 47 | 48 | .. literalinclude:: RenderInXml.php 49 | :language: php 50 | :linenos: 51 | 52 | RenderInJson.php 53 | 54 | .. literalinclude:: RenderInJson.php 55 | :language: php 56 | :linenos: 57 | 58 | Test 59 | ---- 60 | 61 | Tests/DecoratorTest.php 62 | 63 | .. literalinclude:: Tests/DecoratorTest.php 64 | :language: php 65 | :linenos: 66 | 67 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/Decorator 68 | .. __: http://en.wikipedia.org/wiki/Decorator_pattern 69 | -------------------------------------------------------------------------------- /More/Repository/README.rst: -------------------------------------------------------------------------------- 1 | Repository 2 | ========== 3 | 4 | Purpose 5 | ------- 6 | 7 | Mediates between the domain and data mapping layers using a 8 | collection-like interface for accessing domain objects. Repository 9 | encapsulates the set of objects persisted in a data store and the 10 | operations performed over them, providing a more object-oriented view of 11 | the persistence layer. Repository also supports the objective of 12 | achieving a clean separation and one-way dependency between the domain 13 | and data mapping layers. 14 | 15 | Examples 16 | -------- 17 | 18 | - Doctrine 2 ORM: there is Repository that mediates between Entity and 19 | DBAL and contains methods to retrieve objects 20 | - Laravel Framework 21 | 22 | UML Diagram 23 | ----------- 24 | 25 | .. image:: uml/uml.png 26 | :alt: Alt Repository UML Diagram 27 | :align: center 28 | 29 | Code 30 | ---- 31 | 32 | You can also find these code on `GitHub`_ 33 | 34 | Post.php 35 | 36 | .. literalinclude:: Post.php 37 | :language: php 38 | :linenos: 39 | 40 | PostRepository.php 41 | 42 | .. literalinclude:: PostRepository.php 43 | :language: php 44 | :linenos: 45 | 46 | Storage.php 47 | 48 | .. literalinclude:: Storage.php 49 | :language: php 50 | :linenos: 51 | 52 | MemoryStorage.php 53 | 54 | .. literalinclude:: MemoryStorage.php 55 | :language: php 56 | :linenos: 57 | 58 | Test 59 | ---- 60 | 61 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/Repository 62 | -------------------------------------------------------------------------------- /Structural/Composite/README.rst: -------------------------------------------------------------------------------- 1 | `Composite`__ 2 | ============= 3 | 4 | Purpose 5 | ------- 6 | 7 | To treat a group of objects the same way as a single instance of the 8 | object. 9 | 10 | Examples 11 | -------- 12 | 13 | - a form class instance handles all its form elements like a single 14 | instance of the form, when ``render()`` is called, it subsequently 15 | runs through all its child elements and calls ``render()`` on them 16 | - ``Zend_Config``: a tree of configuration options, each one is a 17 | ``Zend_Config`` object itself 18 | 19 | UML Diagram 20 | ----------- 21 | 22 | .. image:: uml/uml.png 23 | :alt: Alt Composite UML Diagram 24 | :align: center 25 | 26 | Code 27 | ---- 28 | 29 | You can also find these code on `GitHub`_ 30 | 31 | FormElement.php 32 | 33 | .. literalinclude:: FormElement.php 34 | :language: php 35 | :linenos: 36 | 37 | Form.php 38 | 39 | .. literalinclude:: Form.php 40 | :language: php 41 | :linenos: 42 | 43 | InputElement.php 44 | 45 | .. literalinclude:: InputElement.php 46 | :language: php 47 | :linenos: 48 | 49 | TextElement.php 50 | 51 | .. literalinclude:: TextElement.php 52 | :language: php 53 | :linenos: 54 | 55 | Test 56 | ---- 57 | 58 | Tests/CompositeTest.php 59 | 60 | .. literalinclude:: Tests/CompositeTest.php 61 | :language: php 62 | :linenos: 63 | 64 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/Composite 65 | .. __: http://en.wikipedia.org/wiki/Composite_pattern 66 | -------------------------------------------------------------------------------- /locale/ru/LC_MESSAGES/Creational/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: 2015-05-30 23:11+0300\n" 8 | "Last-Translator: Eugene Glotov \n" 9 | "MIME-Version: 1.0\n" 10 | "Content-Type: text/plain; charset=UTF-8\n" 11 | "Content-Transfer-Encoding: 8bit\n" 12 | "Language: ru\n" 13 | 14 | #: ../../Creational/README.rst:2 15 | msgid "`Creational`__" 16 | msgstr "Порождающие шаблоны проектирования (`Creational`__)" 17 | 18 | #: ../../Creational/README.rst:4 19 | msgid "" 20 | "In software engineering, creational design patterns are design patterns that" 21 | " deal with object creation mechanisms, trying to create objects in a manner " 22 | "suitable to the situation. The basic form of object creation could result in" 23 | " design problems or added complexity to the design. Creational design " 24 | "patterns solve this problem by somehow controlling this object creation." 25 | msgstr "" 26 | "В разработке программного обеспечения, Порождающие шаблоны проектирования – " 27 | "это паттерны, которые имеют дело с механизмами создания объекта и пытаются " 28 | "создать объекты в порядке, подходящем к ситуации. Обычная форма создания " 29 | "объекта может привести к проблемам проектирования или увеличивать сложность " 30 | "конструкции. Порождающие шаблоны проектирования решают эту проблему, " 31 | "определённым образом контролируя процесс создания объекта." 32 | -------------------------------------------------------------------------------- /Creational/Multiton/Multiton.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Creational\Prototype\BarBookPrototype 5 | 6 | \DesignPatterns\Creational\Prototype\BookPrototype 7 | \DesignPatterns\Creational\Prototype\FooBookPrototype 8 | \DesignPatterns\Creational\Prototype\BarBookPrototype 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | Fields 29 | Constants 30 | Constructors 31 | Methods 32 | 33 | private 34 | 35 | 36 | -------------------------------------------------------------------------------- /Creational/StaticFactory/README.rst: -------------------------------------------------------------------------------- 1 | Static Factory 2 | ============== 3 | 4 | Purpose 5 | ------- 6 | 7 | Similar to the AbstractFactory, this pattern is used to create series of 8 | related or dependent objects. The difference between this and the 9 | abstract factory pattern is that the static factory pattern uses just 10 | one static method to create all types of objects it can create. It is 11 | usually named ``factory`` or ``build``. 12 | 13 | Examples 14 | -------- 15 | 16 | - Zend Framework: ``Zend_Cache_Backend`` or ``_Frontend`` use a factory 17 | method create cache backends or frontends 18 | 19 | UML Diagram 20 | ----------- 21 | 22 | .. image:: uml/uml.png 23 | :alt: Alt StaticFactory UML Diagram 24 | :align: center 25 | 26 | Code 27 | ---- 28 | 29 | You can also find these code on `GitHub`_ 30 | 31 | StaticFactory.php 32 | 33 | .. literalinclude:: StaticFactory.php 34 | :language: php 35 | :linenos: 36 | 37 | FormatterInterface.php 38 | 39 | .. literalinclude:: FormatterInterface.php 40 | :language: php 41 | :linenos: 42 | 43 | FormatString.php 44 | 45 | .. literalinclude:: FormatString.php 46 | :language: php 47 | :linenos: 48 | 49 | FormatNumber.php 50 | 51 | .. literalinclude:: FormatNumber.php 52 | :language: php 53 | :linenos: 54 | 55 | Test 56 | ---- 57 | 58 | Tests/StaticFactoryTest.php 59 | 60 | .. literalinclude:: Tests/StaticFactoryTest.php 61 | :language: php 62 | :linenos: 63 | 64 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/StaticFactory 65 | -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/uml/TemplateMethod.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Behavioral\TemplateMethod\BeachJourney 5 | 6 | \DesignPatterns\Behavioral\TemplateMethod\CityJourney 7 | \DesignPatterns\Behavioral\TemplateMethod\Journey 8 | \DesignPatterns\Behavioral\TemplateMethod\BeachJourney 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | Fields 29 | Constants 30 | Constructors 31 | Methods 32 | 33 | private 34 | 35 | 36 | -------------------------------------------------------------------------------- /locale/ca/LC_MESSAGES/More/Delegation/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../More/Delegation/README.rst:2 15 | msgid "`Delegation`__" 16 | msgstr "" 17 | 18 | #: ../../More/Delegation/README.rst:5 19 | msgid "Purpose" 20 | msgstr "" 21 | 22 | #: ../../More/Delegation/README.rst:7 ../../More/Delegation/README.rst:12 23 | msgid "..." 24 | msgstr "" 25 | 26 | #: ../../More/Delegation/README.rst:10 27 | msgid "Examples" 28 | msgstr "" 29 | 30 | #: ../../More/Delegation/README.rst:15 31 | msgid "UML Diagram" 32 | msgstr "" 33 | 34 | #: ../../More/Delegation/README.rst:22 35 | msgid "Code" 36 | msgstr "" 37 | 38 | #: ../../More/Delegation/README.rst:24 39 | msgid "You can also find these code on `GitHub`_" 40 | msgstr "" 41 | 42 | #: ../../More/Delegation/README.rst:26 43 | msgid "Usage.php" 44 | msgstr "" 45 | 46 | #: ../../More/Delegation/README.rst:32 47 | msgid "TeamLead.php" 48 | msgstr "" 49 | 50 | #: ../../More/Delegation/README.rst:38 51 | msgid "JuniorDeveloper.php" 52 | msgstr "" 53 | 54 | #: ../../More/Delegation/README.rst:45 55 | msgid "Test" 56 | msgstr "" 57 | 58 | #: ../../More/Delegation/README.rst:47 59 | msgid "Tests/DelegationTest.php" 60 | msgstr "" 61 | -------------------------------------------------------------------------------- /locale/es/LC_MESSAGES/More/Delegation/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../More/Delegation/README.rst:2 15 | msgid "`Delegation`__" 16 | msgstr "" 17 | 18 | #: ../../More/Delegation/README.rst:5 19 | msgid "Purpose" 20 | msgstr "" 21 | 22 | #: ../../More/Delegation/README.rst:7 ../../More/Delegation/README.rst:12 23 | msgid "..." 24 | msgstr "" 25 | 26 | #: ../../More/Delegation/README.rst:10 27 | msgid "Examples" 28 | msgstr "" 29 | 30 | #: ../../More/Delegation/README.rst:15 31 | msgid "UML Diagram" 32 | msgstr "" 33 | 34 | #: ../../More/Delegation/README.rst:22 35 | msgid "Code" 36 | msgstr "" 37 | 38 | #: ../../More/Delegation/README.rst:24 39 | msgid "You can also find these code on `GitHub`_" 40 | msgstr "" 41 | 42 | #: ../../More/Delegation/README.rst:26 43 | msgid "Usage.php" 44 | msgstr "" 45 | 46 | #: ../../More/Delegation/README.rst:32 47 | msgid "TeamLead.php" 48 | msgstr "" 49 | 50 | #: ../../More/Delegation/README.rst:38 51 | msgid "JuniorDeveloper.php" 52 | msgstr "" 53 | 54 | #: ../../More/Delegation/README.rst:45 55 | msgid "Test" 56 | msgstr "" 57 | 58 | #: ../../More/Delegation/README.rst:47 59 | msgid "Tests/DelegationTest.php" 60 | msgstr "" 61 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/Tests/AbstractFactoryTest.php: -------------------------------------------------------------------------------- 1 | createText('Lorem Ipsum'), 33 | $factory->createPicture('/image.jpg', 'caption'), 34 | $factory->createText('footnotes') 35 | ); 36 | 37 | $this->assertContainsOnly('DesignPatterns\Creational\AbstractFactory\MediaInterface', $article); 38 | 39 | /* this is the time to look at the Builder pattern. This pattern 40 | * helps you to create complex object like that article above with 41 | * a given Abstract Factory 42 | */ 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /locale/zh_CN/LC_MESSAGES/More/Delegation/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../More/Delegation/README.rst:2 15 | msgid "`Delegation`__" 16 | msgstr "" 17 | 18 | #: ../../More/Delegation/README.rst:5 19 | msgid "Purpose" 20 | msgstr "" 21 | 22 | #: ../../More/Delegation/README.rst:7 ../../More/Delegation/README.rst:12 23 | msgid "..." 24 | msgstr "" 25 | 26 | #: ../../More/Delegation/README.rst:10 27 | msgid "Examples" 28 | msgstr "" 29 | 30 | #: ../../More/Delegation/README.rst:15 31 | msgid "UML Diagram" 32 | msgstr "" 33 | 34 | #: ../../More/Delegation/README.rst:22 35 | msgid "Code" 36 | msgstr "" 37 | 38 | #: ../../More/Delegation/README.rst:24 39 | msgid "You can also find these code on `GitHub`_" 40 | msgstr "" 41 | 42 | #: ../../More/Delegation/README.rst:26 43 | msgid "Usage.php" 44 | msgstr "" 45 | 46 | #: ../../More/Delegation/README.rst:32 47 | msgid "TeamLead.php" 48 | msgstr "" 49 | 50 | #: ../../More/Delegation/README.rst:38 51 | msgid "JuniorDeveloper.php" 52 | msgstr "" 53 | 54 | #: ../../More/Delegation/README.rst:45 55 | msgid "Test" 56 | msgstr "" 57 | 58 | #: ../../More/Delegation/README.rst:47 59 | msgid "Tests/DelegationTest.php" 60 | msgstr "" 61 | -------------------------------------------------------------------------------- /Behavioral/Strategy/README.rst: -------------------------------------------------------------------------------- 1 | `Strategy`__ 2 | ============ 3 | 4 | Terminology: 5 | ------------ 6 | 7 | - Context 8 | - Strategy 9 | - Concrete Strategy 10 | 11 | Purpose 12 | ------- 13 | 14 | To separate strategies and to enable fast switching between them. Also 15 | this pattern is a good alternative to inheritance (instead of having an 16 | abstract class that is extended). 17 | 18 | Examples 19 | -------- 20 | 21 | - sorting a list of objects, one strategy by date, the other by id 22 | - simplify unit testing: e.g. switching between file and in-memory 23 | storage 24 | 25 | UML Diagram 26 | ----------- 27 | 28 | .. image:: uml/uml.png 29 | :alt: Alt Strategy UML Diagram 30 | :align: center 31 | 32 | Code 33 | ---- 34 | 35 | You can also find these code on `GitHub`_ 36 | 37 | ObjectCollection.php 38 | 39 | .. literalinclude:: ObjectCollection.php 40 | :language: php 41 | :linenos: 42 | 43 | ComparatorInterface.php 44 | 45 | .. literalinclude:: ComparatorInterface.php 46 | :language: php 47 | :linenos: 48 | 49 | DateComparator.php 50 | 51 | .. literalinclude:: DateComparator.php 52 | :language: php 53 | :linenos: 54 | 55 | IdComparator.php 56 | 57 | .. literalinclude:: IdComparator.php 58 | :language: php 59 | :linenos: 60 | 61 | Test 62 | ---- 63 | 64 | Tests/StrategyTest.php 65 | 66 | .. literalinclude:: Tests/StrategyTest.php 67 | :language: php 68 | :linenos: 69 | 70 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Strategy 71 | .. __: http://en.wikipedia.org/wiki/Strategy_pattern -------------------------------------------------------------------------------- /Behavioral/Visitor/README.rst: -------------------------------------------------------------------------------- 1 | `Visitor`__ 2 | =========== 3 | 4 | Purpose 5 | ------- 6 | 7 | The Visitor Pattern lets you outsource operations on objects to other 8 | objects. The main reason to do this is to keep a separation of concerns. 9 | But classes have to define a contract to allow visitors (the 10 | ``Role::accept`` method in the example). 11 | 12 | The contract is an abstract class but you can have also a clean 13 | interface. In that case, each Visitor has to choose itself which method 14 | to invoke on the visitor. 15 | 16 | UML Diagram 17 | ----------- 18 | 19 | .. image:: uml/uml.png 20 | :alt: Alt Visitor UML Diagram 21 | :align: center 22 | 23 | Code 24 | ---- 25 | 26 | You can also find these code on `GitHub`_ 27 | 28 | RoleVisitorInterface.php 29 | 30 | .. literalinclude:: RoleVisitorInterface.php 31 | :language: php 32 | :linenos: 33 | 34 | RolePrintVisitor.php 35 | 36 | .. literalinclude:: RolePrintVisitor.php 37 | :language: php 38 | :linenos: 39 | 40 | Role.php 41 | 42 | .. literalinclude:: Role.php 43 | :language: php 44 | :linenos: 45 | 46 | User.php 47 | 48 | .. literalinclude:: User.php 49 | :language: php 50 | :linenos: 51 | 52 | Group.php 53 | 54 | .. literalinclude:: Group.php 55 | :language: php 56 | :linenos: 57 | 58 | Test 59 | ---- 60 | 61 | Tests/VisitorTest.php 62 | 63 | .. literalinclude:: Tests/VisitorTest.php 64 | :language: php 65 | :linenos: 66 | 67 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Visitor 68 | .. __: http://en.wikipedia.org/wiki/Visitor_pattern -------------------------------------------------------------------------------- /locale/pt_BR/LC_MESSAGES/More/Delegation/README.po: -------------------------------------------------------------------------------- 1 | # 2 | msgid "" 3 | msgstr "" 4 | "Project-Id-Version: DesignPatternsPHP 1.0\n" 5 | "Report-Msgid-Bugs-To: \n" 6 | "POT-Creation-Date: 2015-05-29 12:18+0200\n" 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 8 | "Last-Translator: FULL NAME \n" 9 | "Language-Team: LANGUAGE \n" 10 | "MIME-Version: 1.0\n" 11 | "Content-Type: text/plain; charset=UTF-8\n" 12 | "Content-Transfer-Encoding: 8bit\n" 13 | 14 | #: ../../More/Delegation/README.rst:2 15 | msgid "`Delegation`__" 16 | msgstr "" 17 | 18 | #: ../../More/Delegation/README.rst:5 19 | msgid "Purpose" 20 | msgstr "" 21 | 22 | #: ../../More/Delegation/README.rst:7 ../../More/Delegation/README.rst:12 23 | msgid "..." 24 | msgstr "" 25 | 26 | #: ../../More/Delegation/README.rst:10 27 | msgid "Examples" 28 | msgstr "" 29 | 30 | #: ../../More/Delegation/README.rst:15 31 | msgid "UML Diagram" 32 | msgstr "Diagrama UML" 33 | 34 | #: ../../More/Delegation/README.rst:22 35 | msgid "Code" 36 | msgstr "" 37 | 38 | #: ../../More/Delegation/README.rst:24 39 | msgid "You can also find these code on `GitHub`_" 40 | msgstr "" 41 | 42 | #: ../../More/Delegation/README.rst:26 43 | msgid "Usage.php" 44 | msgstr "" 45 | 46 | #: ../../More/Delegation/README.rst:32 47 | msgid "TeamLead.php" 48 | msgstr "" 49 | 50 | #: ../../More/Delegation/README.rst:38 51 | msgid "JuniorDeveloper.php" 52 | msgstr "" 53 | 54 | #: ../../More/Delegation/README.rst:45 55 | msgid "Test" 56 | msgstr "" 57 | 58 | #: ../../More/Delegation/README.rst:47 59 | msgid "Tests/DelegationTest.php" 60 | msgstr "" 61 | -------------------------------------------------------------------------------- /Behavioral/Mediator/Mediator.php: -------------------------------------------------------------------------------- 1 | database = $db; 37 | $this->server = $srv; 38 | $this->client = $cl; 39 | } 40 | 41 | /** 42 | * make request 43 | */ 44 | public function makeRequest() 45 | { 46 | $this->server->process(); 47 | } 48 | 49 | /** 50 | * query db 51 | * @return mixed 52 | */ 53 | public function queryDb() 54 | { 55 | return $this->database->getData(); 56 | } 57 | 58 | /** 59 | * send response 60 | * 61 | * @param string $content 62 | */ 63 | public function sendResponse($content) 64 | { 65 | $this->client->output($content); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Behavioral/Iterator/README.rst: -------------------------------------------------------------------------------- 1 | `Iterator`__ 2 | ============ 3 | 4 | Purpose 5 | ------- 6 | 7 | To make an object iterable and to make it appear like a collection of 8 | objects. 9 | 10 | Examples 11 | -------- 12 | 13 | - to process a file line by line by just running over all lines (which 14 | have an object representation) for a file (which of course is an 15 | object, too) 16 | 17 | Note 18 | ---- 19 | 20 | Standard PHP Library (SPL) defines an interface Iterator which is best 21 | suited for this! Often you would want to implement the Countable 22 | interface too, to allow ``count($object)`` on your iterable object 23 | 24 | UML Diagram 25 | ----------- 26 | 27 | .. image:: uml/uml.png 28 | :alt: Alt Iterator UML Diagram 29 | :align: center 30 | 31 | Code 32 | ---- 33 | 34 | You can also find these code on `GitHub`_ 35 | 36 | Book.php 37 | 38 | .. literalinclude:: Book.php 39 | :language: php 40 | :linenos: 41 | 42 | BookList.php 43 | 44 | .. literalinclude:: BookList.php 45 | :language: php 46 | :linenos: 47 | 48 | BookListIterator.php 49 | 50 | .. literalinclude:: BookListIterator.php 51 | :language: php 52 | :linenos: 53 | 54 | BookListReverseIterator.php 55 | 56 | .. literalinclude:: BookListReverseIterator.php 57 | :language: php 58 | :linenos: 59 | 60 | Test 61 | ---- 62 | 63 | Tests/IteratorTest.php 64 | 65 | .. literalinclude:: Tests/IteratorTest.php 66 | :language: php 67 | :linenos: 68 | 69 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Iterator 70 | .. __: http://en.wikipedia.org/wiki/Iterator_pattern -------------------------------------------------------------------------------- /Creational/FactoryMethod/Tests/FactoryMethodTest.php: -------------------------------------------------------------------------------- 1 | type as $oneType) { 36 | $vehicle = $shop->create($oneType); 37 | $this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\VehicleInterface', $vehicle); 38 | } 39 | } 40 | 41 | /** 42 | * @dataProvider getShop 43 | * @expectedException \InvalidArgumentException 44 | * @expectedExceptionMessage spaceship is not a valid vehicle 45 | */ 46 | public function testUnknownType(FactoryMethod $shop) 47 | { 48 | $shop->create('spaceship'); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Behavioral/NullObject/uml/NullObject.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Behavioral\NullObject\Service 5 | 6 | \DesignPatterns\Behavioral\NullObject\NullLogger 7 | \DesignPatterns\Behavioral\NullObject\PrintLogger 8 | \DesignPatterns\Behavioral\NullObject\LoggerInterface 9 | \DesignPatterns\Behavioral\NullObject\Service 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | Fields 30 | Constants 31 | Constructors 32 | Methods 33 | 34 | private 35 | 36 | 37 | -------------------------------------------------------------------------------- /Structural/Adapter/README.rst: -------------------------------------------------------------------------------- 1 | `Adapter / Wrapper`__ 2 | ===================== 3 | 4 | Purpose 5 | ------- 6 | 7 | To translate one interface for a class into a compatible interface. An 8 | adapter allows classes to work together that normally could not because 9 | of incompatible interfaces by providing it's interface to clients while 10 | using the original interface. 11 | 12 | Examples 13 | -------- 14 | 15 | - DB Client libraries adapter 16 | - using multiple different webservices and adapters normalize data so 17 | that the outcome is the same for all 18 | 19 | UML Diagram 20 | ----------- 21 | 22 | .. image:: uml/uml.png 23 | :alt: Alt Adapter UML Diagram 24 | :align: center 25 | 26 | Code 27 | ---- 28 | 29 | You can also find these code on `GitHub`_ 30 | 31 | PaperBookInterface.php 32 | 33 | .. literalinclude:: PaperBookInterface.php 34 | :language: php 35 | :linenos: 36 | 37 | Book.php 38 | 39 | .. literalinclude:: Book.php 40 | :language: php 41 | :linenos: 42 | 43 | EBookAdapter.php 44 | 45 | .. literalinclude:: EBookAdapter.php 46 | :language: php 47 | :linenos: 48 | 49 | EBookInterface.php 50 | 51 | .. literalinclude:: EBookInterface.php 52 | :language: php 53 | :linenos: 54 | 55 | Kindle.php 56 | 57 | .. literalinclude:: Kindle.php 58 | :language: php 59 | :linenos: 60 | 61 | Test 62 | ---- 63 | 64 | Tests/AdapterTest.php 65 | 66 | .. literalinclude:: Tests/AdapterTest.php 67 | :language: php 68 | :linenos: 69 | 70 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/Adapter 71 | .. __: http://en.wikipedia.org/wiki/Adapter_pattern 72 | -------------------------------------------------------------------------------- /Structural/Facade/Tests/FacadeTest.php: -------------------------------------------------------------------------------- 1 | getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface') 17 | ->setMethods(array('launch', 'execute', 'waitForKeyPress')) 18 | ->disableAutoload() 19 | ->getMock(); 20 | $operatingSys = $this->getMockBuilder('DesignPatterns\Structural\Facade\OsInterface') 21 | ->setMethods(array('getName')) 22 | ->disableAutoload() 23 | ->getMock(); 24 | $bios->expects($this->once()) 25 | ->method('launch') 26 | ->with($operatingSys); 27 | $operatingSys 28 | ->expects($this->once()) 29 | ->method('getName') 30 | ->will($this->returnValue('Linux')); 31 | 32 | $facade = new Computer($bios, $operatingSys); 33 | return array(array($facade, $operatingSys)); 34 | } 35 | 36 | /** 37 | * @dataProvider getComputer 38 | */ 39 | public function testComputerOn(Computer $facade, OsInterface $os) 40 | { 41 | // interface is simpler : 42 | $facade->turnOn(); 43 | // but I can access to lower component 44 | $this->assertEquals('Linux', $os->getName()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Behavioral/Strategy/uml/Strategy.uml: -------------------------------------------------------------------------------- 1 | 2 | 3 | PHP 4 | \DesignPatterns\Behavioral\Strategy\ComparatorInterface 5 | 6 | \DesignPatterns\Behavioral\Strategy\ObjectCollection 7 | \DesignPatterns\Behavioral\Strategy\DateComparator 8 | \DesignPatterns\Behavioral\Strategy\ComparatorInterface 9 | \DesignPatterns\Behavioral\Strategy\IdComparator 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | Fields 30 | Constants 31 | Constructors 32 | Methods 33 | 34 | private 35 | 36 | 37 | -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/README.rst: -------------------------------------------------------------------------------- 1 | `Template Method`__ 2 | =================== 3 | 4 | Purpose 5 | ------- 6 | 7 | Template Method is a behavioral design pattern. 8 | 9 | Perhaps you have encountered it many times already. The idea is to let 10 | subclasses of this abstract template "finish" the behavior of an 11 | algorithm. 12 | 13 | A.k.a the "Hollywood principle": "Don't call us, we call you." This 14 | class is not called by subclasses but the inverse. How? With abstraction 15 | of course. 16 | 17 | In other words, this is a skeleton of algorithm, well-suited for 18 | framework libraries. The user has just to implement one method and the 19 | superclass do the job. 20 | 21 | It is an easy way to decouple concrete classes and reduce copy-paste, 22 | that's why you'll find it everywhere. 23 | 24 | UML Diagram 25 | ----------- 26 | 27 | .. image:: uml/uml.png 28 | :alt: Alt TemplateMethod UML Diagram 29 | :align: center 30 | 31 | Code 32 | ---- 33 | 34 | You can also find these code on `GitHub`_ 35 | 36 | Journey.php 37 | 38 | .. literalinclude:: Journey.php 39 | :language: php 40 | :linenos: 41 | 42 | BeachJourney.php 43 | 44 | .. literalinclude:: BeachJourney.php 45 | :language: php 46 | :linenos: 47 | 48 | CityJourney.php 49 | 50 | .. literalinclude:: CityJourney.php 51 | :language: php 52 | :linenos: 53 | 54 | Test 55 | ---- 56 | 57 | Tests/JourneyTest.php 58 | 59 | .. literalinclude:: Tests/JourneyTest.php 60 | :language: php 61 | :linenos: 62 | 63 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/TemplateMethod 64 | .. __: http://en.wikipedia.org/wiki/Template_method_pattern --------------------------------------------------------------------------------