0

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;
        }
    }
);
  • 2
    Just export an instance from a module? Then you never need to actually implement the singleton pattern, the module system provides you only one object. – VLAZ Aug 19 '21 at 11:09
  • so the only issue I can think about is the modules that are the instance properties. Wouldn't this function approach should make them protected. Is that done automatically by the module system?? – Stephen Green Aug 19 '21 at 11:39
  • https://stackoverflow.com/questions/13179109/singleton-pattern-in-nodejs-is-it-needed – Nadeem Taj Aug 19 '21 at 12:09

0 Answers0