File tree Expand file tree Collapse file tree 7 files changed +126
-0
lines changed
SandersW-LearningPHPDesignPatterns/decorator/minimalist_decorator Expand file tree Collapse file tree 7 files changed +126
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ //BasicSite.php
3+ //Concrete Component
4+ class BasicSite extends IComponent {
5+
6+ public function __construct () {
7+ $ this ->site ="Basic Site " ;
8+ }
9+
10+ public function getSite () {
11+ return $ this ->site ;
12+ }
13+
14+ public function getPrice () {
15+ return 1200 ;
16+ }
17+
18+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ //Client.php
3+
4+ function __autoload ($ class_name ) {
5+ // echo $class_name."\n";
6+ include $ class_name . '.php ' ;
7+ }
8+
9+ class Client {
10+ private $ basicSite ;
11+
12+ public function __construct () {
13+ $ this ->basicSite =new BasicSite ();
14+ $ this ->basicSite =$ this ->wrapComponent ($ this ->basicSite );
15+
16+ $ siteShow =$ this ->basicSite ->getSite ();
17+ $ format ="<br/> <strong>Total= $ " ;
18+
19+ $ price =$ this ->basicSite ->getPrice ();
20+ echo $ siteShow . $ format . $ price . "</strong><p/> " ;
21+ }
22+
23+ private function wrapComponent (IComponent $ component ) {
24+ $ component =new Maintenance ($ component );
25+ $ component =new Video ($ component );
26+ $ component =new DataBase ($ component );
27+ return $ component ;
28+ }
29+ }
30+
31+ $ worker =new Client ();
Original file line number Diff line number Diff line change 1+ <?php
2+ //DataBase.php
3+ //Concrete decorator
4+ class DataBase extends Decorator {
5+
6+ public function __construct (IComponent $ siteNow ) {
7+ $ this ->site = $ siteNow ;
8+ }
9+
10+ public function getSite () {
11+ $ fmat ="<br/> MySQL Database " ;
12+ return $ this ->site ->getSite () . $ fmat ;
13+ }
14+
15+ public function getPrice () {
16+ return 800 + $ this ->site ->getPrice ();
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ //Decorator.php
3+ //Decorator participant
4+ abstract class Decorator extends IComponent
5+ {
6+ //Inherits both getSite() and getPrice()
7+ //This is still an abstract class and there's
8+ //no need to implement either abstract method here
9+ //Job is to maintain reference to Component
10+ //public function getSite() { }
11+ //public function getPrice() { }
12+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ //IComponent.php
3+ //Component interface
4+ abstract class IComponent
5+ {
6+ protected $ site ;
7+ abstract public function getSite ();
8+ abstract public function getPrice ();
9+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ //Maintenance.php
3+ //Concrete decorator
4+ class Maintenance extends Decorator {
5+
6+ public function __construct (IComponent $ siteNow ) {
7+ $ this ->site = $ siteNow ;
8+ }
9+
10+ public function getSite () {
11+ $ fmat ="<br/> Maintenance " ;
12+ return $ this ->site ->getSite () . $ fmat ;
13+ }
14+
15+ public function getPrice () {
16+ return 950 + $ this ->site ->getPrice ();
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ //Video.php
3+ //Concrete decorator
4+ class Video extends IComponent {
5+
6+ public function __construct (IComponent $ siteNow ) {
7+ $ this ->site = $ siteNow ;
8+ }
9+
10+ public function getSite () {
11+ $ fmat ="<br/> Video " ;
12+ return $ this ->site ->getSite () . $ fmat ;
13+ }
14+
15+ public function getPrice () {
16+ return 350 + $ this ->site ->getPrice ();
17+ }
18+
19+ }
You can’t perform that action at this time.
0 commit comments