FREE BOOK

Chapter 6: Collections of Objects

Posted by Apress Free Book | C# Language December 16, 2008
The properties and behaviors of some common collection types,How collections enable us to model very sophisticated real-world concepts or situations,How we can define our own collection types

CourseLoad

The courseLoad attribute is meant to represent a list of all Course objects that the Student is presently enrolled in. So, it makes perfect sense that this attribute be declared to be simply a standard collection of Course objects !

public
class Student
{
private string name;
private string studentId;
private CollectionType courseLoad; // of Course objects
// etc.

Transcript

The transcript attribute is a bit more challenging. What is a transcript, in real-world terms? It's a report of all of the courses that a student has taken since he or she was first admitted to this school, along with the semester in which each course was taken, the number of credit hours that each course was worth, and the letter grade that the student received for the course. If we think of each entry in this list as an object, we can defin  them via a TranscriptEntry class, representing an abstraction of a single line item on the transcript report, as follows:

public class TranscriptEntry
{
    // One TranscriptEntry represents a single line item on a transcript report.
    private Course courseTaken;
    private string semesterTaken; // e.g., "Spring 2000"
    private string gradeReceived; // e.g., "B+"
    // Other details omitted ...
    // Note how we "talk to" the courseTaken object via its methods
    // to retrieve some of this information (delegation once again!).
    public void PrintTranscriptEntry()
    {
        // Reminder: \t is a tab character.
        Console.WriteLine(courseTaken.CourseNo + "\t" +
        courseTaken.Title + "\t" +
        courseTaken.CreditHours + "\t" +
        gradeReceived);
    }
    // etc.
}public class Student
{
private string name;
private string studentId;
private CollectionType courseLoad; // of Course objects
// etc.

Note that we're declaring one of the attributes of TranscriptEntry to be of type Course, which means that each TranscriptEntry object will maintain a handle on its corresponding Course object. By doing this, the TranscriptEntry object can avail itself of the Course object's title, course number, or credit hour value (needed for computing the GPA)-all privately encapsulated in the Course object as attributes -by accessing the appropriate properties on that Course object as needed. Back in the Student class, we can now define the Student's transcript attribute to be a collection of TranscriptEntry objects. We can then add a PrintTranscript method to the Student class, the code for which is highlighted in the following
snippet:

public class Student
{
    private string name;
    private string studentId;
    // Pseudocode.
    private CollectionType transcript; // of TranscriptEntry objects
    // etc.
    // Details omitted ...
    public void PrintTranscript() {
// Pseudocode.
for (each TranscriptEntry t in transcript) {
t.PrintTranscriptEntry();
}
}
}

Transcript, Take 2

Alternatively, we could use the technique of creating a wrapper class called Transcript to encapsulate some standard collection type, as we did with
EnrollmentCollection in an earlier example:

public class Transcript
{
private ArrayList transcriptEntries; // of TranscriptEntry objects
// other attributes omitted from this example
// Pseudocode.
public void AddTranscriptEntry(arglist) {
insert new entry into the transcriptEntries ArrayList -- details omitted
}
// We've transferred the logic of the Student class's PrintTranscript
// method into THIS class instead.
public void PrintTranscript() {
// Pseudocode.
for (each TranscriptEntry t in transcriptEntries) {
t.PrintTranscriptEntry();
}
}
// etc.
}

We then can go back to the Student class and change our declaration of the transcript attribute from being a standard collection type to being of type

Transcript:


public class Student
{
private string name;
private string studentId;
// etc.
private Transcript transcript; // an ENCAPSULATED collection of
// TranscriptEntry objects
// etc.

We can then turn around and simplify the PrintTranscript method of the

Student class accordingly:
public class Student
{
// Details omitted.
public void PrintTranscript(string filename) {
// We now delegate the work to the Transcript attribute!
transcript.Print();
}
// etc.

This "take 2" approach of introducing two new classes/abstractions- TranscriptEntry and Transcript—is a bit more sophisticated than the first approach, where we only introduced TranscriptEntry as an abstraction. Also, this second approach is "truer" to the object paradigm, because the Student class needn't be complicated by the details of how Transcripts are represented or managed internally -those details are hidden inside of the Transcript class, as they should be. Our Completed Student Data Structure Table 6-2 illustrates how we've taken full advantage of collections to round out our Student class definition.

Table 6-2. Rounding Out the Student Class's Data Structure with Collections



Summary

In this chapter, you've learned

  • That collections are special types of objects used to gather up and manage references to other objects.
  • That arrays, as simple collections, have some limitations, but that we have other more powerful collection types to draw upon with OO languages, such as
  • Ordered lists
  • Sets
  • Dictionaries
  • That it's important to familiarize ourselves with the unique characteristics of whatever collection types are available for a particular OO language so as to make the most intelligent selection of which collection type to use for a particular circumstance.
  • That we can invent our own collection types by creating "wrapper classes" around any predefined collection classes.
  • How we can work around the limitation that a method can only return one result by having that result be a collection of objects.
  • How we can create very sophisticated composite classes through the use of collections as attributes.

Exercises

Given the following abstraction:

A book is a collection of chapters, which are each collections of pages. sketch out the code for the Book, Chapter, and Page classes.

  • Invent whatever attributes you think would be relevant, taking advantage of collections as attributes where appropriate.
  • Include methods on the Chapter class for adding pages, and for determining how many pages a chapter contains.
  • Include methods on the Book class for adding chapters, for determining how many chapters the book contains, for determining how many pages the book contains (hint: use delegation!), and for printing out a book's table of contents.

What generic type (s) of collection (s)-ordered list, sorted ordered list, set, dictionary - might you use to represent each of the following abstractions ? Explain your choices.

  • A computer parts catalog
  • A poker hand
  • Trouble calls logged by a technical help desk.

What collections do you think it would be important to maintain for the SRS, based on the requirements presented in the Introduction to this book ?

What collections do you think it would be important to maintain for the Prescription Tracking System (PTS) described in Appendix B ?

What collections do you think it would be important to maintain for the problem area that you described for exercise 3 in Chapter 2 ?

Total Pages : 8 45678

comments