Created
April 9, 2020 19:59
-
-
Save Kcko/df700ca52d971214571b452d839c8ff3 to your computer and use it in GitHub Desktop.
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
<?php | |
/* | |
Definice | |
Návrhový vzor Command zapouzdřuje jednotlivé požadavky jako objekty. Díky | |
tomu lze parametrizovat jiné objekty s požadavky, přidávat požadavky do fronty | |
a podporovat operace, které je možné provádět zpětně. | |
Pro implementaci tohoto návrhového vzoru jsou nutné následující kroky: | |
1. Definovat rozhraní pro jednotlivé příkazy. | |
2. Vytvořit konkrétní příkazy, které implementují rozhraní z bodu 1 a ve třídě | |
zapouzdřují jednotlivé požadavky. | |
3. Vytvořit způsob, který umožní | |
*/ | |
// BAD | |
interface PublishingProcess { | |
public function publish(Publication $publication); | |
} | |
class PrintedBook implements PublishingProcess { | |
public function publish(Publication $publication) { | |
// provedení jednotlivých úkonů | |
} | |
} | |
class EBook implements PublishingProcess { | |
public function publish(Publication $publication) { | |
// provedení jednotlivých úkonů | |
} | |
} | |
// GOOD | |
interface BookPublishingCommand | |
{ | |
public function execute(Publication $publication); | |
} | |
class CommandManuscript implements BookPublishingCommand | |
{ | |
public function execute(Publication $publication) | |
{ | |
printf( | |
"Obdržen rukopis publikace z kategorie %s, %d stran.\n", | |
$publication->getCategory(), | |
$publication->getPageCount() | |
); | |
} | |
} | |
class CommandTranslation implements BookPublishingCommand | |
{ | |
public function execute(Publication $publication) | |
{ | |
printf( | |
"Překlad publikace z kategorie %s, %d stran.\n", | |
$publication->getCategory(), | |
$publication->getPageCount() | |
); | |
} | |
} | |
class CommandProofreading implements BookPublishingCommand | |
{ | |
public function execute(Publication $publication) | |
{ | |
printf( | |
"Jazyková korektura publikace " . "z kategorie %s, %d stran.\n", | |
$publication->getCategory(), | |
$publication->getPageCount() | |
); | |
} | |
} | |
class CommandBookCover implements BookPublishingCommand | |
{ | |
public function execute(Publication $publication) | |
{ | |
printf( | |
"Příprava obálky pro publikaci " . "z kategorie %s, %d stran.\n", | |
$publication->getCategory(), | |
$publication->getPageCount() | |
); | |
} | |
} | |
class CommandAssignISBN implements BookPublishingCommand | |
{ | |
public function execute(Publication $publication) | |
{ | |
printf( | |
"Přiřazení ISBN pro publikaci " . "z kategorie %s, %d stran.\n", | |
$publication->getCategory(), | |
$publication->getPageCount() | |
); | |
} | |
} | |
class CommandPrint implements BookPublishingCommand | |
{ | |
public function execute(Publication $publication) | |
{ | |
printf( | |
"Tisk publikace z kategorie %s, %d stran.\n", | |
$publication->getCategory(), | |
$publication->getPageCount() | |
); | |
} | |
} | |
class CommandPrintToPdf implements BookPublishingCommand | |
{ | |
public function execute(Publication $publication) | |
{ | |
printf( | |
"Tisk publikace z kategorie %s, " . "%d stran do PDF souboru.\n", | |
$publication->getCategory(), | |
$publication->getPageCount() | |
); | |
} | |
} | |
class Publisher | |
{ | |
protected $processes = array(); | |
public function addProcess($name, $commands) | |
{ | |
$this->processes[$name] = $commands; | |
} | |
public function publish($process, Publication $publication) | |
{ | |
if (!isset($this->processes[$process])) { | |
throw new BookPublishingException( | |
"Proces '" . $process . "' neexistuje." | |
); | |
} | |
printf("Zahájen proces: %s.\n", $process); | |
foreach ($this->processes[$process] as $command) { | |
$command->execute($publication); | |
} | |
} | |
} | |
// USAGE | |
$printedBookProcess = array( | |
new CommandManuscript(), | |
new CommandProofreading(), | |
new CommandBookCover(), | |
new CommandAssignISBN(), | |
new CommandPrint() | |
); | |
$eBookProcess = array( | |
new CommandTranslation(), | |
new CommandProofreading(), | |
new CommandBookCover(), | |
new CommandPrintToPdf() | |
); | |
$publisher = new Publisher(); | |
$publisher->addProcess('printedBook', $printedBookProcess); | |
$publisher->addProcess('eBook', $eBookProcess); | |
$book1 = new Book('PC', 450); | |
$book2 = new Journal('MEDICINA', 50); | |
$publisher->publish('printedBook', $book1); | |
$publisher->publish('eBook', $book2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment