473,394 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Can Singleton Instances Clash?

hi,

I have a singleton Evaluation class that I'm calling repeatedly in one
sequence. would someone plz have a look at the code below and tell me
if one instance of the singleton can ever "clash" with another?

I'm asking because I'm getting unexpected results. I've looked for the
problem the past several days and can't attribute the anomalies to
logic errors.

the Evaluate function uses private vectors and other internal class
members to return an int value corresponding to poker hand strength.

void Game::RunShowdown()
{
deque<Player> showDownPlayers;

for(posPlayer = players.begin(); posPlayer != players.end();
++posPlayer)
{
if((*posPlayer).GetActive()) showDownPlayers.push_back(*posPlayer);
}

ShowChipsStatus();
ShowRiver();

for(posPlayer = showDownPlayers.begin(); posPlayer !=
showDownPlayers.end(); ++posPlayer)
{
cout << "\n " << (*posPlayer).GetLastName() << "'s hand:\n\n\n";
(*posPlayer).GetHand().Peek();
}

Player winner = showDownPlayers.front();

for(posPlayer = showDownPlayers.begin() + 1; posPlayer !=
showDownPlayers.end(); ++posPlayer)
{
if(Evaluation::Instance().Evaluate(ReturnCommunity Cards(),
(*posPlayer).GetHand()) >
Evaluation::Instance().Evaluate(ReturnCommunityCar ds(),
winner.GetHand())) winner = (*posPlayer);
else if(Evaluation::Instance().Evaluate(ReturnCommunity Cards(),
(*posPlayer).GetHand()) ==
Evaluation::Instance().Evaluate(ReturnCommunityCar ds(),
winner.GetHand()))
{
if((*posPlayer).GetHand().GetTopCard().GetValue() >
winner.GetHand().GetTopCard().GetValue())
winner = (*posPlayer);
else if((*posPlayer).GetHand().GetTopCard().GetValue() ==
winner.GetHand().GetTopCard().GetValue())
{
if((*posPlayer).GetHand().GetLowPairValue() >
winner.GetHand().GetLowPairValue())
winner = (*posPlayer);
else if((*posPlayer).GetHand().GetLowPairValue() ==
winner.GetHand().GetLowPairValue())
{
if((*posPlayer).GetHand().GetKicker().GetValue() >
winner.GetHand().GetKicker().GetValue())
winner = (*posPlayer);
}
}
}

PayWinner(winner);
}

Dec 7 '05 #1
10 1448
A_*********@hotmail.com wrote:
hi,

I have a singleton Evaluation class that I'm calling repeatedly in one
sequence. would someone plz have a look at the code below and tell me
if one instance of the singleton can ever "clash" with another?

I'm asking because I'm getting unexpected results. I've looked for the
problem the past several days and can't attribute the anomalies to
logic errors.

the Evaluate function uses private vectors and other internal class
members to return an int value corresponding to poker hand strength.

void Game::RunShowdown()
{
deque<Player> showDownPlayers;

for(posPlayer = players.begin(); posPlayer != players.end();
++posPlayer)
{
if((*posPlayer).GetActive()) showDownPlayers.push_back(*posPlayer);
}

ShowChipsStatus();
ShowRiver();

for(posPlayer = showDownPlayers.begin(); posPlayer !=
showDownPlayers.end(); ++posPlayer)
{
cout << "\n " << (*posPlayer).GetLastName() << "'s hand:\n\n\n";
(*posPlayer).GetHand().Peek();
}

Player winner = showDownPlayers.front();

for(posPlayer = showDownPlayers.begin() + 1; posPlayer !=
showDownPlayers.end(); ++posPlayer)
{
if(Evaluation::Instance().Evaluate(ReturnCommunity Cards(),
(*posPlayer).GetHand()) >
Evaluation::Instance().Evaluate(ReturnCommunityCar ds(),
winner.GetHand())) winner = (*posPlayer);
else if(Evaluation::Instance().Evaluate(ReturnCommunity Cards(),
(*posPlayer).GetHand()) ==
Evaluation::Instance().Evaluate(ReturnCommunityCar ds(),
winner.GetHand()))
{
if((*posPlayer).GetHand().GetTopCard().GetValue() >
winner.GetHand().GetTopCard().GetValue())
winner = (*posPlayer);
else if((*posPlayer).GetHand().GetTopCard().GetValue() ==
winner.GetHand().GetTopCard().GetValue())
{
if((*posPlayer).GetHand().GetLowPairValue() >
winner.GetHand().GetLowPairValue())
winner = (*posPlayer);
else if((*posPlayer).GetHand().GetLowPairValue() ==
winner.GetHand().GetLowPairValue())
{
if((*posPlayer).GetHand().GetKicker().GetValue() >
winner.GetHand().GetKicker().GetValue())
winner = (*posPlayer);
}
}
}

PayWinner(winner);
}


What is the reason to use a singleton class here? Or better, what is the
reason to use a class at all? To me it looks like you are using

Evaluation::Instance().Evaluate( some_arguments )

just like a freestanding function. So why not implement it as such? If you
need to make use of preinitialized tables, a class to encapsulate static
constant data and a static evaluation function would be good enough. I do
not see a reason to actually implement a singleton pattern here and have
all those issues of lifetime management around.

So what about an implementation along those lines:

#include <vector>

// first alternative: freestanding function using static const data:
int table_lookup ( unsigned index ) {

struct my_fibb_table {

std::vector<int> data;

my_fibb_table ( unsigned length )
: data ( length )
{
if ( length > 0 ) {
data[0] = 1;
}
if ( length > 1 ) {
data[1] = 1;
}
for ( unsigned index = 2; index < length; ++index ) {
data[index] = data[index-1] + data[index-2];
}
}

int operator[] ( unsigned i ) const {
return ( data[i] );
}

};

static
my_fibb_table const table ( 20 );

return ( table[index] );

}

// second alternative: class encapsulates static const data and static
// function
class fibb {

struct my_fibb_table {

std::vector<int> data;

my_fibb_table ( unsigned length )
: data ( length )
{
if ( length > 0 ) {
data[0] = 1;
}
if ( length > 1 ) {
data[1] = 1;
}
for ( unsigned index = 2; index < length; ++index ) {
data[index] = data[index-1] + data[index-2];
}
}

int operator[] ( unsigned i ) const {
return ( data[i] );
}

};

static
my_fibb_table const table;

public:

static
int table_lookup ( unsigned index ) {
return ( table[index] );
}

};

fibb::my_fibb_table const fibb::table ( 20 );
#include <iostream>

int main ( void ) {
std::cout << table_lookup(13) << '\n';
std::cout << fibb::table_lookup(13) << '\n';
}


Best

Kai-Uwe Bux
Dec 7 '05 #2
A_*********@hotmail.com writes:
hi,

I have a singleton Evaluation class that I'm calling repeatedly in one
sequence. would someone plz have a look at the code below and tell me
if one instance of the singleton can ever "clash" with another?


If the Evaluation class is a singleton there is no other instance of it!

Impossible to say anything more without seeing the internals of Evaluation
class, but I agree with Kai-Uwe Bux about getting rid of the singleton
alltogether.

/Niklas Norrthon
Dec 7 '05 #3
Kai-Uwe Bux wrote:

What is the reason to use a singleton class here? Or better, what is the
reason to use a class at all? To me it looks like you are using

Evaluation::Instance().Evaluate( some_arguments )

just like a freestanding function. So why not implement it as such? If you
need to make use of preinitialized tables, a class to encapsulate static
constant data and a static evaluation function would be good enough. I do
not see a reason to actually implement a singleton pattern here and have
all those issues of lifetime management around.


thx for your response, Kai. I am not certain I am competent enough to
follow your example however.

the Evaluate function indeed takes two arguments. first is the vector
of five Card objects returned by ReturnCommunityCards(). second is a
reference to the Hand object returned by (*posPlayer).GetHand(). from
this Hand object the function extracts two more Card objects (Texas
Hold 'em "hole cards") contained therein. then these seven Cards are
examined to find the best possible hand based on their values and
suits. the function finally returns an integer indicating hand
strength.

in light of this is the singleton design justified? I want a static
class that specializes in evaluating Card objects, and I believe this
class is probably too large and cumbersome to be contained in
freestanding functions...

Dec 7 '05 #4
Niklas Norrthon wrote:
A_*********@hotmail.com writes:
hi,

I have a singleton Evaluation class that I'm calling repeatedly in one
sequence. would someone plz have a look at the code below and tell me
if one instance of the singleton can ever "clash" with another?


If the Evaluation class is a singleton there is no other instance of it!

Impossible to say anything more without seeing the internals of Evaluation
class, but I agree with Kai-Uwe Bux about getting rid of the singleton
alltogether.

/Niklas Norrthon


plz see my reply above, Niklas. the class contains more functions than
just Evaluate (which itself calls eight or so others). these include
methods of assessing "hole cards" and assigning "kickers" to hands.

Dec 7 '05 #5

A_StClai...@hotmail.com wrote:
Niklas Norrthon wrote:
A_*********@hotmail.com writes:
hi,

I have a singleton Evaluation class that I'm calling repeatedly in one
sequence. would someone plz have a look at the code below and tell me
if one instance of the singleton can ever "clash" with another?


If the Evaluation class is a singleton there is no other instance of it!

Impossible to say anything more without seeing the internals of Evaluation
class, but I agree with Kai-Uwe Bux about getting rid of the singleton
alltogether.

/Niklas Norrthon


plz see my reply above, Niklas. the class contains more functions than
just Evaluate (which itself calls eight or so others). these include
methods of assessing "hole cards" and assigning "kickers" to hands.


Well, the most important part of Niklas's reply is that there is
something wrong with your singleton if there is more than one of them.
That is what the "single" in singleton means. Either you mean
something else (perhapse factory) or your singleton is broken.

Dec 7 '05 #6
ro**********@gmail.com wrote:

Well, the most important part of Niklas's reply is that there is
something wrong with your singleton if there is more than one of them.
That is what the "single" in singleton means. Either you mean
something else (perhapse factory) or your singleton is broken.


this is one of my first experiences with the singleton pattern so you
may be quite right about my not knowing how to use it.

I believe I have explained what my singleton Evaluation class is used
for and am open to any input regarding its appropriateness.

Dec 7 '05 #7

A_*********@hotmail.com wrote:
ro**********@gmail.com wrote:

Well, the most important part of Niklas's reply is that there is
something wrong with your singleton if there is more than one of them.
That is what the "single" in singleton means. Either you mean
something else (perhapse factory) or your singleton is broken.
this is one of my first experiences with the singleton pattern so you
may be quite right about my not knowing how to use it.


http://en.wikipedia.org/wiki/Singleton_pattern
I believe I have explained what my singleton Evaluation class is used
for and am open to any input regarding its appropriateness.


Dec 7 '05 #8
>
Well, the most important part of Niklas's reply is that there is
something wrong with your singleton if there is more than one of them.
That is what the "single" in singleton means. Either you mean
something else (perhapse factory) or your singleton is broken.


Actually, the singleton pattern is easily broken, even by experts. One
of the 'patterns' used to implement a singleton, Double-checked
locking, turns out to not work.

http://en.wikipedia.org/wiki/Double_...ocking_pattern

(Normally wikipedia isn't the first place I'd check for info, but it
turns out to be a good reference, at least for this topic, which is
linked from the Singleton page posted in this thread)

Dec 7 '05 #9
A_*********@hotmail.com wrote:
Kai-Uwe Bux wrote:

What is the reason to use a singleton class here? Or better, what is the
reason to use a class at all? To me it looks like you are using

Evaluation::Instance().Evaluate( some_arguments )

just like a freestanding function. So why not implement it as such? If
you need to make use of preinitialized tables, a class to encapsulate
static constant data and a static evaluation function would be good
enough. I do not see a reason to actually implement a singleton pattern
here and have all those issues of lifetime management around.


thx for your response, Kai. I am not certain I am competent enough to
follow your example however.

the Evaluate function indeed takes two arguments. first is the vector
of five Card objects returned by ReturnCommunityCards(). second is a
reference to the Hand object returned by (*posPlayer).GetHand(). from
this Hand object the function extracts two more Card objects (Texas
Hold 'em "hole cards") contained therein. then these seven Cards are
examined to find the best possible hand based on their values and
suits. the function finally returns an integer indicating hand
strength.

in light of this is the singleton design justified? I want a static
class that specializes in evaluating Card objects, and I believe this
class is probably too large and cumbersome to be contained in
freestanding functions...


I see, the process of evaluating the strength of a hand is too complex for a
single freestanding function. So you want to take it apart into managable
pieces (that is good). Also, you want to hide those pieces from the client
code of the evaluation function as these pieces are just implementation
details (that is good, too). Hence you want them to be the private parts of
some class. So far, I am with you. However, there still is no need for a
singleton pattern. In fact, there is no need for that class to have objects
at all. Think of something like this (not run through a compiler):

// the evaluation class:
class Evaluation {

int some_helper_function ( Hand const & ) {
lots of code
}

struct some_helper_struct {

std::vector< int > some_table;

some_helper_struct ( void ) {
initialization_code;
}

};

static some_helper_struct const my_table;

more_of_the_above;

public:

static
int evaluate ( std::vector< Card > const &, Hand const & );

};

// initialize the static const objects of Evaluation:
Evaluation::some_helper_struct const Evaluation::my_table ();

int main ( void ) {

...
value = Evaluation::evaluate( card_vect, the_hand );

}

There is no need for any objects of type Evaluation, not even a single one.
Make all data of the class Evaluation static const and turn the evaluation
function into a static function of that class. No need for a singleton.

If you want to make sure that no objects of the Evaluation class are
created, make the default constructor private.
Best

Kai-Uwe Bux
Dec 7 '05 #10

thx for your help, Kai.

Dec 8 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rajarshi Guha | last post by:
Hi, I'm having a little problem with understanding the working of a singleton and borg class. Basically I nedd an class whose state will be shared across several modules. I found the stuff on the...
26
by: Uwe Mayer | last post by:
Hi, I've been looking into ways of creating singleton objects. With Python2.3 I usually used a module-level variable and a factory function to implement singleton objects. With Python2.4 I...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
7
by: phl | last post by:
hello, My project in a web project. I choose to use singleton in one of my projects. Towards the end I realise that I seemed to have refered to the fields singleton in my other classes in my...
4
by: Simon Hazelton | last post by:
Is it possible in VB.net to have a single or singleton instance of a class per specific database record. I'm writing an auction site and have a problem with resolving proxy bids, effectively I...
14
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
6
by: Andre Meyer | last post by:
While looking for an elegant implementation of the singleton design pattern I came across the decorator as described in PEP318<http://www.python.org/dev/peps/pep-0318/> . Unfortunately, the...
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.