Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created April 9, 2020 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/40f60c5b8fa1da1450870c63ee5976b3 to your computer and use it in GitHub Desktop.
Save Kcko/40f60c5b8fa1da1450870c63ee5976b3 to your computer and use it in GitHub Desktop.
<?php
/*
Návrhový vzor Template Method definuje rozhraní algoritmu v metodě a přenechává
implementaci jednotlivých kroků potomkům třídy. Ti mohou jednotlivé části
algoritmu modifikovat, aniž by změnili jeho strukturu.
*/
abstract class AbstractPrinter implements Printer, Observable
{
// ... atributy a metody původní třídy LaserPrinter
protected $paperAmount = 150;
abstract protected function replaceCartridges();
abstract protected function checkPrintingParts();
abstract protected function isLowPaperAmount();
protected function addPaper()
{
printf(
"Doplnění %d listů do zásobníku papíru.\n",
1000 - $this->paperAmount
);
$this->paperAmount = 1000;
}
final public function check()
{
printf("Probíhá kontola tiskárny %s:\n", $this->type);
$this->replaceCartridges();
$this->checkPrintingParts();
if ($this->isLowPaperAmount()) {
$this->addPaper();
} else {
print "Dostatečné množství papíru.\n";
}
}
}
class LaserPrinter extends AbstractPrinter
{
protected function replaceCartridges()
{
print "Výměna toneru typu AAA-111.\n";
}
protected function checkPrintingParts()
{
print "Kontrola fotoválce.\n";
}
protected function isLowPaperAmount()
{
if ($this->paperAmount < 200) {
return true;
}
return false;
}
}
class InkjetPrinter extends AbstractPrinter
{
protected function replaceCartridges()
{
print "Výměna inkoustových náplní typu BBB-222.\n";
}
protected function checkPrintingParts()
{
print "Kontrola trysek.\n";
}
protected function isLowPaperAmount()
{
if ($this->paperAmount < 100) {
return true;
}
return false;
}
}
$laserPrinter = new LaserPrinter('LP-1');
$inkjetPrinter = new InkjetPrinter('IP-1');
$laserPrinter->check();
$inkjetPrinter->check();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment