Classes, Objects and iTunes P.J. Drongowski 8 September 2004 Objectives * Show data structures in action in a real product * Show how classes and data objects are used Motivation * Yep, we're going to talk about basic object-oriented design * User interface development helped inspire object-oriented design * Example: Files in the UNIX/Linux file system + We can discuss files collectively (a "class" of objects) - What a file is and what it stores - We can define rules of behavior + We can discuss files individually (instances of a class or an "object") + We can define operations on files and apply them to a specific file - Create file - Read and write file contents - Remove an existing file that we don't want anymore * Powerful ideas + Class + Object (instance of a class) + "Object factory" * From the user interface point of view, it's not a bad user model -- a user brings an object into being, does things to an object, deletes it Short iTunes demonstration * What is iTunes for people who haven't used it before? + Database of songs - Sorting - Searching + Rip songs from CDs + Buy and download songs from Apple music service + Burn your own CDs + Download songs to an iPod music player * Does anyone know any neat iTunes tricks? * The iTunes interface is patented! Discussion points/questions * What can we observe about the iTunes interface? + What are the kinds of objects in iTunes? + What are the attributes of the objects? + What are the operations on the objects? + What are the relationships between objects? + Are relationships one-to-one? Many-to-one? * How can songs be described and represented in C++? + Kinds of object == class + A particular object == an instance of a class + Notion of class as an "object factory" + How are attributes represented as data memebers? + How are operations declared and defined? + How do we refer to objects? - Can declare and define an object like an object of primitive type - Can indirectly refer to an object using a "handle" or "pointer" * How are relationships represented? Example // Class declaration class Song { private: char title[64] ; // Song title int minutes, seconds ; // Playing time int count ; // Number of times played int month, day, year ; // Last played public: Song() ; // Constructor ~Song() ; // Destructor void setTitle(char *t) ; // Set song title char *getTitle() ; // Get song title ... void play() ; // Playback song thru speakers } ; // Instances of Song Song louieLouie, iFoughtTheLaw ; Optional questions to investigate or ask * Where is song data (the actual AAC or MP3 file) stored? /users/XXX/Music/iTunes/iTunes Music/ARTIST/ALBUM/SONG /users/XXX/Music/iTunes/iTunes Music/Zero 7/Simple Things/Destiny.mp3 * If we drew a picture of the iTunes database structure, what would it look like? * Consider the classes: Song, Artist and Album + Several songs are released on an album (many-to-one relationship) + Each song is recorded by a single artist (one-to-one relationship) + A song may be included on more than one album (many-to-one relationship) * In a 1-to-1 relationship, whould one of the objects be represented as an attribute of the other object? + A developer should consider if the redundancy is tolerable, i.e., if it's more space efficient to store the objects separately to avoid unnecessary copies of data + Also to be considered is if the subordinate object has relationships to other objects. If yes, it should be separate. IS-A relation * Is a Compilation a special case of an Album? Artist? (Look into the iTunes library on disk for clues) * Example of IS-A relation: the program MASSIVE used to generate the armies in The Lord of The Rings * IS-A relationships are often implemented using inheritance