diff --git a/examples-per-language/php/behavioral/Chain_Of_Responsibility.php b/examples-per-language/php/behavioral/Chain_Of_Responsibility.php new file mode 100644 index 00000000..c66b6622 --- /dev/null +++ b/examples-per-language/php/behavioral/Chain_Of_Responsibility.php @@ -0,0 +1,23 @@ +$paypal->$bitcoin + +$bank = new Bank(100); +$paypal = new Paypal(200); +$bitcoin = new BitCoin(300); + +// First priority bank +// If bank can't pay then paypal +// If paypal can't pay then bit coin. +$bank->setNext($paypal); +$paypal->setNext($bitcoin); + +// Let's try and pay our bill. +$bank->pay(259); diff --git a/examples-per-language/php/behavioral/Command.php b/examples-per-language/php/behavioral/Command.php new file mode 100644 index 00000000..93a5c6e1 --- /dev/null +++ b/examples-per-language/php/behavioral/Command.php @@ -0,0 +1,17 @@ +submit($turnOn); +$remote->submit($turnOff); diff --git a/examples-per-language/php/behavioral/Iterator.php b/examples-per-language/php/behavioral/Iterator.php new file mode 100644 index 00000000..d9f02de4 --- /dev/null +++ b/examples-per-language/php/behavioral/Iterator.php @@ -0,0 +1,31 @@ +addStation(new RadioStation(89)); +$stationList->addStation(new RadioStation(101)); +$stationList->addStation(new RadioStation(102)); +$stationList->addStation(new RadioStation(103.2)); + +/** @var RadioStation $station */ +echo 'These are the known Radio Stations' . PHP_EOL; +foreach ($stationList as $station) { + echo $station->getFrequency(). PHP_EOL; +} + +$stationList->removeStation(new RadioStation(89)); + +// To prove the station was indeed removed, we iterate through the list again. +// Small caveat : Iterators need to be rewinded after beeing Iterated! +$stationList->rewind(); + +/** @var RadioStation $station */ +echo 'These are the known Radio Stations' . PHP_EOL; +foreach ($stationList as $station) { + echo $station->getFrequency(). PHP_EOL; +} diff --git a/examples-per-language/php/behavioral/Mediator.php b/examples-per-language/php/behavioral/Mediator.php new file mode 100644 index 00000000..9c02ba67 --- /dev/null +++ b/examples-per-language/php/behavioral/Mediator.php @@ -0,0 +1,14 @@ +send('Hi there!'); +$jane->send('Hey!'); diff --git a/examples-per-language/php/behavioral/Memento.php b/examples-per-language/php/behavioral/Memento.php new file mode 100644 index 00000000..c4735154 --- /dev/null +++ b/examples-per-language/php/behavioral/Memento.php @@ -0,0 +1,20 @@ +type('This is the first sentence' . PHP_EOL); +$editor->type('This is the second sentence' . PHP_EOL); + +$saved = $editor->save(); + +$editor->type('And this is the third one.' . PHP_EOL); + +echo $editor->getContent(); + +$editor->restore($saved); +echo 'Restored $editor.' . PHP_EOL; +echo $editor->getContent(); diff --git a/examples-per-language/php/behavioral/Observer.php b/examples-per-language/php/behavioral/Observer.php new file mode 100644 index 00000000..c6c0e42e --- /dev/null +++ b/examples-per-language/php/behavioral/Observer.php @@ -0,0 +1,16 @@ +attach($john); +$jobPostings->attach($jane); + +$jobPostings->add(new JobPost('Software Engineer')); diff --git a/examples-per-language/php/behavioral/State.php b/examples-per-language/php/behavioral/State.php new file mode 100644 index 00000000..5b8645bc --- /dev/null +++ b/examples-per-language/php/behavioral/State.php @@ -0,0 +1,22 @@ +type('First line'); + +$editor->setState(new UpperCase()); + +$editor->type('Second line'); +$editor->type('Third line'); + +$editor->setState(new LowerCase()); + +$editor->type('Fourth line'); +$editor->type('Fifth line'); diff --git a/examples-per-language/php/behavioral/Strategy.php b/examples-per-language/php/behavioral/Strategy.php new file mode 100644 index 00000000..b84e1789 --- /dev/null +++ b/examples-per-language/php/behavioral/Strategy.php @@ -0,0 +1,15 @@ +sort($dataset); + +$sorter = new Sorter(new QuickSortStrategy()); +$sorter->sort($dataset); diff --git a/examples-per-language/php/behavioral/Template.php b/examples-per-language/php/behavioral/Template.php new file mode 100644 index 00000000..0dcbeb2f --- /dev/null +++ b/examples-per-language/php/behavioral/Template.php @@ -0,0 +1,12 @@ +build(); + +$iosBuilder = new IosBuilder(); +$iosBuilder->build(); diff --git a/examples-per-language/php/behavioral/Visitor.php b/examples-per-language/php/behavioral/Visitor.php new file mode 100644 index 00000000..f9def424 --- /dev/null +++ b/examples-per-language/php/behavioral/Visitor.php @@ -0,0 +1,29 @@ +accept($speak); +$lion->accept($speak); +$dolphin->accept($speak); + +// Using the Visitor pattern it becomes easy to 'add' new functionality to +// the animals. Declare functionality in a class that implements Visitor. + +$jump = new Jump(); + +$monkey->accept($jump); +$lion->accept($jump); +$dolphin->accept($jump); \ No newline at end of file diff --git a/examples-per-language/php/behavioral/autoload.php b/examples-per-language/php/behavioral/autoload.php new file mode 100644 index 00000000..fa8db82c --- /dev/null +++ b/examples-per-language/php/behavioral/autoload.php @@ -0,0 +1,7 @@ +successor = $account; + } + + public function pay($amountToPay) + { + if ($this->canPay($amountToPay)) { + echo sprintf('Paid %s using %s' . PHP_EOL, $amountToPay, + get_called_class()); + } elseif ($this->successor) { + echo sprintf('Cannot pay using %s. Proceeding ...' . PHP_EOL, + get_called_class()); + $this->successor->pay($amountToPay); + } else { + throw new \Exception('None of the accounts have enough balance'); + } + } + + public function canPay($amount) + { + return $this->balance >= $amount; + } +} diff --git a/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Bank.php b/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Bank.php new file mode 100644 index 00000000..7516553e --- /dev/null +++ b/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Bank.php @@ -0,0 +1,16 @@ +balance = $balance; + } +} diff --git a/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/BitCoin.php b/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/BitCoin.php new file mode 100644 index 00000000..03aef73d --- /dev/null +++ b/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/BitCoin.php @@ -0,0 +1,16 @@ +balance = $balance; + } +} diff --git a/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Paypal.php b/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Paypal.php new file mode 100644 index 00000000..8a306f7c --- /dev/null +++ b/examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Paypal.php @@ -0,0 +1,16 @@ +balance = $balance; + } +} diff --git a/examples-per-language/php/behavioral/src/Command/Bulb.php b/examples-per-language/php/behavioral/src/Command/Bulb.php new file mode 100644 index 00000000..efee1f6a --- /dev/null +++ b/examples-per-language/php/behavioral/src/Command/Bulb.php @@ -0,0 +1,19 @@ +execute(); + } + +} diff --git a/examples-per-language/php/behavioral/src/Command/TurnOff.php b/examples-per-language/php/behavioral/src/Command/TurnOff.php new file mode 100644 index 00000000..cebbf94e --- /dev/null +++ b/examples-per-language/php/behavioral/src/Command/TurnOff.php @@ -0,0 +1,30 @@ +bulb = $bulb; + } + + public function execute() + { + $this->bulb->turnOff(); + } + + public function undo() + { + $this->bulb->turnOn(); + } + + public function redo() + { + $this->execute(); + } +} diff --git a/examples-per-language/php/behavioral/src/Command/TurnOn.php b/examples-per-language/php/behavioral/src/Command/TurnOn.php new file mode 100644 index 00000000..f2c5b6bb --- /dev/null +++ b/examples-per-language/php/behavioral/src/Command/TurnOn.php @@ -0,0 +1,30 @@ +bulb = $bulb; + } + + public function execute() + { + $this->bulb->turnOn(); + } + + public function undo() + { + $this->bulb->turnOff(); + } + + public function redo() + { + $this->execute(); + } +} diff --git a/examples-per-language/php/behavioral/src/Iterator/RadioStation.php b/examples-per-language/php/behavioral/src/Iterator/RadioStation.php new file mode 100644 index 00000000..e5f94544 --- /dev/null +++ b/examples-per-language/php/behavioral/src/Iterator/RadioStation.php @@ -0,0 +1,19 @@ +frequency = $frequency; + } + + public function getFrequency() + { + return $this->frequency; + } +} diff --git a/examples-per-language/php/behavioral/src/Iterator/StationList.php b/examples-per-language/php/behavioral/src/Iterator/StationList.php new file mode 100644 index 00000000..18971145 --- /dev/null +++ b/examples-per-language/php/behavioral/src/Iterator/StationList.php @@ -0,0 +1,61 @@ +stations[] = $station; + } + + public function removeStation(RadioStation $toRemove) + { + $toRemoveFrequency = $toRemove->getFrequency(); + $this->stations = array_filter($this->stations, function (RadioStation $station) use ($toRemoveFrequency) { + return $station->getFrequency() !== $toRemoveFrequency; + }); + } + + public function current() + { + return $this->stations[$this->counter]; + } + + public function next() + { + $this->counter++; + } + + public function key() + { + return $this->counter; + } + + public function valid() + { + return isset($this->stations[$this->counter]); + } + + public function rewind() + { + // When using a numbered array like we do with $counter, we need to reset + // the array as well, otherwise the removed station will wreak havoc with + // our iteration, because the value of removed station at position X will + // be NULL; + $this->stations = array_values($this->stations); + return $this->counter = 0; + } + + public function count() + { + return count($this->stations); + } +} diff --git a/examples-per-language/php/behavioral/src/Mediator/ChatRoom.php b/examples-per-language/php/behavioral/src/Mediator/ChatRoom.php new file mode 100644 index 00000000..ea40bab9 --- /dev/null +++ b/examples-per-language/php/behavioral/src/Mediator/ChatRoom.php @@ -0,0 +1,16 @@ +getName(); + + echo $time . '[' . $sender . ']:' . $message . PHP_EOL; + } +} diff --git a/examples-per-language/php/behavioral/src/Mediator/ChatRoomMediator.php b/examples-per-language/php/behavioral/src/Mediator/ChatRoomMediator.php new file mode 100644 index 00000000..b64bc350 --- /dev/null +++ b/examples-per-language/php/behavioral/src/Mediator/ChatRoomMediator.php @@ -0,0 +1,9 @@ +name = $name; + $this->chatMediator = $chatMediator; + } + + public function getName() + { + return $this->name; + } + + public function send($message) + { + $this->chatMediator->showMessage($this, $message); + } +} diff --git a/examples-per-language/php/behavioral/src/Memento/Editor.php b/examples-per-language/php/behavioral/src/Memento/Editor.php new file mode 100644 index 00000000..c1a8e4a1 --- /dev/null +++ b/examples-per-language/php/behavioral/src/Memento/Editor.php @@ -0,0 +1,30 @@ +content; + } + + public function type($words) + { + $this->content = $this->content . ' ' . $words; + } + + public function save() + { + return new EditorMemento($this->content); + } + + public function restore(EditorMemento $memento) + { + $this->content = $memento->getContent(); + } + +} diff --git a/examples-per-language/php/behavioral/src/Memento/EditorMemento.php b/examples-per-language/php/behavioral/src/Memento/EditorMemento.php new file mode 100644 index 00000000..8df950b0 --- /dev/null +++ b/examples-per-language/php/behavioral/src/Memento/EditorMemento.php @@ -0,0 +1,20 @@ +content = $content; + } + + public function getContent() + { + return $this->content; + } + +} diff --git a/examples-per-language/php/behavioral/src/Observer/JobPost.php b/examples-per-language/php/behavioral/src/Observer/JobPost.php new file mode 100644 index 00000000..3e1f46c4 --- /dev/null +++ b/examples-per-language/php/behavioral/src/Observer/JobPost.php @@ -0,0 +1,21 @@ +title = $title; + } + + public function getTitle() + { + return $this->title; + } + + +} diff --git a/examples-per-language/php/behavioral/src/Observer/JobPostings.php b/examples-per-language/php/behavioral/src/Observer/JobPostings.php new file mode 100644 index 00000000..b0971755 --- /dev/null +++ b/examples-per-language/php/behavioral/src/Observer/JobPostings.php @@ -0,0 +1,27 @@ +observers as $observer) { + $observer->onJobPosted($jobPosting); + } + } + + public function attach(Observer $observer) + { + $this->observers[] = $observer; + } + + public function add(JobPost $jobPosting) + { + $this->notify($jobPosting); + } +} diff --git a/examples-per-language/php/behavioral/src/Observer/JobSeeker.php b/examples-per-language/php/behavioral/src/Observer/JobSeeker.php new file mode 100644 index 00000000..7492776a --- /dev/null +++ b/examples-per-language/php/behavioral/src/Observer/JobSeeker.php @@ -0,0 +1,20 @@ +name = $name; + } + + public function onJobPosted(JobPost $job) + { + echo 'Hi ', $this->name . '! New job posted: ' . $job->getTitle() . PHP_EOL; + } +} diff --git a/examples-per-language/php/behavioral/src/Observer/Observable.php b/examples-per-language/php/behavioral/src/Observer/Observable.php new file mode 100644 index 00000000..860310dd --- /dev/null +++ b/examples-per-language/php/behavioral/src/Observer/Observable.php @@ -0,0 +1,11 @@ +state = $state; + } + + public function setState(WritingState $state) + { + $this->state = $state; + } + + public function type($words) + { + $this->state->write($words); + } + +} diff --git a/examples-per-language/php/behavioral/src/State/UpperCase.php b/examples-per-language/php/behavioral/src/State/UpperCase.php new file mode 100644 index 00000000..bb0c0174 --- /dev/null +++ b/examples-per-language/php/behavioral/src/State/UpperCase.php @@ -0,0 +1,13 @@ +sorter = $sorter; + } + + public function sort(array $dataset) + { + return $this->sorter->sort($dataset); + } + +} diff --git a/examples-per-language/php/behavioral/src/Template/AndroidBuilder.php b/examples-per-language/php/behavioral/src/Template/AndroidBuilder.php new file mode 100644 index 00000000..3c187d2b --- /dev/null +++ b/examples-per-language/php/behavioral/src/Template/AndroidBuilder.php @@ -0,0 +1,28 @@ +test(); + $this->lint(); + $this->assemble(); + $this->deploy(); + } + + abstract public function test(); + abstract public function lint(); + abstract public function assemble(); + abstract public function deploy(); +} diff --git a/examples-per-language/php/behavioral/src/Template/IosBuilder.php b/examples-per-language/php/behavioral/src/Template/IosBuilder.php new file mode 100644 index 00000000..fec5219d --- /dev/null +++ b/examples-per-language/php/behavioral/src/Template/IosBuilder.php @@ -0,0 +1,28 @@ +visitDolphin($this); + } + + public function speak() + { + echo '[High pitched ramblings about the end of the world].' . PHP_EOL; + } +} diff --git a/examples-per-language/php/behavioral/src/Visitor/Jump.php b/examples-per-language/php/behavioral/src/Visitor/Jump.php new file mode 100644 index 00000000..20d7394e --- /dev/null +++ b/examples-per-language/php/behavioral/src/Visitor/Jump.php @@ -0,0 +1,23 @@ +visitLion($this); + } + + public function roar() + { + echo 'Roarrrrrr!' . PHP_EOL; + } +} diff --git a/examples-per-language/php/behavioral/src/Visitor/Monkey.php b/examples-per-language/php/behavioral/src/Visitor/Monkey.php new file mode 100644 index 00000000..85bf16ce --- /dev/null +++ b/examples-per-language/php/behavioral/src/Visitor/Monkey.php @@ -0,0 +1,18 @@ +visitMonkey($this); + } + + public function shout() + { + echo 'Ooh oo Aa aa!' . PHP_EOL; + } +} diff --git a/examples-per-language/php/behavioral/src/Visitor/Speak.php b/examples-per-language/php/behavioral/src/Visitor/Speak.php new file mode 100644 index 00000000..831a146b --- /dev/null +++ b/examples-per-language/php/behavioral/src/Visitor/Speak.php @@ -0,0 +1,23 @@ +shout(); + } + + public function visitLion(Lion $lion) + { + $lion->roar(); + } + + public function visitDolphin(Dolphin $dolphin) + { + $dolphin->speak(); + } +} diff --git a/examples-per-language/php/creational/AbstractFactoryMethod.php b/examples-per-language/php/creational/AbstractFactoryMethod.php new file mode 100644 index 00000000..2aa5de5c --- /dev/null +++ b/examples-per-language/php/creational/AbstractFactoryMethod.php @@ -0,0 +1,22 @@ +makeDoor(); +$expert = $woodenDoorFactory->makeFittingExpert(); + +$door->getDescription(); +$expert->getDescription(); + +$ironFactory = new IronDoorFactory(); + +$door = $ironFactory->makeDoor(); +$expert = $ironFactory->makeFittingExpert(); + +$door->getDescription(); +$expert->getDescription(); diff --git a/examples-per-language/php/creational/Builder.php b/examples-per-language/php/creational/Builder.php new file mode 100644 index 00000000..d649d0a7 --- /dev/null +++ b/examples-per-language/php/creational/Builder.php @@ -0,0 +1,13 @@ +addPepperoni() + ->addLettuce() + ->addTomato() + ->build(); + +var_dump($burger); diff --git a/examples-per-language/php/creational/FactoryMethod.php b/examples-per-language/php/creational/FactoryMethod.php new file mode 100644 index 00000000..5d6df106 --- /dev/null +++ b/examples-per-language/php/creational/FactoryMethod.php @@ -0,0 +1,12 @@ +takeInterview(); + +$marketingManager = new MarketingManager(); +$marketingManager->takeInterview(); diff --git a/examples-per-language/php/creational/Prototype.php b/examples-per-language/php/creational/Prototype.php new file mode 100644 index 00000000..d61fcfd3 --- /dev/null +++ b/examples-per-language/php/creational/Prototype.php @@ -0,0 +1,16 @@ +getName() . PHP_EOL; +echo $original->getCategory() . PHP_EOL; + +$cloned = clone $original; +$cloned->setName('Dolly'); +echo $cloned->getName() . PHP_EOL; +echo $cloned->getCategory() . PHP_EOL; + +echo 'Remember, you can use the magic method __clone to modify the behaviour of cloning the object!'; diff --git a/examples-per-language/php/creational/SimpleFactory.php b/examples-per-language/php/creational/SimpleFactory.php new file mode 100644 index 00000000..8fd444eb --- /dev/null +++ b/examples-per-language/php/creational/SimpleFactory.php @@ -0,0 +1,9 @@ +getWidth() . PHP_EOL; +echo 'Height: ' . $door->getHeight() . PHP_EOL; diff --git a/examples-per-language/php/creational/Singleton.php b/examples-per-language/php/creational/Singleton.php new file mode 100644 index 00000000..56527767 --- /dev/null +++ b/examples-per-language/php/creational/Singleton.php @@ -0,0 +1,11 @@ +size = $builder->size; + $this->cheese = $builder->cheese; + $this->pepperoni = $builder->pepperoni; + $this->lettuce = $builder->lettuce; + $this->tomato = $builder->tomato; + } +} diff --git a/examples-per-language/php/creational/src/Builder/BurgerBuilder.php b/examples-per-language/php/creational/src/Builder/BurgerBuilder.php new file mode 100644 index 00000000..a5357b7b --- /dev/null +++ b/examples-per-language/php/creational/src/Builder/BurgerBuilder.php @@ -0,0 +1,50 @@ +size = $size; + } + + public function addCheese() + { + $this->cheese = true; + return $this; + } + + public function addPepperoni() + { + $this->pepperoni = true; + return $this; + } + + public function addLettuce() + { + $this->lettuce = true; + return $this; + } + + public function addTomato() + { + $this->tomato = true; + return $this; + } + + // Finishes the build process. + public function build() + { + return new Burger($this); + } + +} diff --git a/examples-per-language/php/creational/src/FactoryMethod/CommunityExecutive.php b/examples-per-language/php/creational/src/FactoryMethod/CommunityExecutive.php new file mode 100644 index 00000000..e6870e55 --- /dev/null +++ b/examples-per-language/php/creational/src/FactoryMethod/CommunityExecutive.php @@ -0,0 +1,13 @@ +makeInterviewer(); + $interviewer->askQuestions(); + } +} diff --git a/examples-per-language/php/creational/src/FactoryMethod/Interviewer.php b/examples-per-language/php/creational/src/FactoryMethod/Interviewer.php new file mode 100644 index 00000000..9a284121 --- /dev/null +++ b/examples-per-language/php/creational/src/FactoryMethod/Interviewer.php @@ -0,0 +1,9 @@ +name = $name; + $this->category = $category; + } + + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @param string $category + */ + public function setCategory($category) + { + $this->category = $category; + } + + public function getCategory() + { + return $this->category; + } + +} diff --git a/examples-per-language/php/creational/src/SimpleFactory/Door.php b/examples-per-language/php/creational/src/SimpleFactory/Door.php new file mode 100644 index 00000000..7281d665 --- /dev/null +++ b/examples-per-language/php/creational/src/SimpleFactory/Door.php @@ -0,0 +1,12 @@ +width = $width; + $this->height = $height; + } + + public function getWidth() + { + return $this->width; + } + + public function getHeight() + { + return $this->height; + } +} diff --git a/examples-per-language/php/creational/src/Singleton/President.php b/examples-per-language/php/creational/src/Singleton/President.php new file mode 100644 index 00000000..3dbcce65 --- /dev/null +++ b/examples-per-language/php/creational/src/Singleton/President.php @@ -0,0 +1,38 @@ +hunt($wildDogAdapter); diff --git a/examples-per-language/php/structural/Bridge.php b/examples-per-language/php/structural/Bridge.php new file mode 100644 index 00000000..f1cbbb79 --- /dev/null +++ b/examples-per-language/php/structural/Bridge.php @@ -0,0 +1,15 @@ +getContent() . PHP_EOL; +echo $careers->getContent() . PHP_EOL; diff --git a/examples-per-language/php/structural/Composite.php b/examples-per-language/php/structural/Composite.php new file mode 100644 index 00000000..3c46654c --- /dev/null +++ b/examples-per-language/php/structural/Composite.php @@ -0,0 +1,17 @@ +addEmployee($john); +$organization->addEmployee($jane); + +echo 'Net salaries ' . $organization->getNetSalaries() . PHP_EOL; diff --git a/examples-per-language/php/structural/Decorator.php b/examples-per-language/php/structural/Decorator.php new file mode 100644 index 00000000..1026f51f --- /dev/null +++ b/examples-per-language/php/structural/Decorator.php @@ -0,0 +1,24 @@ +getCost() . PHP_EOL; +echo $someCoffee->getDescription() . PHP_EOL; + +$someCoffee = new MilkCoffee($someCoffee); +echo $someCoffee->getCost() . PHP_EOL; +echo $someCoffee->getDescription() . PHP_EOL; + +$someCoffee = new WhipCoffee($someCoffee); +echo $someCoffee->getCost() . PHP_EOL; +echo $someCoffee->getDescription() . PHP_EOL; + +$someCoffee = new VanillaCoffee($someCoffee); +echo $someCoffee->getCost() . PHP_EOL; +echo $someCoffee->getDescription() . PHP_EOL; diff --git a/examples-per-language/php/structural/Facade.php b/examples-per-language/php/structural/Facade.php new file mode 100644 index 00000000..cdb26f81 --- /dev/null +++ b/examples-per-language/php/structural/Facade.php @@ -0,0 +1,11 @@ +turnOn(); +$computer->turnOff(); diff --git a/examples-per-language/php/structural/Flyweight.php b/examples-per-language/php/structural/Flyweight.php new file mode 100644 index 00000000..4802e960 --- /dev/null +++ b/examples-per-language/php/structural/Flyweight.php @@ -0,0 +1,17 @@ +takeOrder('less sugar', 1); +$shop->takeOrder('more milk', 2); +$shop->takeOrder('without sugar', 5); +$shop->takeOrder('without sugar', 3); +$shop->takeOrder('less sugar', 3); + +$shop->serve(); diff --git a/examples-per-language/php/structural/Proxy.php b/examples-per-language/php/structural/Proxy.php new file mode 100644 index 00000000..bb611a55 --- /dev/null +++ b/examples-per-language/php/structural/Proxy.php @@ -0,0 +1,12 @@ +open('invalid'); + +$door->open('$ecr@t'); +$door->close(); diff --git a/examples-per-language/php/structural/autoload.php b/examples-per-language/php/structural/autoload.php new file mode 100644 index 00000000..7216f597 --- /dev/null +++ b/examples-per-language/php/structural/autoload.php @@ -0,0 +1,8 @@ +dog = $dog; + } + + public function roar() + { + $this->dog->bark(); + } +} diff --git a/examples-per-language/php/structural/src/Bridge/About.php b/examples-per-language/php/structural/src/Bridge/About.php new file mode 100644 index 00000000..453495d8 --- /dev/null +++ b/examples-per-language/php/structural/src/Bridge/About.php @@ -0,0 +1,19 @@ +theme = $theme; + } + + public function getContent() + { + return 'About page in ' . $this->theme->getColor(); + } +} diff --git a/examples-per-language/php/structural/src/Bridge/AquaTheme.php b/examples-per-language/php/structural/src/Bridge/AquaTheme.php new file mode 100644 index 00000000..ca0aa914 --- /dev/null +++ b/examples-per-language/php/structural/src/Bridge/AquaTheme.php @@ -0,0 +1,13 @@ +theme = $theme; + } + + public function getContent() + { + return 'Careers page in ' . $this->theme->getColor(); + } +} diff --git a/examples-per-language/php/structural/src/Bridge/DarkTheme.php b/examples-per-language/php/structural/src/Bridge/DarkTheme.php new file mode 100644 index 00000000..28c4dd55 --- /dev/null +++ b/examples-per-language/php/structural/src/Bridge/DarkTheme.php @@ -0,0 +1,13 @@ +name = $name; + $this->salary = $salary; + } + + public function getName() + { + return $this->name; + } + + public function setSalary($salary) + { + $this->salary = $salary; + } + + public function getSalary() + { + return $this->salary; + } + + public function getRoles() + { + return $this->roles; + } +} diff --git a/examples-per-language/php/structural/src/Composite/Developer.php b/examples-per-language/php/structural/src/Composite/Developer.php new file mode 100644 index 00000000..6ee5c19c --- /dev/null +++ b/examples-per-language/php/structural/src/Composite/Developer.php @@ -0,0 +1,37 @@ +name = $name; + $this->salary = $salary; + } + + public function getName() + { + return $this->name; + } + + public function setSalary($salary) + { + $this->salary = $salary; + } + + public function getSalary() + { + return $this->salary; + } + + public function getRoles() + { + return $this->roles; + } +} diff --git a/examples-per-language/php/structural/src/Composite/Employee.php b/examples-per-language/php/structural/src/Composite/Employee.php new file mode 100644 index 00000000..a21d03ff --- /dev/null +++ b/examples-per-language/php/structural/src/Composite/Employee.php @@ -0,0 +1,18 @@ +employees[] = $employee; + } + + public function getNetSalaries() + { + $netSalary = 0; + + /** @var Employee $employee */ + foreach ($this->employees as $employee) { + $netSalary += $employee->getSalary(); + } + + return $netSalary; + } + +} diff --git a/examples-per-language/php/structural/src/Decorator/Coffee.php b/examples-per-language/php/structural/src/Decorator/Coffee.php new file mode 100644 index 00000000..b3e5bb4b --- /dev/null +++ b/examples-per-language/php/structural/src/Decorator/Coffee.php @@ -0,0 +1,13 @@ +coffee = $coffee; + } + + public function getCost() + { + return $this->coffee->getCost() + 2; + } + + public function getDescription() + { + return $this->coffee->getDescription() . ', milk'; + } +} diff --git a/examples-per-language/php/structural/src/Decorator/SimpleCoffee.php b/examples-per-language/php/structural/src/Decorator/SimpleCoffee.php new file mode 100644 index 00000000..4bc880da --- /dev/null +++ b/examples-per-language/php/structural/src/Decorator/SimpleCoffee.php @@ -0,0 +1,18 @@ +coffee = $coffee; + } + + public function getCost() + { + return $this->coffee->getCost() + 3; + } + + public function getDescription() + { + return $this->coffee->getDescription() . ', vanilla'; + } +} diff --git a/examples-per-language/php/structural/src/Decorator/WhipCoffee.php b/examples-per-language/php/structural/src/Decorator/WhipCoffee.php new file mode 100644 index 00000000..6b738ced --- /dev/null +++ b/examples-per-language/php/structural/src/Decorator/WhipCoffee.php @@ -0,0 +1,26 @@ +coffee = $coffee; + } + + public function getCost() + { + return $this->coffee->getCost() + 5; + } + + public function getDescription() + { + return $this->coffee->getDescription() . ', whip'; + } +} diff --git a/examples-per-language/php/structural/src/Facade/Computer.php b/examples-per-language/php/structural/src/Facade/Computer.php new file mode 100644 index 00000000..841a8fff --- /dev/null +++ b/examples-per-language/php/structural/src/Facade/Computer.php @@ -0,0 +1,44 @@ +computer = $computer; + } + + public function turnOn() + { + $this->computer->getElectricShock(); + $this->computer->makeSound(); + $this->computer->showLoadingScreen(); + $this->computer->bam(); + } + + public function turnOff() + { + $this->computer->closeEverything(); + $this->computer->pullCurrent(); + $this->computer->sooth(); + } + + +} diff --git a/examples-per-language/php/structural/src/Flyweight/KarakTea.php b/examples-per-language/php/structural/src/Flyweight/KarakTea.php new file mode 100644 index 00000000..906ae8b6 --- /dev/null +++ b/examples-per-language/php/structural/src/Flyweight/KarakTea.php @@ -0,0 +1,10 @@ +availableTea[$preference])) { + $this->availableTea[$preference] = new KarakTea(); + echo 'The tea ' . $preference . ' was not available and was freshly made' . PHP_EOL; + } else { + echo 'The tea ' . $preference . ' was already made, so we served that' . PHP_EOL; + } + + return $this->availableTea[$preference]; + } + +} diff --git a/examples-per-language/php/structural/src/Flyweight/TeaShop.php b/examples-per-language/php/structural/src/Flyweight/TeaShop.php new file mode 100644 index 00000000..0255cc7f --- /dev/null +++ b/examples-per-language/php/structural/src/Flyweight/TeaShop.php @@ -0,0 +1,34 @@ +teaMaker = $teaMaker; + } + + /** + * @param string $teaType + * @param int $table + */ + public function takeOrder($teaType, $table) + { + $this->orders[$table] = $this->teaMaker->make($teaType); + } + + public function serve() + { + foreach ($this->orders as $table => $order) { + echo 'Serving tea to table # ' . $table . PHP_EOL; + } + } + + +} diff --git a/examples-per-language/php/structural/src/Proxy/Door.php b/examples-per-language/php/structural/src/Proxy/Door.php new file mode 100644 index 00000000..6ba48c1d --- /dev/null +++ b/examples-per-language/php/structural/src/Proxy/Door.php @@ -0,0 +1,13 @@ +door = $door; + } + + public function open($password) + { + if ($this->authenticate($password)) { + echo 'Access granted!' . PHP_EOL; + $this->door->open(); + } else { + echo "Big no! It ain't possible!" . PHP_EOL; + } + } + + // In the code examples this is public, but there is no real reason + // why this method should be available to anything except this class. + private function authenticate($password) + { + return $password === '$ecr@t'; + } + + public function close() + { + $this->door->close(); + } + + +}