The Registrar Idiom
Goal
To construct/create objects without hard-coding the class name in the code.
Motivation
I heard that this sort of technique was used by the developers of the AS/400 OS to introduce new printer drivers at runtime. One of the complaints of an old version was the length of time required to install new drivers, so when IBM re-wrote the OS in C++ one of the goals was to have a dynamic run time registration system for new hardware drivers. The developers used this idiom to accomplish that requirement.
C++ vs Java
This is much simpler to do in Java with the static code block facility that is executed when the .class file is loaded by the VM. The solution below details how to accomplish this Idiom in C++, doing the same in Java is omitted for brevity.
Solution
- Add a clone() method to all classes (The 'Virtual Copy Constructor?' Idiom)
- Create a Map instance to use as a registry for all the subclasses of Thing
- This must use 'construct on first use' semantics
- This should be a static member of the base class, so the Registrar is referenced as Thing::registrar()
- 'Ugly' code can populate the registry with hard-coded or well known subclasses of 'Thing' as follows
Thing::registrar()["ThingA"] = new ThingA();
Thing::registrar()["ThingB"] = new ThingB();
... (one per derived class) - The original goal is achieved as now the following works:
String className = ...;
Thing *t = Thing.getThing(className);
where
Thing* Thing::getThing(String className)
{
return Thing::registrar()[className]->clone();
} - OPTIONAL:
- Use the constructor of a static object in the derived class to populate the registry
- If the registry doesn't contain className, then getThing can try to dynamically load a library that could contain the new derived class and then retry querying the registry.
Example
Shape.h
class Shape
{
public:
...
virtual Shape* clone() const = 0;
Shape* getShape(String& name);
...
protected:
...
static Map& registrar();
...
class Registrarer {
public:
Registrarer(String className, Shape* prototype);
};
...
private:
...
};
Shape.cpp
Map& Shape::registrar()
{
static Map* registry = new Map ;
return *registry;
}
Shape* Shape::getShape(String& name)
{
if (!registrar().containsKey(className))
{
String DLLName = className + ".DLL";
// try to load DLL containing the requested shape...
...
if (!registrar().containsKey(className))
throw Exception("Can't find: " + className);
}
return registrar()[className]->clone();
}
...
Shape::Registrarer(String className, Shape* prototype)
{
Shape::registrar()[className] = prototype;
}
...
Circle.cpp (note no .h is necessary)
class Circle : public Shape
{
public:
...
virtual Shape* clone() const;
...
private:
...
static Shape::Registrarer reg_;
...
};
// Automatically add myself to the Shape registry
Shape::Registarer Circle::reg_("Circle", new Circle());
...
22 comments:
It's going to be ending of mine day, however before end I am reading this wonderful post to increase my know-how.
My page Young teen daughter abuse - Videos
What's Taking place i am new to this, I stumbled upon this I've
found It absolutely useful and it has aided me out loads. I
hope to give a contribution & help other customers like its aided me.
Great job.
My web blog - Gail Pickle 01
I really love your blog.. Excellent colors & theme.
Did you create this amazing site yourself? Please reply
back as I'm hoping to create my own personal blog and want to find out where you got this from or what the theme is called. Cheers!
Feel free to visit my web-site ; daily-wet-tshirt.com
When someone writes an post he/she retains the thought of a user
in his/her brain that how a user can be aware of it.
Thus that's why this paragraph is outstdanding. Thanks!
My blog - amazingteenbabes.com
Does your website have a contact page? I'm having a tough time locating it but, I'd like to send you an email.
I've got some suggestions for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.
Here is my blog post ; click through The Up coming Web page
Thanks for every other fantastic article. The place else may anyone get that
type of info in such an ideal method of writing?
I've a presentation subsequent week, and I'm on the search for such info.
My web site > click the up coming web site
Pretty component of content. I simply stumbled upon your weblog and in accession capital to say that I get
in fact enjoyed account your weblog posts. Any way I'll be subscribing to your augment or even I achievement you get right of entry to persistently quickly.
Also visit my web page : www.daily-wet-tshirt.com
Thanks a lot for sharing this with all folks you really recognise what you are talking approximately!
Bookmarked. Please also talk over with my website =). We can have a link
change agreement among us
My web-site free porn
It's wonderful that you are getting thoughts from this paragraph as well as from our discussion made here.
My web page > hardcore porn star pic
I got this web page from my buddy who shared with me regarding this
web page and at the moment this time I am browsing this web page and reading very informative content at this place.
My website :: Is it selfish to fight for someone you love and to earn their love back?
My relatives all the time say that I am wasting my time here at web, except I know I am getting knowledge daily by reading such fastidious posts.
Take a look at my web page :: sachin retirement
Hi there, just wanted to say, I liked this blog post. It was funny.
Keep on posting!
Feel free to visit my homepage - Geeks on Steroids Janeth
I usually do not drop a ton of remarks, however after looking at some of the responses on "The Registrar Idiom".
I actually do have a couple of questions for you if you do not mind.
Is it just me or do a few of these responses appear like they are written by brain dead folks?
:-P And, if you are posting at other online social
sites, I would like to keep up with anything fresh you
have to post. Could you list of all of all your social networking sites like your Facebook page, twitter feed, or
linkedin profile?
Feel free to surf my web blog ; Web Series
That is a good tip particularly to those new to the blogosphere.
Brief but very precise information… Many thanks for sharing this one.
A must read post!
My web blog ... Lexington Law
Malaysia & Singapore & brunei finest online blogshop for wholesale & supply korean accessories, accessories,
earstuds, necklace, rings, bracelet, bracelet & hair accessories.
Promotion 35 % wholesale markdown. Ship Worldwide
My homepage :: unemployment.ohio.gov
Hi it's me, I am also visiting this web site regularly, this web site is really good and the people are in fact sharing nice thoughts.
Here is my web-site : Dreambox
It is perfect time to make some plans for the long run and it's time to be happy. I've read
this post and if I could I wish to counsel you some fascinating
issues or advice. Perhaps you can write next articles referring
to this article. I desire to learn more issues approximately it!
My web page :: sailboat charter marketing
Thank you, I've recently been searching for info about this subject for ages and yours is the greatest I have found out till now. But, what about the bottom line? Are you positive in regards to the source?
my blog post ... Summitt Energy
my webpage :: Visit site
I was recommended this website by my cousin. I am now
not sure whether or not this publish is written by means of him as no
one else realize such specified about my problem. You're amazing! Thanks!
my weblog ... MensHealth
my web page - covermodel
Very soon this wеb pаge ωill be famous amοng all blogging visitoгs, due to it's fastidious articles or reviews
Visit my web page: DiamondLinks
After exploring a few of the blog articles on your web page, I really appreciate your way of blogging.
I added it to my bookmark website list and will be
checking back in the near future. Please check out my web site as well and let me know how you feel.
My web-site; http://sexygirlchat.net/
e cigarette, buy electronic cigarette, electronic cigarettes, electronic cigarette reviews, electronic cigarettes, electronic cigarette
Post a Comment