Skip to content

Commit 5520fef

Browse files
author
Tomáš Votruba
authored
Merge pull request #41 from peter-gribanov/ocp
Open/Closed Principle (OCP)
2 parents 3388621 + b17ca6e commit 5520fef

1 file changed

Lines changed: 49 additions & 32 deletions

File tree

‎README.md‎

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,102 +1022,119 @@ class UserSettings {
10221022
**[⬆ back to top](#table-of-contents)**
10231023

10241024
### Open/Closed Principle (OCP)
1025+
10251026
As stated by Bertrand Meyer, "software entities (classes, modules, functions,
10261027
etc.) should be open for extension, but closed for modification." What does that
10271028
mean though? This principle basically states that you should allow users to
10281029
add new functionalities without changing existing code.
10291030

10301031
**Bad:**
1032+
10311033
```php
1032-
abstract class Adapter {
1034+
abstract class Adapter
1035+
{
10331036
protected $name;
1034-
public function getName() {
1037+
1038+
public function getName()
1039+
{
10351040
return $this->name;
10361041
}
10371042
}
10381043

1039-
class AjaxAdapter extends Adapter {
1040-
public function __construct() {
1044+
class AjaxAdapter extends Adapter
1045+
{
1046+
public function __construct()
1047+
{
10411048
parent::__construct();
10421049
$this->name = 'ajaxAdapter';
10431050
}
10441051
}
10451052

1046-
class NodeAdapter extends Adapter {
1047-
public function __construct() {
1053+
class NodeAdapter extends Adapter
1054+
{
1055+
public function __construct()
1056+
{
10481057
parent::__construct();
10491058
$this->name = 'nodeAdapter';
10501059
}
10511060
}
10521061

1053-
class HttpRequester {
1062+
class HttpRequester
1063+
{
10541064
private $adapter;
1055-
public function __construct($adapter) {
1065+
1066+
public function __construct($adapter)
1067+
{
10561068
$this->adapter = $adapter;
10571069
}
10581070

1059-
public function fetch($url) {
1071+
public function fetch($url)
1072+
{
10601073
$adapterName = $this->adapter->getName();
1074+
10611075
if ($adapterName === 'ajaxAdapter') {
10621076
return $this->makeAjaxCall($url);
1063-
} else if ($adapterName === 'httpNodeAdapter') {
1077+
} elseif ($adapterName === 'httpNodeAdapter') {
10641078
return $this->makeHttpCall($url);
10651079
}
10661080
}
10671081

1068-
protected function makeAjaxCall($url) {
1082+
protected function makeAjaxCall($url)
1083+
{
10691084
// request and return promise
10701085
}
10711086

1072-
protected function makeHttpCall($url) {
1087+
protected function makeHttpCall($url)
1088+
{
10731089
// request and return promise
10741090
}
10751091
}
10761092
```
10771093

10781094
**Good:**
1095+
10791096
```php
1080-
abstract class Adapter {
1081-
abstract protected function getName();
1082-
abstract public function request($url);
1097+
interface Adapter
1098+
{
1099+
public function request($url);
10831100
}
10841101

1085-
class AjaxAdapter extends Adapter {
1086-
protected function getName() {
1087-
return 'ajaxAdapter';
1088-
}
1089-
1090-
public function request($url) {
1102+
class AjaxAdapter implements Adapter
1103+
{
1104+
public function request($url)
1105+
{
10911106
// request and return promise
10921107
}
10931108
}
10941109

1095-
class NodeAdapter extends Adapter {
1096-
protected function getName() {
1097-
return 'nodeAdapter';
1098-
}
1099-
1100-
public function request($url) {
1110+
class NodeAdapter implements Adapter
1111+
{
1112+
public function request($url)
1113+
{
11011114
// request and return promise
11021115
}
11031116
}
11041117

1105-
class HttpRequester {
1118+
class HttpRequester
1119+
{
11061120
private $adapter;
1107-
public function __construct(Adapter $adapter) {
1121+
1122+
public function __construct(Adapter $adapter)
1123+
{
11081124
$this->adapter = $adapter;
11091125
}
11101126

1111-
public function fetch($url) {
1127+
public function fetch($url)
1128+
{
11121129
return $this->adapter->request($url);
11131130
}
11141131
}
1115-
11161132
```
1117-
**[⬆ back to top](#table-of-contents)**
11181133

1134+
**[⬆ back to top](#table-of-contents)**
11191135

11201136
### Liskov Substitution Principle (LSP)
1137+
11211138
This is a scary term for a very simple concept. It's formally defined as "If S
11221139
is a subtype of T, then objects of type T may be replaced with objects of type S
11231140
(i.e., objects of type S may substitute objects of type T) without altering any

0 commit comments

Comments
 (0)