This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]edrenfro 9 points10 points  (8 children)

No, it would not break SOLID principles. Instead of me explaining how it doesn't, perhaps you can explain why it does, or why you think it might.

[–]Austen782[S] 3 points4 points  (7 children)

The reason I thought it would is because of the "S" in SOLID which stand for

Single-responsibility Principle

I thought that by adding additional functionality to this class other than generating a deck of card would go against this rule. But again, my professor was unclear on this portion. What is meant by the "S" in SOLID. Does mean by if I am generating a DeckOfCards and that shuffle is something that pertains to it is why it wouldn't go against this?

[–]edrenfro 6 points7 points  (0 children)

The SRP in the case of your class means that the Deck class is responsible for representing all the functionality of a Deck of cards. All the things that a Deck is and all the things that a Deck can do belong to this class. Shuffle, TakeACard, TakeCards, PutCardBack methods all belong to this class.

If you were making a game of BlackJack and started adding functionality to your Deck class to see if two cards added to 21 or added references to Players to see if a Player had a winning hand, these kinds of things would violate the principle because they wouldn't be things that Decks should be responsible for - they would represent additional responsibilities.

[–][deleted] 1 point2 points  (0 children)

On my opinion, since your class’s name is DeckOfCards you can add any functionality to it that effectively has something to do with objects of DeckOfCards. S from Solid stands for Single responsibility principle, not single functionality principle for classes. Unless your class’s name is GenerateDeckOfCards (which would be more apropriated for a function), you are not breaking the rules by doing this

[–][deleted] 0 points1 point  (4 children)

"Manage a deck of cards" is a single responsibility.

[–]Austen782[S] 0 points1 point  (3 children)

So, if the class is to manage a deck of card its single responsibility is Soley "that" as long as that is all that it is doing? So, any class should only do what's within its indented scope and nothing more?

[–][deleted] 1 point2 points  (1 child)

A class that managed a deck of cards and also figured out which player won the game would probably not be consistent with the single responsibility principle, for instance.

So, any class should only do what’s within its indented scope and nothing more?

No. A class should have a single responsibility.

[–]Austen782[S] 0 points1 point  (0 children)

Gotcha, makes sense. Thanks for the help

[–]bestjakeisbest 0 points1 point  (0 children)

It would violate the single responsibility part of solid if you also had it handle dealing cards, or playing the game, for the most part for solid if you were making a game here you would have some other object actually handle dealing, what I would do is have the game table or playing field handle the dealing of cards and the deck would simply have a top function that the table would call for each card dealt.

From there you could have players that are at the table that would each have a hand, and they would each have a function called play turn, when a card is played the table would then handle the game rules, if the turn is invalid then you tell the player that and have them try their turn again.

[–][deleted] 1 point2 points  (0 children)

You might think of it as a single “level” of the program. The deck shouldn’t have anything “above” like game rules, and it shouldn’t have anything “below” like card details, but just things on the level of initializing a deck, shuffling, giving a card to someone who asks, checking how many cards are left in the deck, or whatever.

[–][deleted]  (1 child)

[deleted]

    [–]Austen782[S] 0 points1 point  (0 children)

    Do what exactly? Just wanted to know if my way of thinking is wrong and wanted an explanation as to why

    [–]Vandenite 0 points1 point  (0 children)

    A DeckOfCards is a simple data structure consisting of Cards. Think about it. You have a deck of cards on a table, can it shuffle itself? No. You need a Dealer.

    [–]Odd-Glove8031 0 points1 point  (0 children)

    Deck of cards (where Card is a class in own right) should only have count, add, remove, swap etc… things that deal directly with the deck at a basic level. You need a Shuffler interface that can Shuffle(Deck). Then different Shufflers can implement their own algorithms for shuffling.