A
A
Andrew2017-04-03 14:13:09
JavaScript
Andrew, 2017-04-03 14:13:09

What is the essence of the Facade pattern?

I read a little about the Facade pattern, and I want to figure out if I understood it correctly, like when there are some operations, for example, on an object, and these operations need to be performed in many places in the application, but they may differ slightly, then Facade would be useful here? Is it like a wrapper over operations to perform them in one line and not duplicate code, and using parameters to use differently in each situation? that is, here I wrote an example in js

a = new ExampleObjeact();
a.makeSome1();
a.makeSome2();
a.makeSome3();

instead we would write
a = new ExampleObjeact();
makeSomething(a, makeLastSome) {
  a.makeSome1();
  a.makeSome2();
  if (makeLastSome) {
    a.makeSome3();
  }
}
makeSomething(a, true);

is it a facade? is it implemented correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2017-04-03
@undefined_title

No, not right.
For your example, the correct one would be:

a = new ExampleObjeact();
a.makeSomething = function(action) {
    return a[action]();
});
a.makeSomething('makeSome1');
a.makeSomething('makeSome2');
a.makeSomething('makeSome3');

The idea is that you make one public method that takes roughly action-type. Depending on the action, you are already doing what you need (perhaps converting the arguments and calling a specific private function). Hides your module's internal API behind a Facade.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question