Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c53088

Browse files
author
Badacadabra
committed
Add Adapter (ES5 + ES6 + CoffeeScript)
1 parent 11ae76d commit 5c53088

File tree

6 files changed

+197
-0
lines changed

6 files changed

+197
-0
lines changed

‎doc/GoF/Structural/Adapter/Adapter.dia

2.04 KB
Binary file not shown.

‎doc/GoF/Structural/Adapter/Adapter.png

18.3 KB
Loading[フレーム]

‎doc/GoF/Structural/Adapter/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Synopsis
2+
3+
I have some amazing photos on my computer that I would like to display using my old projector. But my PC uses HDMI and my projector uses VGA.
4+
5+
# Problem
6+
7+
HDMI and VGA are not compatible interfaces. HDMI can handle images and sound through a digital signal whereas VGA can only handle images through an analog signal.
8+
9+
# Solution
10+
11+
Adapter is a well-known solution in this kind of situation. We need:
12+
13+
* Abstract representations (generally interfaces) of the problem. Here we easily see that a digital interface is not compatible with an analog one.
14+
* Concrete implementations of these abstractions: e.g. HDMIToVGAAdapter & VGA. Here the adapter should be able to delegate some work to the adapted entity.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict"
2+
3+
# ==============================
4+
# ABSTRACT CONNECTIONS
5+
# ==============================
6+
7+
class AnalogInterface
8+
constructor: ->
9+
throw new Error "You cannot instantiate an abstract class!" if @constructor is AnalogInterface
10+
11+
handleAnalogSignal: ->
12+
throw new Error "You cannot call an abstract method!"
13+
14+
class DigitalInterface
15+
constructor: ->
16+
throw new Error "You cannot instantiate an abstract class!" if @constructor is DigitalInterface
17+
18+
handleDigitalSignal: ->
19+
throw new Error "You cannot call an abstract method!"
20+
21+
# ==============================
22+
# CONCRETE CONNECTIONS
23+
# ==============================
24+
25+
# VGA has its own interface which handles images only through an analog signal
26+
class VGA extends AnalogInterface
27+
handleAnalogSignal: ->
28+
"Interface: VGA\nData: images\nSignal: analog\n"
29+
30+
# But your computer uses HDMI as output and your projector uses VGA as input...
31+
# Here you need an adapter if you want to display images. The two interfaces are incompatible.
32+
class HDMIToVGAAdapter extends DigitalInterface
33+
constructor: ->
34+
@_vga = new VGA
35+
36+
handleDigitalSignal: ->
37+
@_vga.handleAnalogSignal()
38+
39+
# ==============================
40+
# CLIENT CODE
41+
# ==============================
42+
43+
adapter = new HDMIToVGAAdapter
44+
45+
console.log adapter.handleDigitalSignal() # Your computer uses HDMI and your projetor uses VGA
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
// ==============================
4+
// ABSTRACT CONNECTIONS
5+
// ==============================
6+
7+
var AnalogInterface = (function () {
8+
function AnalogInterface() {
9+
if (this.constructor === AnalogInterface) {
10+
throw new Error("You cannot instantiate an abstract class!");
11+
}
12+
}
13+
14+
AnalogInterface.prototype.handleDigitalSignal = function () {
15+
throw new Error("You cannot call an abstract method!");
16+
};
17+
18+
return AnalogInterface;
19+
})();
20+
21+
22+
var DigitalInterface = (function () {
23+
function DigitalInterface() {
24+
if (this.constructor === DigitalInterface) {
25+
throw new Error("You cannot instantiate an abstract class!");
26+
}
27+
}
28+
29+
DigitalInterface.prototype.handleDigitalSignal = function () {
30+
throw new Error("You cannot call an abstract method!");
31+
};
32+
33+
return DigitalInterface;
34+
})();
35+
36+
// ==============================
37+
// CONCRETE CONNECTIONS
38+
// ==============================
39+
40+
// VGA has its own interface which handles images only through an analog signal
41+
var VGA = (function () {
42+
var name = "VGA",
43+
data = "images",
44+
signal = "analog";
45+
46+
function VGA() {}
47+
VGA.prototype = Object.create(AnalogInterface.prototype);
48+
VGA.prototype.constructor = VGA;
49+
50+
VGA.prototype.handleAnalogSignal = function () {
51+
return "Interface: " + name + "\nData: " + data + "\nSignal: " + signal;
52+
};
53+
54+
return VGA;
55+
})();
56+
57+
// But your computer uses HDMI as output and your projector uses VGA as input...
58+
// Here you need an adapter if you want to display images. The two interfaces are incompatible.
59+
var HDMIToVGAAdapter = (function () {
60+
var vga = new VGA();
61+
62+
function HDMIToVGAAdapter() {}
63+
HDMIToVGAAdapter.prototype = Object.create(DigitalInterface.prototype);
64+
HDMIToVGAAdapter.prototype.constructor = HDMIToVGAAdapter;
65+
66+
HDMIToVGAAdapter.prototype.handleDigitalSignal = function () {
67+
return vga.handleAnalogSignal();
68+
};
69+
70+
return HDMIToVGAAdapter;
71+
})();
72+
73+
// ==============================
74+
// CLIENT CODE
75+
// ==============================
76+
77+
var adapter = new HDMIToVGAAdapter();
78+
79+
console.log(adapter.handleDigitalSignal()); // Your computer uses HDMI and your projector uses VGA
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// ==============================
2+
// ABSTRACT CONNECTIONS
3+
// ==============================
4+
5+
class AnalogInterface {
6+
constructor() {
7+
if (new.target !== undefined) {
8+
throw new Error("You cannot instantiate an abstract class!");
9+
}
10+
}
11+
12+
handleAnalogSignal() {
13+
throw new Error("You cannot call an abstract method!");
14+
}
15+
}
16+
17+
class DigitalInterface {
18+
constructor() {
19+
if (new.target !== undefined) {
20+
throw new Error("You cannot instantiate an abstract class!");
21+
}
22+
}
23+
24+
handleDigitalSignal() {
25+
throw new Error("You cannot call an abstract method!");
26+
}
27+
}
28+
29+
// ==============================
30+
// CONCRETE CONNECTIONS
31+
// ==============================
32+
33+
// VGA has its own interface which handles images only through an analog signal
34+
class VGA extends AnalogInterface {
35+
handleAnalogSignal() {
36+
return "Interface: VGA\nData: images\nSignal: analog\n";
37+
}
38+
}
39+
40+
// But your computer uses HDMI as output and your projector uses VGA as input...
41+
// Here you need an adapter if you want to display images. The two interfaces are incompatible.
42+
class HDMIToVGAAdapter extends DigitalInterface {
43+
constructor() {
44+
super();
45+
this._vga = new VGA();
46+
}
47+
48+
handleDigitalSignal() {
49+
return this._vga.handleAnalogSignal();
50+
}
51+
}
52+
53+
// ==============================
54+
// CLIENT CODE
55+
// ==============================
56+
57+
var adapter = new HDMIToVGAAdapter();
58+
59+
console.log(adapter.handleDigitalSignal()); // Your computer uses HDMI and your projector uses VGA

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /