home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!igor!dirac!rmartin
- From: rmartin@dirac.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: OOD and Inheritance question
- Message-ID: <rmartin.714436548@dirac>
- Date: 21 Aug 92 22:35:48 GMT
- References: <6298@lhdsy1.lahabra.chevron.com>
- Sender: news@Rational.COM
- Lines: 50
-
- hwrvo@kato.lahabra.chevron.com (W.R. Volz) writes:
-
- |I have a question on how to design a set of classes. It probably has a really
- |simple answer. But here goes anyway:
-
- |Let's say I have a class called Card (A playing card).
- |A collection of Cards is a CardPile. So this implies maybe a linked list.
-
- |Question 1: How should CardPile have the link installed: inherited or member
- |data? A link is not a Card so maybe it should be a data member. On the other
- |hand, I can create a new entity of CardPile by inheriting from both
- |Card and Link. (similar to inheriting from class Boy and class Scout to
- |make a new class BoyScout.) What would be best or does it depend a lot of
- |how I'm going to use CardPile?
-
- You are diving much too quickly into the details of your
- implementation. First, design your application, then fiddle with the
- details of how items get placed into linked lists.
-
- The real abstractions here are Card and Pile. A Card represents a
- single playing card. It has state: (suit and rank). It's behavior is
- unknown to me, but depends upon your application. For example, for a
- poker application, a card will probably need to compare itself with
- another card according to the ranking rules of poker.
-
- Piles are groups of cards. They have state (some number of cards) and
- they also have behavior (shuffle, draw from top, draw from bottom,
- draw random, cut, etc....)
-
- There are no inheritance relationships between these classes.
-
- As to "how the link should be installed"; Card should not have to know
- this. In fact, card should know nothing about Pile at all. It is up
- to Pile to figure out how to manage its list of Card objects. This
- can be done by creating a carrier object which maintains your link,
- and also refers to a particular Card. Pile creates carriers as Cards
- are added to the list.
-
- However, I doubt that you should make Pile manage the list directly.
- You probably want to create a List class template, and then have Pile
- contain a List < Card >; member variable.... This helps to decouple
- the Pile from the kind of list being used so keep the Cards....
-
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-