C # abstract inheritance design issue

I am a little new to C # and try to do a good job on OO principles and write good code. After spending a couple of hours looking for my question, I found some vague answers, but I did not have enough self-confidence. There is a good chance, I ask a stupid question and approach this problem from the wrong angle.

My question is: let's say I wanted to create an abstract base card class from which I could make any card game (for example, you can compare the base deck of playing cards and Magic: The Gathering). After that: http://sourcemaking.com/refactoring/replace-type-code-with-class, I managed to create PokerCardType, which is basically an illustrious listing for every card in a standard deck. My problem is how to create an abstract card class that requires an instance of CardType, but no specific instance is defined. My first thought was to create an abstract CardType class that would have some way to require a set of static CardType objects for any subclass, but I cannot figure out how to do this. My other question is whether this idea really makes sense, or I just have to create two separate class structures: one for poker and one for Magic. If you are wondering why I would like to do this, this is really only for my own education. Thanks for the help or any links you can provide.

+3
source share
6 answers

Here is an example of an abstract Deck class, with specific implementations of PokerDeck and MagicDeck. As others have said, I'm not sure if this is the best example, because there really isn’t much in common between games.

Perhaps if you did something like Hand with SpadesHand, HeartsHand, PinochleHand, PresidentsHand, where you will have several # cards in each hand. Each game has a "PlayNextCard" method, and they have different rules, but everyone revolves around putting a card from your hand on a common pile.

public abstract class Deck
    {
          public int NumberOfCards{get; private set;}

          public IEnumerable<Card> Cards {get; private set;}

          public void Shuffle()
          {
              Cards = RandomizeCards();
          }

          protected abstract IEnumerable<Card> CreateCardList();

    }

    public class PokerDeck
    {
         public PokerDeck()
         {
              NumberOfCards = 52;
              Cards = CreateCardList();
         }
    }

    public class MagicDeck
    {
         public MagicDeck()
         {
             NumberOfCards = 10000; // have no idea
              Cards = CreateCardList();
         }
    }

Then you implement CreateCardList for each specific class. Thus, poker has 2-A for Hearts, Spades, Clubs and Diamonds. Magic has what a magic deck has.

You do not need to implement the Shuffle method because it runs in the base class.

+1

, . OO , , . OO, - . , . Shape- > Circle/Rectangle/...- > Draw .

, : . - "" - . - , . , , , () "".

: - OO. Magic - OO. , . . OO . .

+3

. ( ), , , " a Deck" Deck.

, .

class PokerDeck { }
class MagicDeck { }

, Deck, . , . , , , .

: - .

+2

, CardType, , . , , , .

0

. , ? , , . . . .

, ... , IShuffleable, .

, , , , , , . , :

public class CardDeck<T> : where T:BaseCardType
{
   private List<T> _cards new List<T>();

   public void Shuffle()
   {
      // blah blah
   }

   public void SomethingElseYouCanDoWithADeck
   { }
}

, ... , ?

0

, . " " " " , .

For example: a police car is a more specific version of a sedan, so a police car would inherit from a sedan. But the MTG card is not a more specific example of a poker card, and vice versa. You can do this for some card games, such as poker cards and Uno cards, because they have common features, such as face value and suit / color.

0
source

All Articles