lundi 9 avril 2018

What is this method of hiding a class called?

I've implemented a library that exposes a function speach() to create an object with specific public API functions. The functions proxy to an internal object class Speach which I don't expose to the end-user so the implementation details cannot be touched. I can change implementation details later as long as I continue to support the publicly exposed API.

Is there a name for this pattern?

class Speach {
  constructor() {
    // ...
  }

  browserSupportsFeature() {}

  loadAPI() {}

  voice(name) {
    // ...
  }

  speak(textToSpeak) {
    // ...
  }

  then(onFulfilled, onRejected) {
    // ...
  }
}

const speach = () => {
  const speach = new Speach();
  return {
    voice(name) {
      speach.voice(name);
      return this;
    },
    speak(textToSpeak) {
      speach.speak(textToSpeak);
      return this;
    },
    then(thenable) {
      speach.then(thenable);
      return this;
    }
  };
};

Aucun commentaire:

Enregistrer un commentaire