Common design-patterns in TypeScript
npm install @codefrite/design-patterns
const eventHandler = EventHandler.getInstance();
`
Indeed, the constructor is declared protected and cannot (in TS at least) and shouldn't be invoked:
`
export default class EventHandler implements IEventHandler {
...
private static instance: object;
...
/**
* constructor: Declared protected to defeat instantiation outside of class or child class (Singleton pattern)
*/
protected constructor() {}
/**
* Returns the instance of the EventHandler class (Singleton pattern)
* @returns the instance of the EventHandler class
*/
public static getInstance = (): EventHandler =>
!EventHandler.instance
? (EventHandler.instance = new EventHandler())
: (EventHandler.instance as EventHandler);
...
};
`
$3
`
eventHandler.createChannel("keyboard-inputs");
eventHandler.createChannel("mouse-inputs");
`
$3
`
const cb = () => {}
const sub = new Subscriber();
sub.setCallBack(cb)
`
$3
`
eventHandler.subscribe("keyboard-inputs", sub);
eventHandler.unsubscribe("keyboard-inputs", sub);
`
$3
`
eventHandler.notify("keyboard-inputs", message)
``