Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

singleton

let instance;
let counter = 0;

class Counter {
  constructor() {
    if (instance) {
      throw new Error("You can only create one instance!");
    }
    instance = this;
  }
}

const singletonCounter = Object.freeze(new Counter());
export default singletonCounter;
Source by www.patterns.dev #
 
PREVIOUS NEXT
Tagged: #singleton
ADD COMMENT
Topic
Name
8+7 =