Skip to content

Commit ab0f5b9

Browse files
committed
Decorator with multiple components complete.
1 parent d0a6b7a commit ab0f5b9

File tree

8 files changed

+198
-0
lines changed

8 files changed

+198
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
//Client.php
3+
/*Age groups:
4+
18-29: Group 1
5+
30-39: Group 2
6+
40-49: Group 3
7+
50+ : Group 4
8+
*/
9+
function __autoload($class_name)
10+
{
11+
include $class_name . '.php';
12+
}
13+
class Client
14+
{
15+
//$hotDate is component instance
16+
private $hotDate;
17+
public function __construct()
18+
{
19+
$this->hotDate=new Female();
20+
$this->hotDate->setAge("Age Group 4");
21+
echo $this->hotDate->getAge();
22+
$this->hotDate=$this->wrapComponent($this->hotDate);
23+
echo $this->hotDate->getFeature();
24+
}
25+
private function wrapComponent(IComponent $component)
26+
{
27+
$component=new ProgramLang($component);
28+
$component->setFeature("php");
29+
$component=new Hardware($component);
30+
$component->setFeature("lin");
31+
$component=new Food($component);
32+
$component->setFeature("veg");
33+
//var_dump($component); die;
34+
return $component;
35+
}
36+
}
37+
$worker=new Client();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
//Decorator.php
3+
//Decorator participant
4+
abstract class Decorator extends IComponent
5+
{
6+
public function setAge($ageNow)
7+
{
8+
$this->ageGroup=$this->ageGroup;
9+
}
10+
public function getAge()
11+
{
12+
return $this->ageGroup;
13+
}
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
//Female.php
3+
//Female Concrete Component
4+
class Female extends IComponent
5+
{
6+
public function __construct()
7+
{
8+
$this->date="Female";
9+
$this->setFeature("<br />Grrrl programmer features: ");
10+
}
11+
public function getAge()
12+
{
13+
return $this->ageGroup;
14+
}
15+
public function setAge($ageNow)
16+
{
17+
$this->ageGroup=$ageNow;
18+
}
19+
public function getFeature()
20+
{
21+
return $this->feature;
22+
}
23+
public function setFeature($fea)
24+
{
25+
$this->feature=$fea;
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
//Food.php
3+
//Concrete decorator
4+
class Food extends Decorator
5+
{
6+
private $chowNow;
7+
public function __construct(IComponent $dateNow)
8+
{
9+
$this->date = $dateNow;
10+
}
11+
private $snacks=array("piz"=>"Pizza",
12+
"burg"=>"Burgers",
13+
"nach"=>"Nachos",
14+
"veg"=>"Veggies");
15+
public function setFeature($yum)
16+
{
17+
$this->chowNow=$this->snacks[$yum];
18+
}
19+
public function getFeature()
20+
{
21+
$output=$this->date->getFeature();
22+
$fmat="<br/>&nbsp;&nbsp;";
23+
$output .="$fmat Favorite food: ";
24+
$output .= $this->chowNow . "<br/>";
25+
return $output;
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
//Hardware.php
3+
//Concrete decorator
4+
class Hardware extends Decorator
5+
{
6+
private $hardwareNow;
7+
public function __construct(IComponent $dateNow)
8+
{
9+
$this->date = $dateNow;
10+
}
11+
private $box=array("mac"=>"Macintosh",
12+
"dell"=>"Dell",
13+
"hp"=>"Hewlett-Packard",
14+
"lin"=>"Linux");
15+
public function setFeature($hdw)
16+
{
17+
$this->hardwareNow=$this->box[$hdw];
18+
}
19+
public function getFeature()
20+
{
21+
$output=$this->date->getFeature();
22+
$fmat="<br/>&nbsp;&nbsp;";
23+
$output .="$fmat Current Hardware: ";
24+
$output .= $this->hardwareNow;
25+
return $output;
26+
}
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
//IComponent.php
3+
//Component interface
4+
abstract class IComponent
5+
{
6+
protected $date;
7+
protected $ageGroup;
8+
protected $feature;
9+
abstract public function setAge($ageNow);
10+
abstract public function getAge();
11+
abstract public function getFeature();
12+
abstract public function setFeature($fea);
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
//Male.php
3+
//Male Concrete Component
4+
class Male extends IComponent
5+
{
6+
public function __construct()
7+
{
8+
$this->date="Male";
9+
$this->setFeature("<br/>Dude programmer features: ");
10+
}
11+
public function getAge()
12+
{
13+
return $this->ageGroup;
14+
}
15+
public function setAge($ageNow)
16+
{
17+
$this->ageGroup=$ageNow;
18+
}
19+
public function getFeature()
20+
{
21+
return $this->feature;
22+
}
23+
public function setFeature($fea)
24+
{
25+
$this->feature=$fea;
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
//ProgramLang.php
3+
//Concrete decorator
4+
class ProgramLang extends Decorator
5+
{
6+
private $languageNow;
7+
public function __construct(IComponent $dateNow)
8+
{
9+
$this->date = $dateNow;
10+
}
11+
private $language=array("php"=>"PHP",
12+
"cs"=>"C#",
13+
"js"=>"JavaScript","as3"=>"ActionScript 3.0");
14+
public function setFeature($lan)
15+
{
16+
$this->languageNow=$this->language[$lan];
17+
}
18+
public function getFeature()
19+
{
20+
$output=$this->date->getFeature();
21+
$fmat="<br/>&nbsp;&nbsp;";
22+
$output .="$fmat Preferred programming language: ";
23+
$output .= $this->languageNow;
24+
return $output;
25+
}
26+
}

0 commit comments

Comments
 (0)