4

I'm following the Stanford online course Developing iOS 7 Apps for iPhone and iPad (link to course in itunes U).

The first assignment asks the students to create some classes(Card, PlayingCard, Deck, PlayingCardDeck) detailed in the notes and update a view controller to display a random card in a deck of playing cards.

Two of the required tasks include:

  1. Add a private property of type Deck * to the CardGameViewController.
  2. Use lazy instantiation to allocate and initialize this property (in the property’s getter) so that it starts off with a full deck of PlayingCards.

I've added the following to my code:

// CardGameViewController.m
#import "PlayingCardDeck.H"

@interface CardGameViewController ()
...
@property (strong, nonatomic) Deck *deck;
@end

@implementation CardGameViewController
- (Deck *)deck
{
    if (!_deck) _deck = [[PlayingCardDeck alloc] init];
    return _deck;
}
...
@end

A hint indicates the following:

  1. Even though the type of the property you must add is required to be a Deck (not PlayingCardDeck) you’ll obviously have to lazily instantiate it using a PlayingCardDeck. This is perfectly legal in object-oriented programming because a PlayingCardDeck inherits from Deck and thus it “is a” Deck. If you are confused by this concept in object-oriented programming, this course may be rather difficult for you.

PlayingCardDeck is a subclass of Deck. I understand that it "is a" Deck.

What I don't understand is why a property of Deck is being used instead of PlyaingCardDeck.

jscs
  • 63,694
  • 13
  • 151
  • 195
Alex John
  • 65
  • 4

1 Answers1

1

Using Deck as the type of the property keeps your CardGameViewController more generic. If you wanted to use a different type of deck in the future, you could simply change that one line of code where the deck is created.

Also, if you simply made that property public, you could then create different CardGameViewControllers with different kinds of decks instead of having a PlayingCardDeckGameViewController and a TarotCardDeckGameViewController and a PinochleCardDeckGameViewController, etc.

In general, using Deck instead of PlayingCardDeck keeps more options open for you and increases the reusability of CardGameViewController.

Jonathan Arbogast
  • 9,620
  • 4
  • 35
  • 47
  • 4
    While I agree with your explanation, I think the OP also points out an important case of likely over-generalization. When you're generalizing a private property in case some day you make it public, you've probably over-designed the system, and should simplify until such a time that you need this level of abstraction. – Rob Napier Nov 07 '13 at 19:35
  • Thank you, that's the explanation I was looking for, but didn't find. @Rob Napier You're very likely correct. This is the first assignment and I expect this project will be built upon. The property may be private for a reason that becomes more clear later on. – Alex John Nov 07 '13 at 19:43
  • 2
    Thats a great point @RobNapier. In this specific classroom setting, Paul Hegarty is setting things up like this because I think in another lesson or assignment he has the students use other kinds of decks. So its a weird situation where Paul is driving the design and knows where he's going, but isn't sharing all that information with the students right up front. – Jonathan Arbogast Nov 07 '13 at 19:44