-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Homework 18. Design patterns #1202
Open
dm1tru
wants to merge
8
commits into
DAleynik/main
Choose a base branch
from
DAleynik/hw16
base: DAleynik/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
803e006
Create .gitignore
dm1tru 9f2c0d1
Update .gitignore
dm1tru d546f31
add code
dm1tru 5da6176
update code
dm1tru f79df76
Update index.php
dm1tru abe763f
update codestyle
dm1tru aab8351
update codestyle
dm1tru 593627a
working on comments
dm1tru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea/ | ||
/vendor/ | ||
.env | ||
/helpers/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
# PHP_2023 | ||
# Homework 16. Design patterns | ||
|
||
https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus | ||
|
||
Разрабатываем часть интернет-ресторана. Продаёт он фаст-фуд. | ||
|
||
1. Абстрактная фабрика будет отвечать за генерацию базового продукта-прототипа: бургер, сэндвич или хот-дог | ||
2. При готовке каждого типа продукта Декоратор будет добавлять составляющие к базовому продукту либо по рецепту, либо по пожеланию клиента (салат, лук, перец и т.д.) | ||
3. Наблюдатель подписывается на статус приготовления и отправляет оповещения о том, что изменился статус приготовления продукта. | ||
4. Адаптер используем для сущности пицца. | ||
5. Строитель будет отвечать за то, что нужно приготовить. | ||
6. Все сущности должны по максимуму генерироваться через DI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "dmitry/hw16", | ||
"type": "project", | ||
"autoload": { | ||
"psr-4": { | ||
"Dmitry\\Hw16\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Dmitry Aleynik" | ||
} | ||
], | ||
"require": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Dmitry\Hw16\App; | ||
|
||
|
||
try { | ||
$app = new App(); | ||
$app->run(); | ||
} catch (\Exception $e) { | ||
throw new \Exception($e->getMessage()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dmitry\Hw16; | ||
|
||
use Dmitry\Hw16\Application\Adapter\PizzaAdapter; | ||
use Dmitry\Hw16\Application\Decorator\OnionDecorator; | ||
use Dmitry\Hw16\Application\Decorator\PepperDecorator; | ||
use Dmitry\Hw16\Application\Decorator\SaladDecorator; | ||
use Dmitry\Hw16\Application\Factory\BurgerFactory; | ||
use Dmitry\Hw16\Application\Factory\ProductFactory; | ||
use Dmitry\Hw16\Application\Publisher\Publisher; | ||
use Dmitry\Hw16\Application\Services\CookingInterface; | ||
use Dmitry\Hw16\Application\Services\CookingService; | ||
|
||
class App | ||
{ | ||
private $useCase; | ||
private CookingInterface $cookingService; | ||
private array $products; | ||
|
||
public function __construct() | ||
{ | ||
$this->useCase = 'Dmitry\Hw16\Application\UseCase\CookingUseCase'; | ||
$this->cookingService = new CookingService(new Publisher()); | ||
} | ||
|
||
public function run(): void | ||
{ | ||
$burger = ProductFactory::makeFood('burger'); | ||
$sandwich = new PepperDecorator(new OnionDecorator(new SaladDecorator(ProductFactory::makeFood('sandwich')))); | ||
$pizza = new PizzaAdapter(); | ||
|
||
$useCase = new $this->useCase(); | ||
$useCase($this->cookingService, $burger, $sandwich, $pizza); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Adapter; | ||
|
||
use Dmitry\Hw16\Domain\Entity\Pizza; | ||
use Dmitry\Hw16\Domain\Entity\ProductInterface; | ||
|
||
class PizzaAdapter implements ProductInterface | ||
{ | ||
private ProductInterface $pizza; | ||
|
||
public function __construct() | ||
{ | ||
$this->createPizza(); | ||
} | ||
|
||
private function createPizza(): void | ||
{ | ||
$this->pizza = new Pizza(); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->pizza->getName(); | ||
} | ||
|
||
public function makeCooked(): void | ||
{ | ||
$this->pizza->makeCooked(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Builder; | ||
|
||
use Dmitry\Hw16\Application\Decorator\OnionDecorator; | ||
use Dmitry\Hw16\Application\Decorator\PepperDecorator; | ||
use Dmitry\Hw16\Application\Decorator\SaladDecorator; | ||
use Dmitry\Hw16\Domain\Entity\ProductInterface; | ||
|
||
class ProductBuilder | ||
{ | ||
private ProductInterface $product; | ||
|
||
public function __construct(ProductInterface $product) | ||
{ | ||
$this->product = $this->product; | ||
} | ||
|
||
|
||
public function addOnion(): self | ||
{ | ||
$this->product = new OnionDecorator($this->product); | ||
return $this; | ||
} | ||
|
||
public function addPepper(): self | ||
{ | ||
$this->product = new PepperDecorator($this->product); | ||
return $this; | ||
} | ||
|
||
|
||
public function addSalad(): self | ||
{ | ||
$this->product = new SaladDecorator($this->product); | ||
return $this; | ||
} | ||
|
||
|
||
public function build(): ProductInterface | ||
{ | ||
return $this->product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Decorator; | ||
|
||
use Dmitry\Hw16\Domain\Entity\ProductInterface; | ||
|
||
class OnionDecorator implements ProductInterface | ||
{ | ||
private $product; | ||
|
||
public function __construct($product) | ||
{ | ||
$this->product = $product; | ||
} | ||
|
||
public function makeCooked(): void | ||
{ | ||
$this->product->makeCooked(); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->product->getName() . ' с луком'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Decorator; | ||
|
||
use Dmitry\Hw16\Domain\Entity\ProductInterface; | ||
|
||
class PepperDecorator implements ProductInterface | ||
{ | ||
private $product; | ||
|
||
public function __construct($product) | ||
{ | ||
$this->product = $product; | ||
} | ||
|
||
public function makeCooked(): void | ||
{ | ||
$this->product->makeCooked(); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->product->getName() . ' с перцем'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Decorator; | ||
|
||
use Dmitry\Hw16\Domain\Entity\ProductInterface; | ||
|
||
class SaladDecorator implements ProductInterface | ||
{ | ||
private $product; | ||
|
||
public function __construct($product) | ||
{ | ||
$this->product = $product; | ||
} | ||
|
||
public function makeCooked(): void | ||
{ | ||
$this->product->makeCooked(); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->product->getName() . ' с салатом'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Factory; | ||
|
||
use Dmitry\Hw16\Domain\Entity\Burger; | ||
use Dmitry\Hw16\Domain\Entity\Product; | ||
use Dmitry\Hw16\Domain\Entity\Sandwich; | ||
use Dmitry\Hw16\Domain\Entity\Hotdog; | ||
|
||
class ProductFactory | ||
{ | ||
private function __construct() | ||
{ | ||
//Method disabled | ||
} | ||
|
||
public static function makeFood(string $type): Product | ||
{ | ||
switch ($type) { | ||
case 'burger': | ||
return new Burger(); | ||
case 'sandwich': | ||
return new Sandwich(); | ||
case 'hotdog': | ||
return new Hotdog(); | ||
default: | ||
throw new \Exception('Неизвестный продукт - ' . $type); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Publisher; | ||
|
||
class Publisher implements PublisherInterface | ||
{ | ||
private $subscribers = []; | ||
|
||
public function subscribe(SubscriberInterface $subscriber): void | ||
{ | ||
$this->subscribers[] = $subscriber; | ||
} | ||
|
||
public function unsubscribe(SubscriberInterface $subscriber): void | ||
{ | ||
// TODO: Implement unsubscribe() method. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нужно реализовать метод. Для этого посмотрите в сторону https://www.php.net/manual/en/function.spl-object-id.php для того, чтобы результат работы данной функции использовать в качестве ключа в |
||
} | ||
|
||
public function notify($product, $status) | ||
{ | ||
var_dump($product->getName() . ' - ' . $status); | ||
foreach ($this->subscribers as $subscriber) { | ||
$subscriber->notify($product, $status); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Publisher; | ||
|
||
use Dmitry\Hw16\Domain\Entity\Product; | ||
use MongoDB\Driver\Monitoring\Subscriber; | ||
|
||
interface PublisherInterface | ||
{ | ||
public function subscribe(SubscriberInterface $subscriber): void; | ||
|
||
public function unsubscribe(SubscriberInterface $subscriber): void; | ||
|
||
public function notify(Product $product, string $status); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Publisher; | ||
|
||
use Dmitry\Hw16\Domain\Entity\Product; | ||
|
||
interface SubscriberInterface | ||
{ | ||
public function notify(Product $product, string $status); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Services; | ||
|
||
use Dmitry\Hw16\Application\Publisher\PublisherInterface; | ||
use Dmitry\Hw16\Domain\Entity\ProductInterface; | ||
|
||
interface CookingInterface | ||
{ | ||
public function __construct(PublisherInterface $publisher); | ||
|
||
public function cook(ProductInterface $product): ProductInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\Services; | ||
|
||
use Dmitry\Hw16\Application\Publisher\PublisherInterface; | ||
use Dmitry\Hw16\Domain\Entity\ProductInterface; | ||
|
||
class CookingService implements CookingInterface | ||
{ | ||
private PublisherInterface $publisher; | ||
|
||
public function __construct(PublisherInterface $publisher) | ||
{ | ||
$this->publisher = $publisher; | ||
} | ||
|
||
public function cook(ProductInterface $product): ProductInterface | ||
{ | ||
$this->publisher->notify($product, 'Отправлен готовиться'); | ||
$product->makeCooked(); | ||
$this->publisher->notify($product, 'Приготовлен'); | ||
return $product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Dmitry\Hw16\Application\UseCase; | ||
|
||
use Dmitry\Hw16\Application\Services\CookingInterface; | ||
use Dmitry\Hw16\Domain\Entity\ProductInterface; | ||
|
||
class CookingUseCase | ||
{ | ||
public function __invoke( | ||
CookingInterface $cookingService, | ||
ProductInterface ...$product | ||
) { | ||
foreach ($product as $item) { | ||
$cookingService->cook($item); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Довольно странное решение, почему бы нам просто сразу не создать use case и использовать его тут?