Skip to content

Commit 8917430

Browse files
committed
refactor prototype pattern and move to its sub acme folder
1 parent ce8fd08 commit 8917430

File tree

10 files changed

+85
-53
lines changed

10 files changed

+85
-53
lines changed

app/Controller/CreationalController.php

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@
33
namespace App\Controller;
44

55
use App\Patterns\Creational\Pool\Worker\WorkerPool;
6-
use App\Patterns\Creational\FactoryMethod\CarManufacturer\GermanFactory;
7-
use App\Patterns\Creational\Prototype\IAcmePrototype;
8-
use App\Patterns\Creational\Prototype\Marketing;
9-
use App\Patterns\Creational\Prototype\Engineering;
10-
use App\Patterns\Creational\Prototype\Management;
116
use App\Patterns\Creational\SimpleFactory\SimpleFactory;
12-
use App\Patterns\Creational\FactoryMethod\Logger\{
13-
FileLoggerFactory,
14-
StdLoggerFactory
15-
};
167

178
class CreationalController
189
{
@@ -85,45 +76,9 @@ public function reverseString()
8576
* @param null
8677
* @return null
8778
*/
88-
public function prototype()
79+
public function acmePrototype()
8980
{
90-
$marketing = new Marketing();
91-
$management = new Management();
92-
$engineering = new Engineering();
93-
94-
$smith = clone $marketing;
95-
$this->setEmployee( $smith, "Tess Smith", 101, "ts101-1234");
96-
$this->showEmployee($smith);
97-
98-
$jacob = clone $marketing;
99-
$this->setEmployee( $jacob,"Jacob Jones", 102,"jj101-2234");
100-
$this->showEmployee($jacob);
101-
102-
$ricky = clone $management;
103-
$this->setEmployee($ricky,"Ricky Rodriguez", 203,"rr203-5634");
104-
$this->showEmployee($ricky);
105-
106-
$livia = clone $engineering;
107-
$this->setEmployee($livia,"Olivia Perez", 302,"op301-1278");
108-
$this->showEmployee($livia);
109-
110-
$jhon = clone $engineering;
111-
$this->setEmployee($jhon,"John Jackson", 301, "jj302-1454");
112-
$this->showEmployee($jhon);
113-
}
114-
115-
private function showEmployee(IAcmePrototype $employee)
116-
{
117-
echo $employee->getName() . "<br/>";
118-
echo $employee->getDept() . ": " . $employee::UNIT . "<br/>";
119-
echo $employee->getID() . "<p/>";
120-
}
121-
122-
private function setEmployee(IAcmePrototype $employee,$nm,$dp,$id)
123-
{
124-
$employee->setName($nm);
125-
$employee->setDept($dp);
126-
$employee->setID($id);
81+
(new \App\Patterns\Creational\Prototype\AcmePrototype\Application)->render();
12782
}
12883

12984
public function simpleFactory()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php namespace App\Patterns\Creational\Prototype\AcmePrototype;
2+
3+
class Application
4+
{
5+
public function render()
6+
{
7+
$marketing = new Marketing();
8+
$management = new Management();
9+
$engineering = new Engineering();
10+
11+
$smith = clone $marketing;
12+
$this->setEmployee( $smith, "Tess Smith", 101, "ts101-1234");
13+
$this->showEmployee($smith);
14+
15+
$jacob = clone $marketing;
16+
$this->setEmployee( $jacob,"Jacob Jones", 102,"jj101-2234");
17+
$this->showEmployee($jacob);
18+
19+
$ricky = clone $management;
20+
$this->setEmployee($ricky,"Ricky Rodriguez", 203,"rr203-5634");
21+
$this->showEmployee($ricky);
22+
23+
$livia = clone $engineering;
24+
$this->setEmployee($livia,"Olivia Perez", 302,"op301-1278");
25+
$this->showEmployee($livia);
26+
27+
$jhon = clone $engineering;
28+
$this->setEmployee($jhon,"John Jackson", 301, "jj302-1454");
29+
$this->showEmployee($jhon);
30+
}
31+
32+
private function showEmployee(IAcmePrototype $employee)
33+
{
34+
echo $employee->getName() . "<br/>";
35+
echo $employee->getDept() . ": " . $employee::UNIT . "<br/>";
36+
echo $employee->getID() . "<p/>";
37+
}
38+
39+
private function setEmployee(IAcmePrototype $employee,$nm,$dp,$id)
40+
{
41+
$employee->setName($nm);
42+
$employee->setDept($dp);
43+
$employee->setID($id);
44+
}
45+
}

app/Patterns/Creational/Prototype/Engineering.php renamed to app/Patterns/Creational/Prototype/AcmePrototype/Engineering.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace App\Patterns\Creational\Prototype;
1+
<?php namespace App\Patterns\Creational\Prototype\AcmePrototype;
22

33
class Engineering extends IAcmePrototype
44
{
@@ -33,5 +33,5 @@ public function getDept() : string
3333
return $this->dept;
3434
}
3535

36-
public function __clone(){}
36+
public function __clone() {}
3737
}

app/Patterns/Creational/Prototype/IAcmePrototype.php renamed to app/Patterns/Creational/Prototype/AcmePrototype/IAcmePrototype.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace App\Patterns\Creational\Prototype;
1+
<?php namespace App\Patterns\Creational\Prototype\AcmePrototype;
22

33
abstract class IAcmePrototype {
44
/**

app/Patterns/Creational/Prototype/Management.php renamed to app/Patterns/Creational/Prototype/AcmePrototype/Management.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace App\Patterns\Creational\Prototype;
1+
<?php namespace App\Patterns\Creational\Prototype\AcmePrototype;
22

33
class Management extends IAcmePrototype
44
{

app/Patterns/Creational/Prototype/Marketing.php renamed to app/Patterns/Creational/Prototype/AcmePrototype/Marketing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace App\Patterns\Creational\Prototype;
1+
<?php namespace App\Patterns\Creational\Prototype\AcmePrototype;
22

33
class Marketing extends IAcmePrototype {
44
const UNIT = "Marketing";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Acme Prototype
2+
![Illustration](./illustration.png)
Loading

bootstraps/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
['GET', '/car-manufacturer-factory-method', ['App\Controller\CreationalController', 'carManufactureFactoryMethod']],
99
['GET', '/logger-factory-method', ['App\Controller\CreationalController', 'loggerFactoryMethod']],
1010
['GET', '/pool-reverse-string', ['App\Controller\CreationalController', 'reverseString']],
11-
['GET', '/proto', ['App\Controller\CreationalController', 'prototype']],
11+
['GET', '/prototype-acme', ['App\Controller\CreationalController', 'acmePrototype']],
1212
['GET', '/simple', ['App\Controller\CreationalController', 'simpleFactory']],
1313
['GET', '/static', ['App\Controller\CreationalController', 'staticFactory']],
1414
['GET', '/structural', ['App\Controller\StructuralController', 'adapterPattern']],

tests/AcmePrototypeTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
use App\Patterns\Creational\Prototype\AcmePrototype\IAcmePrototype;
6+
use App\Patterns\Creational\Prototype\AcmePrototype\Marketing;
7+
8+
class AcmePrototypeTest extends TestCase {
9+
10+
public function testShouldCloneProperly()
11+
{
12+
$marketing = new Marketing();
13+
14+
$smith = clone $marketing;
15+
$this->setEmployee( $smith, "Tess Smith", 101, "ts101-1234");
16+
17+
$jacob = clone $marketing;
18+
$this->setEmployee( $jacob,"Jacob Jones", 102,"jj101-2234");
19+
20+
$this->assertEquals("Jacob Jones", $jacob->getName());
21+
$this->assertEquals("Tess Smith", $smith->getName());
22+
}
23+
24+
private function setEmployee(IAcmePrototype $employee,$nm,$dp,$id)
25+
{
26+
$employee->setName($nm);
27+
$employee->setDept($dp);
28+
$employee->setID($id);
29+
}
30+
}

0 commit comments

Comments
 (0)