By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,189 Members | 910 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,189 IT Pros & Developers. It's quick & easy.

Code to replace Singletons

P: n/a
I found this code to replace the Singleton methodology, but it doesn't
compile:

#include <cstdlib>
using namespace std;

class Client
{
public:
int behavior () { MyController::behavior(); }
private:
class Server
{
public:
int behavior(){}
};
static Server& MyController() {static Server Instance; return
Instance;}
};

int main()
Client obj;
obj.behavior();
system("PAUSE");
return 0;
}

I have no idea what this goy's trying to accomplish. Help? Thanks!!!!

Jul 1 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Protoman wrote:
I found this code to replace the Singleton methodology, but it doesn't
compile:
It doesn't look like a replacement for the Singleton pattern (a more
appropriate and conventional term than "methodology" for what you mean
here); it looks like a broken implementation thereof.
#include <cstdlib>
using namespace std;

class Client
{
public:
int behavior () { MyController::behavior(); }
private:
class Server
{
public:
int behavior(){}
};
static Server& MyController() {static Server Instance; return
Instance;}
};

int main()
Client obj;
obj.behavior();
system("PAUSE");
return 0;
}

I have no idea what this goy's trying to accomplish. Help? Thanks!!!!


Well, it's pretty hard to read with the complete lack of indentation.
You might consider doing some basic formatting on code that you post,
even if you don't understand it. Better that one person take the time
to make it readable than that n people waste time deciphering.

Anyway, I'd say it doesn't compile because of the statement:
MyController::behavior()
I imagine what's intended is:
MyController().behavior()

As far as what he's trying to accomplish, like I said it's a broken
implementation of the Singleton pattern. If you don't know what the
Singleton pattern is, look it up. Finding a working implementation and
understanding that is a better place to start, compared to a broken
implementation that you don't understand.

Luke

Jul 1 '06 #2

P: n/a
Protoman wrote:
I found this code to replace the Singleton methodology, but it doesn't
compile:

#include <cstdlib>
using namespace std;

class Client
{
public:
int behavior () { MyController::behavior(); }
Probably

int behavior () { return MyController()::behavior(); }
private:
class Server
{
public:
int behavior(){}
};
static Server& MyController() {static Server Instance; return
Instance;}
};

int main()
Client obj;
obj.behavior();
system("PAUSE");
return 0;
}

I have no idea what this goy's trying to accomplish. Help? Thanks!!!!


"Goy"?

And it's not to *replace* singletons, it's just a variant of singleton
implementation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 1 '06 #3

P: n/a
Victor Bazarov wrote:
Protoman wrote:
int behavior () { MyController::behavior(); }


int behavior () { return MyController()::behavior(); }


::? .!

Luke

Jul 1 '06 #4

P: n/a
"Protoman" <Pr**********@gmail.comwrote:
I found this code to replace the Singleton methodology,
It's not an attempted replacement for (or even an attempted
implimentation of) a singleton pattern.
but it doesn't compile.
It's full of errors.
(snip ill-formatted, mistake-ridden program)
What... a... mess.
I have no idea what this goy's trying to accomplish.
Help? Thanks!!!!
Let's clean that up and fix the errors. Done. Here's what
my cleaned-up version looks like:

#include <cstdlib>
#include <iostream>

class Client
{
public:
int behavior()
{
return MyController().behavior();
}
private:
class Server
{
public:
int behavior()
{
std::cout << "Server::behavior" << std::endl;
return 0;
}
};
static Server& MyController()
{
static Server Instance;
return Instance;
}
};

int main()
{
Client obj1;
obj1.behavior();
Client obj2;
obj2.behavior();
return 0;
}

Seems to be some kind of client/server pattern. Many clients
sharing one embedded static server object. Interesting.
But not quite the same thing as a singleton.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 2 '06 #5

P: n/a

Robbie Hatley wrote:
"Protoman" <Pr**********@gmail.comwrote:
I found this code to replace the Singleton methodology,

It's not an attempted replacement for (or even an attempted
implimentation of) a singleton pattern.
but it doesn't compile.

It's full of errors.
(snip ill-formatted, mistake-ridden program)

What... a... mess.
I have no idea what this goy's trying to accomplish.
Help? Thanks!!!!

Let's clean that up and fix the errors. Done. Here's what
my cleaned-up version looks like:

#include <cstdlib>
#include <iostream>

class Client
{
public:
int behavior()
{
return MyController().behavior();
}
private:
class Server
{
public:
int behavior()
{
std::cout << "Server::behavior" << std::endl;
return 0;
}
};
static Server& MyController()
{
static Server Instance;
return Instance;
}
};

int main()
{
Client obj1;
obj1.behavior();
Client obj2;
obj2.behavior();
return 0;
}

Seems to be some kind of client/server pattern. Many clients
sharing one embedded static server object. Interesting.
But not quite the same thing as a singleton.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Ah. So, could I use this to implement a smart pointer?

Jul 2 '06 #6

P: n/a
"Protoman" <Pr**********@gmail.comwrote, regard that
damaged client/server template he found, which I polished up:
Could I use this to implement a smart pointer?
I don't think so. You could use it to impliment any system
where a bunch of clients need access to resources provided
by one centralized server. For example, a document control
server that keeps track of which documents are currently
"checked out" to which clients. Or the memory manager in
an OS kernel, which needs to keep track of what memory
blocks have been allocated to what processes.

But for a smart pointer, I think you need something closer
to a "proxy" pattern than a singleton or a client/server.
Consult page 209 of the book "Design Patterns" by Gamma, Helm,
et al., for more on this.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/

Jul 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.