Can Singleton Instances Clash?

Discussion in 'C++' started by A_StClaire_@hotmail.com, Dec 7, 2005.

  1. Guest

    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);
    }
     
    , Dec 7, 2005
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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(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
     
    Kai-Uwe Bux, Dec 7, 2005
    #2
    1. Advertising

  3. 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
     
    Niklas Norrthon, Dec 7, 2005
    #3
  4. Guest

    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, 2005
    #4
  5. Guest

    Niklas Norrthon wrote:
    > 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, 2005
    #5
  6. Guest

    wrote:
    > Niklas Norrthon wrote:
    > > 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, 2005
    #6
  7. Guest

    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, 2005
    #7
  8. Guest

    wrote:
    > 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, 2005
    #8
  9. >
    > 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)
     
    Michael O'Keeffe, Dec 7, 2005
    #9
  10. Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Dec 7, 2005
    #10
  11. Guest

    thx for your help, Kai.
     
    , Dec 8, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,286
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    172
  3. Trans
    Replies:
    12
    Views:
    284
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    211
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    212
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page