Chapter 12 Other Class and Object Relationships Marilyn
Chapter 12 Other Class and Object Relationships Marilyn Bohl/Maria Rynn Tools for Structured and Object-Oriented Design, 7 e © 2008 Pearson Education Inc. , Upper Saddle River, NJ. All rights reserved.
Introduction The world is filled with objects. A carnation is a kind of flower. A daisy is a kind of flower. A rose is a kind of flower. A yellow rose is a kind of rose. A white rose is a kind of rose. A rose petal is a part of a rose. A thorn is a part of a rose. A vase holds one or more flowers. A watering gives water to one or more flowers.
Introduction Roses, daisies, and carnations are all subclasses of flowers. Classes have relationships with other classes (and objects) that are different than generalization/specialization relationships.
Class Diagram-Generalization/Specialization
Association Connections among objects (instances) of classes. Objects of different classes can interact. Binary association Association between a pair of classes. Represented by a line or path, connects participating classes.
Association—Example 1: Works. For
Association Role names identify an association end and support navigation from one object to another using the association. Person 1. employer Self-association an association involving the same class more than once role names are necessary to identify the association ends attached to the same class.
Association Asterisk (*) Zero or more, without limit. The symbol 1. . . * is one or more, without limit. Multiplicity—how many instances of one class is the association relationship can be related to one instance of the other class in the relationship. An instance of an association is a link.
Example of Multiplicity
Sample Problem 12. 1 Vases and Flowers Tracking flowers in vase for a local nursery that sells floral arrangements to customers. A vase holds one or more flowers. 0. . 1 symbol indicates that a flower might not be contained in a vase at all.
Aggregation Aggregation, or whole-part relationship is between an aggregate (a whole) and a part. “has-a” relationship UML an unfilled diamond symbol. Other end—the part—is unmarked.
Aggregation—Example 1
Aggregation—Example 2
Managing Assets Example The aggregate may send messages to its parts during processing. A part can communicate with its aggregate during processing. A single UML, class diagram may show multiple types of relationships.
Class Diagram—Multiple Relationships Example
A Frame for Measurement Conversion
Composition—Exchange 1
Composition—Exchange 1 cont.
Composition Solid-filled diamond symbols indicate composition relationships. Composition is a form of aggregation relationship in which two additional rules apply: 1—An object may be a part of only one composite at a time. 2—a composite has sole responsibility for the disposition of its parts. The composite is responsible for their creation and destruction.
Composition During instantiation, a composite must ensure that all parts have been instantiated and correctly assigned to it. During the life of the composite no other composite may have responsibility for the part without the composite's concurrence.
Composite—Example 2
Iterative Design The relationships between the classes are aggregations The aggregation relationships are compositions. Iterative design Looking again at our work in progress to improve it or add to it as our understanding of the problem situation increases.
Sample Problem 12. 2 Planning a Group Event Honor society planning the annual induction ceremony and dinner for new members.
Class Diagram—Event Planning Problem
Inner Classes Inner class Defined within another class (the enclosing class) Define a class within another class When the inner class makes sense only in the context of its enclosing class When it relies on the enclosing class for its function. Sometimes the use of an inner class stems from a desire for programming convenience.
Inner Classes Java inner class has access to all of the enclosing class's members, even if they are declared private. The modifiers restrict access by classes outside the enclosing class but not by classes inside it.
Monthly. Inventory Class with Inner Class Write. Excep Pseudocode
Monthly. Inventory Class with Inner Class Write. Excep Pseudocode cont.
Monthly. Inventory Class with Inner Class Write. Excep Pseudocode cont.
Inner Classes An inner class has no separate existence outside of the class that encloses it.
Benefits of Object-Oriented Design Simpler maintenance Resilient to change Changes can be made without invalidating the current design and without having unforeseen “side effects” on other parts of the applications. Higher productivity Class libraries contain many prebuilt (and thus working and tested) classes. Encourages and facilitates reuse not only of software but also of entire designs.
Benefits of Object-Oriented Design The availability of well-tested, proven components means less overall new work to be done on a project. Encourages the developer to work within a scope that he or she can handle easily.
- Slides: 32