-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsp_post-conditions.php
88 lines (72 loc) · 2.87 KB
/
lsp_post-conditions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
// Let's consider a theme of "Vehicle". We can start by defining an interface VehicleInterface
// that specifies the basic behavior of a vehicle:
interface VehicleInterface
{
public function start(): void;
public function stop(): void;
public function accelerate(float $speed): void;
public function brake(): void;
}
// Next, we can define an abstract class AbstractVehicle that implements the VehicleInterface
// and provides default implementations for some methods:
abstract class AbstractVehicle implements VehicleInterface {
public function start(): void {
// default implementation
}
public function stop(): void {
// default implementation
}
public function brake(): void {
// default implementation
}
}
// Now let's consider the principle of "Post-conditions cannot be weakened in a subtype, but they can be strengthened."
// This means that any class that implements the VehicleInterface should not weaken the post-conditions of its methods,
// but it can strengthen them.
//
//For example, let's say we have a class Car that extends AbstractVehicle:
class Car extends AbstractVehicle {
private bool $engineStarted = false;
private float $speed = 0;
public function start(): void {
parent::start();
$this->engineStarted = true;
}
public function stop(): void {
parent::stop();
$this->engineStarted = false;
$this->speed = 0;
}
public function accelerate(float $speed): void {
$this->speed += $speed;
}
public function brake(): void {
$this->speed = 0;
}
public function getCurrentSpeed(): float {
return $this->speed;
}
}
// Notice that the Car class strengthens the post-condition of the getCurrentSpeed() method
// by specifying that it returns a float.
// This is a valid use of the LSP, since we are not weakening any post-conditions.
//
//Finally, let's ensure that our implementation conforms to all the SOLID principles.
//
//Single Responsibility Principle (SRP):
// The VehicleInterface and AbstractVehicle classes have a single responsibility of defining the behavior of a vehicle.
//
//Open/Closed Principle (OCP):
// The VehicleInterface and AbstractVehicle classes are open for extension (by allowing new classes to implement them)
// but closed for modification (by not changing their existing implementation).
//
//Liskov Substitution Principle (LSP):
// The Car class extends AbstractVehicle and implements VehicleInterface without weakening any post-conditions.
//
//Interface Segregation Principle (ISP):
// The VehicleInterface defines only the methods that are necessary for a vehicle, and any class that implements
// it must implement all the methods.
//
//Dependency Inversion Principle (DIP):
// The Car class depends on abstractions (AbstractVehicle and VehicleInterface) rather than concrete implementations.