Skip to content

Commit b05f570

Browse files
author
Dominik Liebler
committed
cs FactoryMethod
1 parent 754ea98 commit b05f570

File tree

7 files changed

+51
-61
lines changed

7 files changed

+51
-61
lines changed

FactoryMethod/Bicycle.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
<?php
22

3-
/*
4-
* DesignPatternPHP
5-
*/
6-
73
namespace DesignPatterns\FactoryMethod;
84

95
/**
106
* Bicycle is a bicycle
117
*/
128
class Bicycle implements Vehicle
139
{
10+
/**
11+
* @var string
12+
*/
13+
protected $color;
1414

15+
/**
16+
* sets the color of the bicycle
17+
*
18+
* @param string $rgb
19+
*/
1520
public function setColor($rgb)
1621
{
17-
22+
$this->color = $rgb;
1823
}
19-
20-
}
24+
}

FactoryMethod/FactoryMethod.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<?php
22

3-
/*
4-
* DesignPatternPHP
5-
*/
6-
73
namespace DesignPatterns\FactoryMethod;
84

95
/**
@@ -50,5 +46,4 @@ public function create($type)
5046

5147
return $obj;
5248
}
53-
54-
}
49+
}

FactoryMethod/Ferrari.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
<?php
22

3-
/*
4-
* DesignPatternPHP
5-
*/
6-
73
namespace DesignPatterns\FactoryMethod;
84

95
/**
106
* Ferrari is a italian car
117
*/
128
class Ferrari implements Vehicle
139
{
10+
/**
11+
* @var string
12+
*/
13+
protected $color;
1414

15+
/**
16+
* @param string $rgb
17+
*/
1518
public function setColor($rgb)
1619
{
17-
20+
$this->color = $rgb;
1821
}
19-
20-
}
22+
}

FactoryMethod/GermanFactory.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
<?php
22

3-
/*
4-
* DesignPatternPHP
5-
*/
6-
73
namespace DesignPatterns\FactoryMethod;
84

95
/**
10-
* GermanFactory is vehicle factory in Germany
6+
* GermanFactory is a vehicle factory in Germany
117
*/
128
class GermanFactory extends FactoryMethod
139
{
14-
1510
/**
16-
* @inheritdoc
11+
* {@inheritdoc}
1712
*/
1813
protected function createVehicle($type)
1914
{
2015
switch ($type) {
21-
22-
case parent::CHEAP :
16+
case parent::CHEAP:
2317
return new Bicycle();
2418
break;
25-
26-
case parent::FAST :
19+
case parent::FAST:
2720
$obj = new Porsche();
2821
// we can specialize the way we want some concrete Vehicle since
2922
// we know the class
3023
$obj->addTuningAMG();
24+
3125
return $obj;
3226
break;
33-
3427
default :
3528
throw new \InvalidArgumentException("$type is not a valid vehicle");
3629
}
3730
}
38-
39-
}
31+
}

FactoryMethod/ItalianFactory.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
11
<?php
22

3-
/*
4-
* DesignPatternPHP
5-
*/
6-
73
namespace DesignPatterns\FactoryMethod;
84

95
/**
106
* ItalianFactory is vehicle factory in Italy
117
*/
128
class ItalianFactory extends FactoryMethod
139
{
14-
1510
/**
16-
* @inheritdoc
11+
* {@inheritdoc}
1712
*/
1813
protected function createVehicle($type)
1914
{
2015
switch ($type) {
21-
22-
case parent::CHEAP :
16+
case parent::CHEAP:
2317
return new Bicycle();
2418
break;
25-
26-
case parent::FAST :
19+
case parent::FAST:
2720
return new Ferrari();
2821
break;
29-
3022
default :
3123
throw new \InvalidArgumentException("$type is not a valid vehicle");
3224
}
3325
}
34-
35-
}
26+
}

FactoryMethod/Porsche.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
<?php
22

3-
/*
4-
* DesignPatternPHP
5-
*/
6-
73
namespace DesignPatterns\FactoryMethod;
84

95
/**
106
* Porsche is a german car
117
*/
128
class Porsche implements Vehicle
139
{
10+
/**
11+
* @var string
12+
*/
13+
protected $color;
1414

15+
/**
16+
* @param string $rgb
17+
*/
1518
public function setColor($rgb)
1619
{
17-
20+
$this->color = $rgb;
1821
}
1922

23+
/**
24+
* although tuning by AMG is only offered for Mercedes Cars,
25+
* this is a valid coding example ...
26+
*/
2027
public function addTuningAMG()
2128
{
22-
23-
}
2429

25-
}
30+
}
31+
}

FactoryMethod/Vehicle.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22

3-
/*
4-
* DesignPatternPHP
5-
*/
6-
73
namespace DesignPatterns\FactoryMethod;
84

95
/**
106
* Vehicle is a contract for a vehicle
117
*/
128
interface Vehicle
139
{
14-
10+
/**
11+
* sets the color of the vehicle
12+
*
13+
* @param string $rgb
14+
*/
1515
function setColor($rgb);
16-
}
16+
}

0 commit comments

Comments
 (0)