DEV Community

Facade pattern in JavaScript

Tomasz Buszewski on December 23, 2018

When building an application, we often face problems with external APIs. One has simple methods, other has them very complicated. Unifying them und...
Collapse
 
saeedzandian profile image
Saeed • Edited

amazing! thanks for great article!
just out of curiosity, would it be nicer if we use symbol instead of string literals for different types?
and then instead of switch using strategy pattern using symbol as object key to select intended function

Collapse
 
tomekbuszewski profile image
Tomasz Buszewski

You can use Symbol, example I've brought is purely illustrational. Or you can try TypeScript and use enums. Choose whatever you're most comfortable with, but remember to stick with it :)

Collapse
 
fabriziobertoglio1987 profile image
fabriziobertoglio1987 • Edited

in ruby would be .send(:"_findMusic", id), in JS _findMusic.call(this, id)?? or something similar??... thanks

Thread Thread
 
tomekbuszewski profile image
Tomasz Buszewski

Hi fabriziobertoglio1987, can you rephrase your question? I don't seem to understand it fully :)

Thread Thread
 
fabriziobertoglio1987 profile image
fabriziobertoglio1987

I was referring to use either Symbol or other code like .call or other Object method to delete your switch statement and replace it with a loop

developer.mozilla.org/en-US/docs/W...

developer.mozilla.org/en-US/docs/W...

Thread Thread
 
tomekbuszewski profile image
Tomasz Buszewski

Why would you obscure such a simple statement with a loop? It would be shorter, true, but more complex to the reader.

Collapse
 
metcoder profile image
Carlos Fuentes

Good topic, great explanation. Thanks!

Collapse
 
hanneslim profile image
hanneslim

When building the facade wouldn't it be better to use the rxjs observables? Like in this example: nerd-corner.com/how-to-build-a-pus...

Collapse
 
wh0ismi profile image
wh0ismi

What theme is this?

Collapse
 
tomekbuszewski profile image
Tomasz Buszewski

Hey wh0ismi, what do you mean? If you are referring to the code theme used on dev.to, I am not sure, but looks like Monokai.

Collapse
 
jamalabd profile image
Jamal Abdul-Majid

Nice! like a beautiful reducer.

Collapse
 
silverium profile image
Soldeplata Saketos

TypeScript and enums could help as well