jeudi 19 août 2021

Is there a better way to implement a singleton pattern, using Node.js?

I'm working on a (pseudo) game engine to be used with Foundry.js. The intention was to create a scaffolding that can be reused to quickly create different game systems. To make the final product easier to read and troubleshoot, I decided to use Node.js and a very modular system. (I'm not sure if any more context is needed, but please ask if it is.)

Is there any obvious issue with my implementation?

Note: I'm an 2nd year medical student and self-taught over the past summer --> a lot of what I do might look weird so...

Note: the file and object names were removed, along with extemporaneous code. I added some fillers so that the general idea would still be conveyed

core file: singleton which contains all engine modules within its instance

const Module_A = requires('./modules/module_a.js');
const Module_B = requires('./modules/module_b.js')

module.exports = (function(){
        var instance;
        
        function createInstance(){
            var o = Object.create({
                Module_A: Module_A,
                Module_B: Module_B,
                // etc.
            })
            return o;
        }
        return {
            getInstance: function(){
                if(!instance){
                    instance = createInstance();
                }
                return instance;
            },
            Module_A: function(){
                if(!instance){
                    instance = createInstance();
                }
                return instance.Module_A;
            },
            Module_B: function(){
                if(!instan ce){
                    instance = createInstance();
                }
                return instance.Module_B;
            }
        };
    }())

an abstract class declaration

module.exports = (
    class {
        constructor(){}
        static getType = function(){
            return entityConfig.type;
        };
        static entityConfig = {
            type: 'Object.(Engine.Entity)'
        }
    }
);

a concrete class declaration

const Entity = requires('*./entity.js');

module.exports = ( 
    class extends Entity {
        static requiredParameters = () => {throw `incomplete code at ${new Error().lineNumber}`};
        static optionalParameters = () => {throw `incomplete code at ${new Error().lineNumber}`};
        static docData = () => {throw `incomplete code at ${new Error().lineNumber}`};
        /* ------------------------------------ */
        /* CONSTRUCTOR                          */
        /* ------------------------------------ */ 
        constructor(){ 
            arguments.forEach(arg => {
                if(!arg.includes(requiredParameters) && !arg.includes(optionalParameters)){
                    throw console.error(`ERROR: unknown token; ${arg}\n${docData}`);
                }
            });            
            this._data = args.data;
            this.data = this._data;
        }
    }
);

Aucun commentaire:

Enregistrer un commentaire