C++ abstract (singleton) factory implementation...
I want to create an abstract factory template which will allow me to pass in an "ID" for a subclass and return the singleton instance of that class stored in the factory. It'd be easy to adapt for "multi-ton", but for its present use this isn't necessary.
The requirements are:
- I don't want to have to "register" the classes in some main class somewhere, I want them to register themselves as best as possible.
- I want the above to be done with as little boilerplate as possible.
This seems to be close to a Prototype pattern I found before, but I'm not sure I love it...the biggest downfall I see is that it requires a single instance of each type to be created just so it can do nothing with it but clone them.... I'm not fond of that.
So, I tried to create something myself, and it seems to be working, but if possible, I'd love it if some of y'all could comment (or send me somewhere else I can post it for comment). I've done a decent amount of C++, but not so much that I 100% trust myself without some peer review. Unfortunately, there aren't many other devs where I am with exhaustive C++ knowledge to review it either...so...I was hoping I could get a few extra eyes around here.
On the up-shot...if anyone finds it useful, steal it at will :-).
Here's the code so far:
First the template for the AbstractSingletonFactory:
Then the template for the AbstractSingletonSubclass:
Sample usage:
A few things I don't like that I tried and tried to get rid of but it seems useless:
The boilerplate to register the class.... Any other better way to do this? I tried to encapsulate it all into the templates, it doesn't work...at the end of the day, it seems the static object to do the work needs to be created in the class.... I guess this is the flip of the prototype needing a dummy instance, I need this dummy static. Though, at least it's arguably sufficiently smaller.
The oddity with the GetDirectory stuff.... But, it seems better than more boilerplate to have to instantiate this member for each template type you use (or risk crash).
The "bridge" class (NewClassBaseSubclassBase) between the base and sub-classes to tie in the template. It's an empty template, so I'm hoping it could be optimized away, but...if anyone has some bright ideas (without macros) that would eliminate it, love to hear 'em.
Well...any comments would be much appreciated.
I attached the full source as a tar.gz if anyone is interested. It's a more comprehensive use of the templates, mostly for me to attempt to best insure it's working as expected. Hopefully, even if I don't get many comments, someone, someday, will find this useful, lol.
Medel : 9117-MMC
OS: AIX 6.1
Patch level : 6100-07-04-1216
Hacmp version : HACMP v 6.1.0.8
Oracle : 11.2.0.3 RAC
Node : 2 node
Dear, my one node server has been restarted early this morning, So, i tried to start HA and Oracle database.
after that, the follow error appears at the node... (1 Reply)
Can anyone tell me if this is correct when creating classes in Java?
public abstract class Animal
{
public class Point
{
public int x,y;
}
public class Animal
{
protected Point loc;
protected String name;
protected Random rng;
String... (3 Replies)
How to write a class that can only be instancialized once?
what about my implementation below?
#include<iostream>
#include<stdexcept>
using namespace std;
class Singleton
{
public:
int b;
Singleton();
private:
static int num;
};
int Singleton::num = 1; (10 Replies)
Hi all,
I have been working with java for awhile and because of my school projects I needed to switch C++. I tried to implement some patterns in C++ but unfortunately I couldn't. Specifically, I tried to implement abstract factory pattern but since I used separated files (habitual behavior from... (4 Replies)