JSFiddle - React, Tailwind, and code Playground

by Ben Watts

JavaScript

class ColourHelper
{
	constructor(factory)
  {
  	this.SettingsFactory = factory.GetInstance("SettingsHelper");
  }
  
  GetPrimaryColour()
  {
  	return this.SettingsFactory.GetSettings("Colour");
  }
}

class SettingsHelper
{
	constructor()
  {
  	this.Settings = [];
    this.Settings["Name"] = "Testing";
    this.Settings["Colour"] = "FF00FF";
    this.Settings["VersionNumber"] = "V1.5";
  }

	GetSettings(key)
  {
  	return this.Settings[key]
  }
}

class Factory
{
	constructor(dependencies)
  {
    this.Instances = [];
  	this.Instances["SettingsHelper"] = SettingsHelper;
  }
  
  GetInstance(objectName)
  {
  	var currentInstance = this.Instances[objectName + "Instance"];
    if(currentInstance === undefined)
    {
    	this.Instances[objectName + "Instance"] = new this.Instances[objectName]();
      currentInstance = this.Instances[objectName + "Instance"];
    }
    return currentInstance;
  }
}

var factoryHelper = new Factory();
var colourHelper = new ColourHelper(factoryHelper);
console.log(colourHelper.GetPrimaryColour());