-
Notifications
You must be signed in to change notification settings - Fork 0
/
task-multiple-inheritance.php
118 lines (111 loc) · 4.21 KB
/
task-multiple-inheritance.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Multiple inheritance in PHP
*
* Problem: in our class hierarchy we have implementation of a method that we
* would like to re-use in another class, that does not belong to that
* hierarchy and we can't extend one class from another, what other options do
* we have here?
*
* Given abstraction is not fully correct from biological point of view,
* but it should represent the problem of multiple-inheritance quite well.
* Please accept and follow the suggested abstraction and stick to common
* sense understanding of animals, humans and machines.
*
* You are free to adjust all classes and also define new ones, as long as
* public interfaces of the classes that are provided in the original task
* description stay as they are.
*
* For the sake of simplicity all classes and interfaces are described within
* one file, i.e. this file, please keep all changes within this file.
*/
interface IsWalking
{
public function walk();
}
abstract class Mammal implements IsWalking
{
protected $heart;
protected $bloodVesselSystem;
//...
abstract public function walk()
{
}
}
class Human extends Mammal implements IsWalking
{
// ...
private $muscleQuadriceps;
private $muscleLegBiceps;
// ...
public function walk()
{
while (/* target is not reached */) {
/* implementation for the sake of example */
$this->muscleQuadriceps->contract();
sleep(500);
$this->muscleQuadriceps->relax();
$this->muscleLegBiceps->contract();
sleep(500);
$this->muscleLegBiceps->relax();
}
}
}
abstract class Machine
{
protected $energySource;
protected $energyTransportSystem;
// ...
}
class HumanoidRobot extends Machine implements IsWalking
{
// ...
private $muscleQuadriceps;
private $muscleLegBiceps;
// ...
public function walk()
{
/**
* TODO: humanoid robot is supposed to walk like Human.
* for the sake of simplicity let's assume that in our case it means
* that the code of the HumanoidRobot::walk() method should be the same
* as Human::walk() method.
*
* What kind of class hierarchy/structure should we implement here to
* avoid duplication of the walk() method implementation between
* Human::walk() and HumanoidRobot::walk()
*
* You are allowed to refactor implementation of all classes presented
* here as long as they stay compatible with any code that might be
* using them now. In other words: public interfaces of all classes
* should stay like they are now, implementation details may be changed
* without any limitation.
*/
}
}
/**
* Possibly more illustrative visual representation of the problem:
*
*
* |====================|
* |INTERFACE: IsWalking|
* |====================|
* /| walk() |
* / |====================|
* / / \
* |--------| / \ |---------------|
* | Mammal | / \ | Machine |
* |--------| / \ |---------------|
* | | / \ | |
* /-------/ | / \ | \----------\
* | | / \ | |
* |--------| |--------| |---------------| |-------|
* | Dog | | Human | | HumanoidRobot | | Drill |
* |--------| |--------| |---------------| |-------|
* | walk() | | walk() | | walk() |
* |--------| |--------| |---------------|
*
* Dog::walk() will be a different implementation of the walk-method comparing
* to the Human::walk(), at the same time HumanoidRobot::walk() is expected to
* be exactly the same one as the Human::walk(). Drill has no walk() method.
*/