Skip to content

Commit d0a6b7a

Browse files
committed
Minimalist Decorator Implemented. Object holding other object was interesting to see. :)
1 parent 75f8526 commit d0a6b7a

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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/>&nbsp;&nbsp;<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();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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/>&nbsp;&nbsp; MySQL Database ";
12+
return $this->site->getSite() . $fmat;
13+
}
14+
15+
public function getPrice() {
16+
return 800 + $this->site->getPrice();
17+
}
18+
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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/>&nbsp;&nbsp; Maintenance ";
12+
return $this->site->getSite() . $fmat;
13+
}
14+
15+
public function getPrice() {
16+
return 950 + $this->site->getPrice();
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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/>&nbsp;&nbsp; Video ";
12+
return $this->site->getSite() . $fmat;
13+
}
14+
15+
public function getPrice() {
16+
return 350 + $this->site->getPrice();
17+
}
18+
19+
}

0 commit comments

Comments
 (0)