Milan Hradil, Nirdosh
14.08.2015
Wish list
loose coupling
code reuse (is not the same as copy/paste)
open for extension and closed for modification
encapsulate what varies
single responsibility principle
program against an interface not against an implementation (DI)
prefer composition over inheritance
(in fact this is all the same with different words)
Avoid tight coupling!
Otherwise you get into trouble
Pattern groups
Creational
Structural
Behavioral
Template Method
Behavioral type
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Template Method
Template Method
abstract class Worker {
public function DailyRoutine() {
$this->getUp();
$this->eatBreakfast();
$this->goToWork();
$this->work();
$this->returnToHome();
$this->relax();
$this->sleep();
}
public function getUp() {}
public function eatBreakfast() {}
public function goToWork() {}
abstract public function work();
public function returnToHome() {}
public function relax() {}
public function sleep() {}
}
class FireFighter extends Worker {
public function work() {
echo "Fire fighting";
}
}
class Manager extends Worker {
public function work() {
echo "People managing";
}
}
Hands on ...
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality...
Book : Design patterns - Elements of Reusable Object-Oriented Software
Inheritance Approach - Class Exlposion
Design pattern approach - Decorator