From 10425369e92fc8ca00faf353009ce6f9421f588c Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 15 Jun 2018 07:47:09 +0200 Subject: [PATCH] Private constructor in singleton (#535) Updated the `public` constructor to `private` in singleton pattern. This prevents you from creating a new class instance outside the `ConfigurationManager`. --- .../singleton/components/ConfigurationManager.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/samples/react-designpatterns-typescript/Singleton/src/webparts/singleton/components/ConfigurationManager.ts b/samples/react-designpatterns-typescript/Singleton/src/webparts/singleton/components/ConfigurationManager.ts index 13a85ca6d..57d1db297 100644 --- a/samples/react-designpatterns-typescript/Singleton/src/webparts/singleton/components/ConfigurationManager.ts +++ b/samples/react-designpatterns-typescript/Singleton/src/webparts/singleton/components/ConfigurationManager.ts @@ -1,9 +1,11 @@ class ConfigurationManager { private static instance: ConfigurationManager; - public constructor() { + + private constructor() { // do something construct... } - static getInstance(): ConfigurationManager { + + public static getInstance(): ConfigurationManager { if (!ConfigurationManager.instance) { ConfigurationManager.instance = new ConfigurationManager(); // ... any one time initialization goes here ... @@ -12,17 +14,17 @@ class ConfigurationManager { } // excercise for the reader to get data from an external data source. - numberOfItemsPerPage(): number { + public numberOfItemsPerPage(): number { return 10; } - maxNumberOfConnections(): number { + public maxNumberOfConnections(): number { return 10; } - restTimeout(): number { + public restTimeout(): number { return 1000; } } -export default ConfigurationManager; \ No newline at end of file +export default ConfigurationManager;