Skip to content

Commit 7f22362

Browse files
committed
Finished example 2 in js
1 parent 0048865 commit 7f22362

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

README.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,60 +123,59 @@ Wikipedia says
123123

124124
Taking our hiring manager example above. First of all we have an interviewer interface and some implementations for it
125125

126-
```php
127-
interface Interviewer {
128-
public function askQuestions();
129-
}
126+
```js
127+
/*
128+
Interviewer interface
130129
131-
class Developer implements Interviewer {
132-
public function askQuestions() {
133-
echo 'Asking about design patterns!';
134-
}
130+
askQuestions()
131+
*/
132+
133+
class Developer {
134+
askQuestions() {
135+
console.log('Asking about design patterns!')
136+
}
135137
}
136138

137-
class CommunityExecutive implements Interviewer {
138-
public function askQuestions() {
139-
echo 'Asking about community building';
140-
}
139+
class CommunityExecutive {
140+
askQuestions() {
141+
console.log('Asking about community building')
142+
}
141143
}
142144
```
143145

144146
Now let us create our `HiringManager`
145147

146-
```php
147-
abstract class HiringManager {
148-
149-
// Factory method
150-
abstract public function makeInterviewer() : Interviewer;
151-
152-
public function takeInterview() {
153-
$interviewer = $this->makeInterviewer();
154-
$interviewer->askQuestions();
148+
```js
149+
class HiringManager {
150+
151+
takeInterview() {
152+
const interviewer = this.makeInterviewer()
153+
interviewer.askQuestions()
155154
}
156155
}
157156
```
158157
Now any child can extend it and provide the required interviewer
159-
```php
158+
```js
160159
class DevelopmentManager extends HiringManager {
161-
public function makeInterviewer() : Interviewer {
160+
makeInterviewer() {
162161
return new Developer();
163162
}
164163
}
165164

166165
class MarketingManager extends HiringManager {
167-
public function makeInterviewer() : Interviewer {
166+
makeInterviewer() {
168167
return new CommunityExecutive();
169168
}
170169
}
171170
```
172171
and then it can be used as
173172

174-
```php
175-
$devManager = new DevelopmentManager();
176-
$devManager->takeInterview(); // Output: Asking about design patterns
173+
```js
174+
const devManager = new DevelopmentManager()
175+
devManager.takeInterview() // Output: Asking about design patterns
177176

178-
$marketingManager = new MarketingManager();
179-
$marketingManager->takeInterview(); // Output: Asking about community building.
177+
const marketingManager = new MarketingManager()
178+
marketingManager.takeInterview() // Output: Asking about community buildng.
180179
```
181180

182181
**When to use?**

0 commit comments

Comments
 (0)