Can Singleton Instances Clash?

A

A_StClaire_

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(ReturnCommunityCards(),
(*posPlayer).GetHand()) >
Evaluation::Instance().Evaluate(ReturnCommunityCards(),
winner.GetHand())) winner = (*posPlayer);
else if(Evaluation::Instance().Evaluate(ReturnCommunityCards(),
(*posPlayer).GetHand()) ==
Evaluation::Instance().Evaluate(ReturnCommunityCards(),
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);
}
 
K

Kai-Uwe Bux

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(ReturnCommunityCards(),
(*posPlayer).GetHand()) >
Evaluation::Instance().Evaluate(ReturnCommunityCards(),
winner.GetHand())) winner = (*posPlayer);
else if(Evaluation::Instance().Evaluate(ReturnCommunityCards(),
(*posPlayer).GetHand()) ==
Evaluation::Instance().Evaluate(ReturnCommunityCards(),
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 );
}

};

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 );
}

};

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
 
N

Niklas Norrthon

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
 
A

A_StClaire_

Kai-Uwe Bux said:
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...
 
A

A_StClaire_

Niklas said:
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.
 
R

roberts.noah

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.
 
A

A_StClaire_

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.
 
M

Michael O'Keeffe

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_checked_locking_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)
 
K

Kai-Uwe Bux

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top