Skip to content

Commit 2246b01

Browse files
committed
Other example of prototype
1 parent 2fa0341 commit 2246b01

File tree

9 files changed

+219
-215
lines changed

9 files changed

+219
-215
lines changed

SandersW-LearningPHPDesignPatterns/Prototype/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
//Client.php
33
function __autoload($class_name) {
4+
// echo $class_name."<br />";
45
include $class_name . '.php';
56
}
67

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
2-
3-
//MaleProto.php
2+
//FemaleProto.php
43
include_once('IPrototype.php');
54

6-
class MaleProto extends IPrototype {
5+
class FemaleProto extends IPrototype {
76

8-
const gender="MALE";
7+
const gender="FEMALE";
98

10-
public $mated;
9+
public $fecundity;
1110

1211
public function __construct() {
1312
$this->eyeColor="red";
@@ -16,5 +15,4 @@ public function __construct() {
1615
}
1716

1817
function __clone(){}
19-
2018
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
2-
//FemaleProto.php
2+
3+
//MaleProto.php
34
include_once('IPrototype.php');
45

5-
class FemaleProto extends IPrototype {
6+
class MaleProto extends IPrototype {
67

7-
const gender="FEMALE";
8+
const gender="MALE";
89

9-
public $fecundity;
10+
public $mated;
1011

1112
public function __construct() {
1213
$this->eyeColor="red";
@@ -15,4 +16,5 @@ public function __construct() {
1516
}
1617

1718
function __clone(){}
19+
1820
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
//Client.php
3+
function __autoload($class_name) {
4+
include $class_name . '.php';
5+
}
6+
7+
class Client {
8+
9+
private $market;
10+
11+
private $manage;
12+
13+
private $engineer;
14+
15+
public function __construct() {
16+
$this->makeConProto();
17+
$Tess=clone $this->market;
18+
$this->setEmployee($Tess,"Tess Smith",101,"ts101-1234","tess.png");
19+
$this->showEmployee($Tess);
20+
$Jacob=clone $this->market;
21+
$this->setEmployee($Jacob,"Jacob Jones",102,"jj101-2234","jacob.png");
22+
$this->showEmployee($Jacob);
23+
$Ricky=clone $this->manage;
24+
$this->setEmployee($Ricky,"Ricky Rodriguez",203,"rr203-5634","ricky.png");
25+
$this->showEmployee($Ricky);
26+
$Olivia=clone $this->engineer;
27+
$this->setEmployee($Olivia,"Olivia Perez",302,"op301-1278","olivia.png");
28+
$this->showEmployee($Olivia);
29+
$John=clone $this->engineer;
30+
$this->setEmployee($John,"John Jackson",301,"jj302-1454","john.png");
31+
$this->showEmployee($John);
32+
}
33+
34+
private function makeConProto() {
35+
$this->market=new Marketing();
36+
$this->manage=new Management();
37+
$this->engineer=new Engineering();
38+
}
39+
40+
private function showEmployee(IAcmePrototype $employeeNow) {
41+
$px=$employeeNow->getPic();
42+
echo "<img src=$px width='150' height='150'><br/>";
43+
echo $employeeNow->getName() . "<br/>";
44+
echo $employeeNow->getDept() . ": " . $employeeNow::UNIT . "<br/>";
45+
echo $employeeNow->getID() . "<p/>";
46+
}
47+
48+
private function setEmployee(IAcmePrototype $employeeNow,$nm,$dp,$id,$px) {
49+
$employeeNow->setName($nm);
50+
$employeeNow->setDept($dp);
51+
$employeeNow->setID($id);
52+
$employeeNow->setPic("pix/$px");
53+
}
54+
}
55+
56+
$worker = new Client();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
include_once('IAcmePrototype.php');
4+
5+
class Engineering extends IAcmePrototype {
6+
7+
const UNIT="Engineering";
8+
9+
private $development="programming";
10+
11+
private $design="digital artwork";
12+
13+
private $sysAd="system administration";
14+
15+
public function setDept($orgCode) {
16+
switch($orgCode) {
17+
case 301:
18+
$this->dept=$this->development;
19+
break;
20+
21+
case 302:
22+
$this->dept=$this->design;
23+
break;
24+
25+
case 303:
26+
$this->dept=$this->sysAd;
27+
break;
28+
29+
default:
30+
$this->dept="Unrecognized Engineering";
31+
}
32+
}
33+
34+
public function getDept() {
35+
return $this->dept;
36+
}
37+
38+
function __clone(){}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
//IAcmePrototype.php
3+
abstract class IAcmePrototype {
4+
5+
protected $name;
6+
7+
protected $id;
8+
9+
protected $employeePic;
10+
11+
protected $dept;
12+
13+
//Dept
14+
abstract function setDept($orgCode);
15+
16+
abstract function getDept();
17+
18+
//Name
19+
public function setName($emName) {
20+
$this->name=$emName;
21+
}
22+
public function getName() {
23+
return $this->name;
24+
}
25+
//ID
26+
public function setId($emId) {
27+
$this->id=$emId;
28+
}
29+
public function getId() {
30+
return $this->id;
31+
}
32+
//Employee Picture
33+
public function setPic($ePic) {
34+
$this->employeePic=$ePic;
35+
}
36+
37+
public function getPic() {
38+
return $this->employeePic;
39+
}
40+
41+
abstract function __clone();
42+
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
//Management.php
3+
include_once('IAcmePrototype.php');
4+
class Management extends IAcmePrototype {
5+
6+
const UNIT="Management";
7+
8+
private $research="research";
9+
10+
private $plan="planning";
11+
12+
private $operations="operations";
13+
14+
public function setDept($orgCode) {
15+
switch($orgCode) {
16+
case 201:
17+
$this->dept=$this->research;
18+
break;
19+
case 202:
20+
$this->dept=$this->plan;
21+
break;
22+
case 203:
23+
$this->dept=$this->operations;
24+
break;
25+
default:
26+
$this->dept="Unrecognized Management";
27+
}
28+
}
29+
30+
public function getDept() {
31+
return $this->dept;
32+
}
33+
34+
function __clone(){}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
//Marketing.php
3+
include_once('IAcmePrototype.php');
4+
class Marketing extends IAcmePrototype {
5+
6+
const UNIT="Marketing";
7+
8+
private $sales="sales";
9+
10+
private $promotion="promotion";
11+
12+
private $strategic="strategic planning";
13+
14+
public function setDept($orgCode) {
15+
switch($orgCode) {
16+
case 101:
17+
$this->dept=$this->sales;
18+
break;
19+
case 102:
20+
$this->dept=$this->promotion;
21+
break;
22+
case 103:
23+
$this->dept=$this->strategic;
24+
break;
25+
default:
26+
$this->dept="Unrecognized Marketing ";
27+
}
28+
}
29+
30+
public function getDept() {
31+
return $this->dept;
32+
}
33+
34+
function __clone(){}
35+
}

0 commit comments

Comments
 (0)