Encapsulation of subclasses from a constructor in C

0 votes

I apologise if the question is unclear; I wasn't sure how else to put it.

I'm trying to make a card game that has the following classes. The suits are Red, Blue, Green, and Yellow, and the value is determined by the product of the card number and the suit multiplier for each suit.

1 = red

2 = blue

3 = green

4 = yellow

abstract class Card;

public class Deck
{
    private List<Card> deckList;
}

public class RedCard : Card, suit 
{
    private int number;

    public int Getvalue()
    {
        return number;
    }
}

interface suit
{
    int GetValue();
}

Is there a method to encapsulate the Card subclasses so that the Deck function Object() { [native code] } doesn't have to know what kind of cards can be added? The goal is to ensure that the Deck class does not need to be changed in the future if I introduce another suit/card subclass.

Jun 10 in C# by rajiv
• 1,620 points
11 views

1 answer to this question.

0 votes

If you think about it in terms of real life, you have a Deck that contains instances of Card. The cards in the deck all have the same types of qualities or physical features; they all have a Suit and a Number, and in your business situation, they all have a Value. They are structurally identical; the only difference is in the values for each of the characteristics.

There's no point to build more sub-classes or even interfaces for these Cards if they all have the same properties and behaviours.

We use inheritance and composition (Interfaces) in software design to add attributes and behaviours to the underlying implementation, as well as to change existing behaviour. Inheriting from a Card solely to change the values of the characteristics is an anti-pattern that might lead to misunderstanding later on. The concept of structure vs. content must be clearly distinguished. If the structure and behaviour are in good working order,

You've also defined a list of suits and declared that they have specific integer values; in C#, such fixed lists can be encapsulated with an enum.

public enum Suit : int
{
    Red = 1,
    Blue = 2,
    Green = 3,
    Yellow = 4
}

public class Deck
{
    private List<Card> deckList;
}

public class Card
{
    public Suit Suit { get; private set; }
    public int Number { get; private set; }

    public Card (Suit suit, int number)
    {
        this.Suit = suit;
        this.Number = number;
    }
  
    public int Value { get { return (int)Suit * Number; } }
}

We can now design a way to manufacture a deck of cards for us based on some predetermined criteria, which I'll refer to as:

public class Deck
{
    private const int LENGTH_OF_SUIT = 10;
    private List<Card> deckList = new List<Card>();
    
    public Deck()
    {
        BuildDeck();
    }

    private void BuildDeck()
    {
        foreach (Suit suit in Enum.GetValues(typeof(Suit)))
        {
            for(int number = 1 ; number <= LENGTH_OF_SUIT; number ++)
            {
                deckList.Add(new Card(suit, number));
            }
        }
    }
}

You may play with this simple structure here: https://dotnetfiddle.net/BnhGGG

answered Jun 11 by pranav
• 2,320 points

Related Questions In C#

0 votes
1 answer

How to find the extension of a file in C#

Go to here https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension?redirectedfrom=MSDN&view=net-6.0#System_IO_Path_GetExtension_System_String_ string myFilePath = @"C:\MyFile.txt"; string ext ...READ MORE

answered 5 days ago in C# by jyoti
• 1,140 points
12 views
0 votes
0 answers

Learning C from a background in C#

I'd like to increase my programming expertise, ...READ MORE

Jun 11 in C# by jyoti
• 1,140 points
4 views
0 votes
1 answer

What are the differences between C, C# and C++ in terms of real-world applications?

C is a bare-bones, straightforward, and clean ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
19 views
0 votes
1 answer

C# - Can someone please show me a very simple example of Interfaces

Take this as example interface IFlyable { ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
13 views
0 votes
1 answer

How abstraction and encapsulation differ

They are, in my opinion, slightly different ...READ MORE

answered Jun 9 in C# by rajiv
• 1,620 points
10 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,100 points
4,466 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
499 views
+6 votes
16 answers

How do backend of these really cool games work?

Most of the games these days don't ...READ MORE

answered Jul 19, 2018 in Career Counselling by Kalgi
• 52,350 points
7,250 views
0 votes
1 answer

Examples of dynamic polymorphism in c#

Check the example of polymorphism below. We ...READ MORE

answered Jun 7 in C# by pranav
• 2,320 points
12 views
0 votes
1 answer

How do I specify the exit code of a console application in .NET

Simply put, you can return the exit ...READ MORE

answered Jun 11 in C# by pranav
• 2,320 points
20 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP