C# gurus, enlighten me

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Broheim

Diamond Member
Feb 17, 2011
4,587
3
81
I actually wanted to make the whole Deck static as I only want there to be 1 deck during the entire execution. But when I declared the class static it gave me problems. I might be able to fix it though.

singleton pattern is what you are looking for.


Code:
class Dao 
{

        private static Dao _dao;

        private Dao()
        {
        }

        public static Dao GetInstance()
        {
            if (_dao != null) return _dao;
            _dao = new Dao();
            return _dao;
        }
}

this is a straight copypasta from a project I'm working on (Data access object class), and this ensures that you will ever only have one instance of that class. it's probably the most basic design pattern out there, but I've found it to be the one I use the most.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
It would be rather poor design to use a singleton for something like a card deck. There are plenty of games where you do want multiple decks. If you're lazy now and use a singleton, then you create a ton of refactoring work for yourself if you later decide to implement something that needs more than one deck.

Mark_R is correct, just make a normal class and only create one instance of it.
 

Train

Lifer
Jun 22, 2000
13,590
86
91
www.bing.com
Singleton wouldn't be too bad, but it is incredibly overused.

Just create an instance of your deck in void Main or wherever, I doubt it will be too hard to keep track of it.

If at all possible, I wouldn't even bother to create a deck class. The simplest possible deck instance:

public static var Deck = new List<Card>();

But I guess you'd probably want to encapsulate a Shuffle() method, maybe a GetNextCard() method, along with the deck itself, so a deck class might be better.
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
Yeah guys I was just playing with it that's all. Rather use it in an overly simple problem than try to figure it out when it matters ;)
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
singleton pattern is what you are looking for.


Code:
class Dao 
{

        private static Dao _dao;

        private Dao()
        {
        }

        public static Dao GetInstance()
        {
            if (_dao != null) return _dao;
            _dao = new Dao();
            return _dao;
        }
}

this is a straight copypasta from a project I'm working on (Data access object class), and this ensures that you will ever only have one instance of that class. it's probably the most basic design pattern out there, but I've found it to be the one I use the most.

Nice... Makes sense to me!
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
Singleton wouldn't be too bad, but it is incredibly overused.

Just create an instance of your deck in void Main or wherever, I doubt it will be too hard to keep track of it.

If at all possible, I wouldn't even bother to create a deck class. The simplest possible deck instance:

public static var Deck = new List<Card>();

But I guess you'd probably want to encapsulate a Shuffle() method, maybe a GetNextCard() method, along with the deck itself, so a deck class might be better.

Yeah that's what I was thinking... shuffle & deal.