Skip to content

Commit 99f2a2b

Browse files
author
Badacadabra
committed
Add Facade (ES5 + ES6 + CoffeeScript)
1 parent 5c53088 commit 99f2a2b

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed

doc/GoF/Structural/Facade/Facade.dia

2.11 KB
Binary file not shown.

doc/GoF/Structural/Facade/Facade.png

15.3 KB
Loading

doc/GoF/Structural/Facade/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Synopsis
2+
3+
I love my pets but, due to Alzheimer's disease, I often forget how to feed them. Any help would be appreciated.
4+
5+
# Problem
6+
7+
The more pets you have, the more difficult it will be to remember the menu for each animal. Complexity can grow quite fast, which is not ideal in terms of code (re)usability. We should keep it simple, stupid (KISS).
8+
9+
# Solution
10+
11+
Facade is often used to hide the underlying complexity of a system. To implement this design pattern, we can:
12+
13+
* Create an abstract representation of an Animal (abstract class or interface)
14+
* Create concrete implementations of this abstraction (Dog & Rabbit)
15+
* Create a "class" that will make it easy to use the concrete implementations (HowToFeedAnimals)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict"
2+
3+
# ==============================
4+
# ABSTRACT ANIMAL
5+
# ==============================
6+
7+
class Animal
8+
constructor: ->
9+
throw new Error "You cannot instantiate an abstract class!" if @constructor is Animal
10+
11+
eat: ->
12+
throw new Error "You cannot call an abstract method!"
13+
14+
# ==============================
15+
# CONCRETE ANIMALS
16+
# ==============================
17+
18+
class Dog extends Animal
19+
eat: ->
20+
"Dog: 'Meat, please!'\n"
21+
22+
class Rabbit extends Animal
23+
eat: ->
24+
"Rabbit: 'A carrot would be great!'\n"
25+
26+
# ==============================
27+
# THE FACADE
28+
# ==============================
29+
30+
class HowToFeedAnimals
31+
constructor: ->
32+
@_dog = new Dog
33+
@_rabbit = new Rabbit
34+
35+
feedAnimals: ->
36+
"#{@_dog.eat()} #{@_rabbit.eat()}"
37+
38+
# ==============================
39+
# CLIENT CODE
40+
# ==============================
41+
42+
facade = new HowToFeedAnimals
43+
44+
console.log facade.feedAnimals()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
// ==============================
4+
// ABSTRACT ANIMAL
5+
// ==============================
6+
7+
var Animal = (function () {
8+
function Animal() {
9+
if (this.constructor === Animal) {
10+
throw new Error("You cannot instantiate an abstract class!");
11+
}
12+
}
13+
14+
Animal.prototype.eat = function () {
15+
throw new Error("You cannot call an abstract method!");
16+
};
17+
18+
return Animal;
19+
})();
20+
21+
// ==============================
22+
// CONCRETE ANIMALS
23+
// ==============================
24+
25+
var Dog = (function () {
26+
function Dog() {}
27+
Dog.prototype = Object.create(Animal.prototype);
28+
Dog.prototype.constructor = Dog;
29+
30+
Dog.prototype.eat = function () {
31+
return "Dog: 'Meat, please!'\n";
32+
};
33+
34+
return Dog;
35+
})();
36+
37+
var Rabbit = (function () {
38+
function Rabbit() {}
39+
Rabbit.prototype = Object.create(Animal.prototype);
40+
Rabbit.prototype.constructor = Rabbit;
41+
42+
Rabbit.prototype.eat = function () {
43+
return "Rabbit: 'A carrot would be great!'\n";
44+
};
45+
46+
return Rabbit;
47+
})();
48+
49+
// ==============================
50+
// THE FACADE
51+
// ==============================
52+
53+
var HowToFeedAnimals = (function () {
54+
var dog = new Dog(),
55+
rabbit = new Rabbit();
56+
57+
function HowToFeedAnimals() {}
58+
59+
HowToFeedAnimals.prototype.feedAnimals = function () {
60+
return dog.eat() + rabbit.eat();
61+
};
62+
63+
return HowToFeedAnimals;
64+
})();
65+
66+
// ==============================
67+
// CLIENT CODE
68+
// ==============================
69+
70+
var facade = new HowToFeedAnimals();
71+
72+
console.log(facade.feedAnimals());
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// ==============================
2+
// ABSTRACT ANIMAL
3+
// ==============================
4+
5+
class Animal {
6+
constructor() {
7+
if (new.target !== undefined) {
8+
throw new Error("You cannot instantiate an abstract class!");
9+
}
10+
}
11+
12+
eat() {
13+
throw new Error("You cannot call an abstract method!");
14+
}
15+
}
16+
17+
// ==============================
18+
// CONCRETE ANIMALS
19+
// ==============================
20+
21+
class Dog extends Animal {
22+
eat() {
23+
return "Dog: 'Meat, please!'\n";
24+
}
25+
}
26+
27+
class Rabbit extends Animal {
28+
eat() {
29+
return "Rabbit: 'A carrot would be great!'\n";
30+
}
31+
}
32+
33+
// ==============================
34+
// THE FACADE
35+
// ==============================
36+
37+
class HowToFeedAnimals {
38+
constructor() {
39+
this._dog = new Dog();
40+
this._rabbit = new Rabbit();
41+
}
42+
43+
feedAnimals() {
44+
return `${this._dog.eat()} ${this._rabbit.eat()}`;
45+
}
46+
}
47+
48+
// ==============================
49+
// CLIENT CODE
50+
// ==============================
51+
52+
var facade = new HowToFeedAnimals();
53+
54+
console.log(facade.feedAnimals());

0 commit comments

Comments
 (0)