Search code examples
oopdfactory-pattern

Get class instance by class name string


I noticed the function Object.factory(char[] className) in D. But it does not work like I hoped it would work; it does not work ;)

An example:

import std.stdio;

class TestClass
{
    override string toString()
    {
        return typeof(this).stringof; // TestClass
    }
};

void main(string[] args)
{
    auto i = Object.factory("TestClass");
    if (i is null)
    {
        writeln("Class not found");
    }
    else
    {
        writeln("Class string: " ~ i);
    }
}

I think this should result in the message: "Class string: TestClass" but it says "Class not found".

Does anybody know why this happens and how I could fix it ?

Or do I need to make my own class factory. For example by make a class with a static array Object[string] classes; with class instances. When I want a new instance I do this:

auto i = (className in classes);
if (i is null)
{
    return null;
}
return i.classinfo.create();

EDIT:

I use it now like this (an example, this is for a web HMVC pattern):

class Page : Controller
{
    static this()
    {
        register(Page.classinfo);
    }

    protected void registerActions()
    {
        registerAction("index", &index);
    }

    public void index()
    {
        request.response = "Page: " ~ request.params.get("pageID", "0") ~ " in format: " ~ request.params.get("format", "html");
    }
};

void main(string[] args)
{
    Route.add(
        r"page/(\d+)\.(html|json)",
        [
            1: "pageID",
            2: "format"
        ],
        [
            "controller": "page" // tell route to use page as controller class
        ]
    );
    Route.add(
        r"(\S+)/(\S+)",
        [
            1: "controller", // get controller class from uri
            2: "action" // get controller action from uri
        ]
    );

    auto request = Request.factory("/page/43.json").execute();

    // Headers and response can be accessed like this
    // Can be used in http response
    uint code = request.getCode();
    const(string[string]) headers = request.getHeaders();
    string response = request.response;
}

This kind of stuff is hard to do in C++ ;)


Solution

  • Here's one that works:

    module irc2;
    
    import std.stdio;
    
    class TestClass
    {
        override string toString()
        {
            return typeof(this).stringof; // TestClass
        }
    };
    
    void main(string[] args)
    {
        auto i = Object.factory("irc2.TestClass");
        if (i is null)
        {
            writeln("Class not found");
        }
        else
        {
            writeln("Class string: " ~ i.toString);
        }
    }
    

    A few things to note:

    1. You have to use the fully-qualified class name. What if you had more than one "TestClass" in your program.
    2. You can't append an object to a string; you have to use toString. That, or simply use writefln("Class string: %s", i).