Figures
Source code
Sample interactions
Class SortedDirectory
The SortedDirectory class was designed with the AddressCard in mind. It is helpful since no predefined collection had the two necessary features:Since we only want one association keyed State, for instance, it is helpful to have the unique keying provided by Dictionary. Since we want to organize notes in cleanly, it is helpful to have the sorting capabilities of SortedCollection. The SortedDirectory class provides the best of both worlds, with a pleasant and uncluttered interface.
- sorting
- uniquely keyed associations
The methods available within SortedDirectory allow users of the class to easily access and modify data. Several of the notable methods are:
The design decision to not make SortedDirectory a subclass of some sort of collection was motivated by Smalltalk's apparent inablility to effectively hide methods of superclasses.
- set: k value: v allows users to set a key k to a value v If the key does not exist, then create a new key and set it. If the key does exist, just change the value to v.
- exists: k checks if a key exists, and answers a boolean value. Although it is helpful for users of the class, it is vital for proper abstraction with other methods within the class.
- lookup: k returns the association with key k. If such an association does not exist, it returns nil
- remove: k removes the association with key k if it exists. Otherwise, it quietly does nothing.
Class AddressCard
AddressCard is a subclass of SortedDirectory. While the methods inherited were already quite robust, it was helpful to make a few modiciations to make the class more user-friendly. Some notable changes are:The substring searches offered by the contains: and key: contains: allows the AddressBook class to have a very powerful searching interface.
- Default keys, such as last and first names, are reserved. These cannot be removed, although the values can be set to different values.
- last: l first: f for quick setting of last and first names (provided purely as a convenience function).
- contains: s answers true if any value in the card has s as a substring.
- key: k contains: s performs much like contains: except that it only searches the association with key k.
Class AddressBook
AddressBook provides powerful searching methods. Within it is a SortedCollection and a method, cards to access it. By doing this, the user can modify the sortBlock to orgazine the AddressCards nicely when it is displayed using the display: method.With such available methods, it is incredibly simple to filter out cards in infinitely picky ways.
- find: aBlock is perhaps the single most useful function. With it, the user can pass in a block to filter AddressCards. It answers a new AddressBook containing the filtered cards.
- exists:, delete:, and add: operate on AddressCards. Existence and deletion of cards work only if the cards are the same card, that is, if they are identical in memory. When adding cards, identical cards are not allowed (if they pass SmallTalk's '=' test). However, it is allowable to add two cards with identical data as long as they are not shallow copies.
- intersection: and union: work much like their mathematical equivalents. They return new AddressBooks. These functions are useful when operating on different AddressBooks -- merging, finding common entries, etc. subtractFrom: is equivalent to set subtraction. These are especially helpful if one of the AddressBooks is generated with a find:.