Download presentation
Presentation is loading. Please wait.
Published byElijah Short Modified over 9 years ago
1
1 4/97 Object-Oriented Design Workshop Week 6: Some Patterns for Implementing Associations, Part 1 David Van Camp Object Technology Consultant Software Architects P.O. Box 3104, Collegedale, TN 37315 Phone: (301) 918-7431 dvancamp@v2pop.hst.nasa.gov
2
2 4/97 Topics of this Workshop: –Relationship Attribute –Relationship Objects: Week 6: Some Patterns for Implementing Associations, Part 1 O-O Design Workshop Attribute Object Collection Object Mutual Friends Master and Slaves Sender Argument Null Object Timed Relationship Connected Group
3
3 Some Patterns for Implementing Associations This presentation is based on the paper: –“Some Patterns for Relationships”, James Noble, MRI, School of MPCE, Macquarie University, Sydney. Available on the web from: http://www.mri.mq.edu.au/~kjx/drafts.html
4
4 The Relatonship Patterns Null Object Relationship Attribute Relationship Objects Attribute Object Collection Objects Mutual Friends Master & Slaves Connected Group Timed Relationship Patterns Discussed in Today’s Presentation Patterns to be Discussed Next Week
5
5 Relationship Attribute How can you add a simple relationship? Declare a reference or pointer variable –May be single reference or an array –Simplest and often the only solution directly supported by most languages class myclass { anotherclass * relationship; void makerelationship ( void ) { relationship = new anotherclass; }
6
6 Relationship Object “How can you model a big, important or common relationship” Make a Relationship Object: –The Relationship Object encapsulates an indirect relation between a multiple objects –More flexible than Relationship Attributes, but also more complex and harder to implement –Requires adding special implementation objects to a design and may reduce efficiency
7
7 Relationship Object Many kinds of Relationship Objects are possible: Attribute Object Collection Object Mutual Friends Master and Slaves Sender Argument Null Object Timed Relationship Connected Group Client ref Relationship Object ref Related Object
8
8 Attribute Object “How do you model an important one-to-one relationship?” Make an Attribute Object –Generally AttributeObject has a single Relationship Attribute and provides methods for accessing the related object –Decouples association between a client and its related object, thus providing a looser binding –Increases overhead & memory usage
9
9 Attribute Object Examples: –Object Adapter in GOF Patterns Catalog (pg. 139) –GOF Observer can be used to link Attribute Objects for syncronized updates Attribute Objects often form a Connected Group (discussed in Part 2) Client ref Attribute Object ref DoOperation Related Object DoService ref
10
10 Collecton Object “How do you model a one-to-many relationship?” Make a Collection Object: –Usually encapsulates a linked list or queue –May be ordered or unordered –Usually used to implement uni-directional relationships –Most common form of Relationship Object
11
11 Collection Objects Examples: –Many collection classes are provided in C++ by RougeWave Tools.h Standard Template Library (STL) –Arrays of Relationship Attributes may be used to form more limited collections Client ref Collection Object ref Related Object
12
12 Mutual Friends “How can you represent an important bidirectional relationship?” Use Mutual Friends: –Represent relationships as a series of separate unidirectional relationships –Carefully keep all relationships syncronized –Simplist case uses two unidirectional Master and Slaves relationships for each related object –Bidirectional relationships may use a Collection Object for the relationship from the Client
13
13 Mutual Friends Consequences –Increases coupling between objects, making design less flexible –Difficult to implement because of need for syncronization –Sender Argument can be used to increase flexibility and simplify overhead Client ref Collection Object Related Object
14
14 Master and Slaves “How can you manage a relationship?” Create a Master which manages Slaves: –Master controls all access to and maintains Slave objects –Master often creates and destroys Slaves –Simpler to design and implement than cooperating relationships Client ref Master Object ref Slave Object
15
15 Sender Argument “How can an object identify itself to other participating objects?” Pass a reference via a Sender Argument: –When the Sender calls a function in an other object, include a reference or pointer to the Sender as a function parameter: someobject.doSomething (this); –The Reciever may use this for short-term localized processing, or store the reference for future access
16
16 Sender Argument Consequences –Makes designs very flexible and enhanceable, particularly if Reciever does not maintain reference to Sender Sender Action Callback Reciever doSomething (Sender & target) ref ref.doSomething (*this);... target.Callback();...
17
17 Complex Relationships Next Week in Part 2: –Null Object –Timed Relationship –Connected Group –A closer look a Relationship Attributes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.