What pattern combination does this code demonstrate?
const singleton = (() => {
// Private methods and variables
const privateVariable = 'I am private';
function privateMethod() {
return privateVariable;
}
// Public API
return {
publicVariable: 'I am public',
publicMethod() {
return privateMethod();
}
};
})();
This code demonstrates a combination of Singleton and Module patterns: 1) It creates a single instance with the immediately invoked function expression, 2) It provides encapsulation via closures to create private variables and methods, 3) It exposes a public API with only selected functionality, 4) The singleton is created once and is immediately available, 5) The module pattern aspect provides clear separation between private implementation and public interface, 6) This is a common pattern for creating utility libraries with internal state in JavaScript.