dimanche 3 janvier 2021

Using Abstract Factory to create Objects in virtual machine

I implemented object system for my prototype based programming langauge. https://github.com/ObjectBerry/ORE/tree/master/source/Objects

Every object have own factory method that is used to construct objects (operator new and constructors are private/protected).

Now , after finishing, i found one thing.
Every factory method need Object map to be injected as argument - this is good for testing.
But in release , every type of object will share one map (all symbols will have same map etc.).

I want solve this by class that will be acting as abstract factory - object that will have method to create every object in system. Only difference will be that this factory will also have attributess for shared maps and every time we use this abstract factory to create new object , it will add this map into object.

Like this:

class ObjectFactory {
    Object_Layout::ObjectMap* _byteArrayMap;
    Object_Layout::ObjectMap* _objectArrayMap;
    /* other maps */

public:
    // we will include allocator that will be used for map allocation
    ObjectFactory(Memory::MemoryAllocator* allocator); 

    Objects::ByteArray newByteArray(Memory::MemoryAllocator* allocator, unsigned short length);
    Objects::ObjectArray newObjectArray(Memory::MemoryAllocator* allocator, unsigned short length);
    /* other methods */
}

This factory will be created during initialization and it will be pased to constructors or methods.
My question is - is this good design or there is better way to do this ?

Aucun commentaire:

Enregistrer un commentaire