Created
May 15, 2017 17:54
-
-
Save ramzesucr/c07ca1e0a591f0d563090facb4f21377 to your computer and use it in GitHub Desktop.
DesignPatterns: Adapter (javascript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Circle(x, y, r) { | |
this.x = x | |
this.y = y | |
this.r = r | |
} | |
Circle.prototype.draw = function(canvas) { | |
console.log('circle of radius', this.r, 'draw with', 'x', this.x, 'y', this.y, 'position') | |
} | |
function ExternalLibCircle(r) { | |
this.r = r | |
} | |
ExternalLibCircle.prototype.complexDraw = function(canvas, x, y) { | |
console.log('external draw of radius', this.r, 'with position', 'x', x, 'y', y) | |
} | |
function LibCircleAdapter(x, y, r) { | |
let externalLibCircle = new ExternalLibCircle(r) | |
return { | |
x: x, | |
y: y, | |
draw: function(canvas) { | |
externalLibCircle.complexDraw(canvas, this.x, this.y) | |
} | |
} | |
} | |
function run() { | |
let shapes = [] | |
shapes.push(new Circle(1, 1, 3)) | |
shapes.push(new LibCircleAdapter(4, 2, 2)) | |
let canvas = {} | |
shapes.forEach((shape) => { | |
shape.draw(canvas) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment