Download presentation
Presentation is loading. Please wait.
1
3.2.5. Associations The relations is_a and has_a are fundamental ways to understand collections of classes. In an OO implementation these relations will usually be visible in the code. But they are not the only interesting relations!
2
Almost every OO model features relations which are ad hoc and reflect the special semantical meaning or intension of classes. Such general relations are called associations. They are used in database theory, where records, and relationships between records are described in Entity/Relationship diagrams.
3
Reasons: because two or more objects may exchange messages or services, because two or more objects may co-exist independently, or dependently because a relation may clarify the meaning of a class when we don’t quite know what it is yet. etc. etc. …
4
So aspects of dynamical behaviour and meaning may be visible in a (static) class diagram. An important association will be reflected in the code somewhere, but not in any naïve way, e.g. inheritance construct, line 27, etc.
5
The simplest association is binary and represented by a line … e.g. Class_AClass_B Normally, we at least annotate the association with a, e.g.
6
StudentCourse studies name title An arrow can be added to show the orientation or asymmetry of the relation. In this case studies is not symmetric.
7
Actually, this asymmetry is already implicit in the two different class names. We can also make the asymmetry explicit and more interesting by assigning role names For example …
8
StudentCourse name title The existence of an association between two classes often indicates some level of coupling, in the sense of related concepts. According to standard practice, coupling should be minimized and cohesion maximized. participant subject
9
Excessive coupling often indicates a bad architectural decomposition. Many associations (relations) are binary, but not all! Some are: ternary relations (3 arguments) n-ary relations (n arguments)
10
E.g. a ternary relation can be drawn as: MenWomen Priests marries and we can add more classes to the diamond.
11
3.2.6. Multiplicity of Associations In an ER diagram it is common to give the multiplicity of each component of a relation. In OO analysis and design this can help to implement, test and debug the code.
12
The commonest multiplicities are: One-to-one John Smith Mary Jones 760901-1234 691205-5678 People Tax_codes tax_coding
13
One-to-many M.E. Meinke K. Meinke N. Meinke Women People mother_of
14
Many-to-many M.E. Meinke K. Meinke N. Meinke People parent_of K.W. Meinke
15
Relation R between classes X and Y is: n-to-… if for every object y :Y there are n distinct objects x 1,…, x n : X with x i R y for i = 1,…, n …-to-m if for every object x :X there are m distinct objects y 1,…, y m : Y with x R y i for i = 1,…, m
16
So e.g. parent_of is 2-to-many. UML has a notation for multiplicity: 1one and only one 0.. 1zero or one M.. Nfrom M to N *greater than or equal to zero 0.. *…same … M.. *greater than or equal to M
17
Examples: one-to-one Class_AClass_B 11 Class_AClass_B 1* one-to-many
18
many-to-many Class_AClass_B ** We can also add multiplicity constraints to aggregation and composition relations, e.g. …
19
1 Class_A object has many Class_B objects Class_AClass_B 1*
20
We can express a choice of multiplicities in other ways, e.g 1, 3, 51 or 3 or 5 7, 9.. 12 7 or 9 or 10 or 11 or 12
21
3.3. Mapping to Code package MyPackage abstract class MyAbstractClass package MyPackage class MyDerivedClass extends MyAbstractClass { int att; void myFunction( referencedClass r ) {.. } } MyPackage MyDerivedClass att: int myFunction() MyAbstractClass
22
class MyClass { MyAggregatedClass ac; int att; void myFunction( referencedClass r ) {.. } } MyClass att: int myFunction() MyAggregatedClass Aggregation ac
23
Dependence class MyDependentClass { void myFunction1( MyReferencedClass r ) {.. } MyreferencedClass myFunction2(.. ) {.. } void myFunction3(.. ) { MyReferencedClass m.. } } MyClass att: int myFunction() MyReferencedClass 3 possible implementations! dependence arrow
24
3.3. Object Diagrams Introduction. An object diagram represents a specific collection of objects, with specific attribute values and relationships. Therefore it looks like a snapshot of an O-O system at some instant in time.
25
We can draw an object diagram to try to instantiate a particular class diagram. An object diagram O is a valid instance a class diagram C if, and only if, O satisfies all the constraints (numerical and logical) of C. This means we must check (validate) whether O really instantiates C.
26
In fact, even without any class diagrams, we can just draw object diagrams to help us think about how the system might look. So object diagrams are a complement or an alternative to class diagrams. An object diagram is also called an instance diagram.
27
Common uses of object diagrams are: 1.to illustrate the system state at a high level of abstraction, before and after some action (e.g. instruction execution) i.e. dynamic modeling !! 2.to analyze the structure of recursive data types (classes) by unfolding a class diagram.
28
3.3.1. Objects An object is drawn as a rectangle, containing possibly the: object name, object type (class), attribute names, attribute values. For example …
29
namename : type : type name : type attribute = value typeless anonymous
30
3.3.2. Links A link is a concrete instance of an association or relation. A class can be modeled as the set of all its instances. Then an n-ary association (relation) R is modeled as an n-ary relation on these sets.
31
In this case, a link is a specific n-tuple of objects: l = ( o 1, …, o n ) R Graphically a link looks like an association, only the context distinguishes the two. Association and role names are copied over to links.
32
However: 1.Logical constraints on associations are carried over as the presence or absence of links. 2. Numerical constraints on associations are carried over as numbers of links between endpoints.
33
For example, the following object diagram (below) is a legal instance of the class diagram (above): has 2 BiplaneWing Sopwith-Camel : Biplane Lower_wing : Wing Upper_wing : Wing has
34
But this object diagram is not a valid instance of the previous class diagram: Sopwith-Camel : Biplane Lower_wing : Wing has since it has the wrong number of links (1 instead of 2 on RHS)
35
Ternary and n-ary relations are represented using the diamond as before, e.g. Object_1 : Class_1 Object_2 : Class_2 Object_3 : Class_3
36
Object composition and aggregation use the same symbols as for classes, e.g. Object : Class Component2 : Class_2 Component1 : Class_1
37
Notice that now we can capture the semantic difference between composition and aggregation. E.g. the following is illegal P1 : PolygonL1 : Line Start : PointMyname : Name x = 0 y = 0 text = “big” !!!!!
38
We can use object diagrams to understand recursively defined classes. For example, the class diagram Binary_Tree 0.. 2 0,1
39
has the object diagram T1 : Binary Tree T2 : Binary Tree T3 : Binary Tree T4 : Binary Tree T5 : Binary Tree
40
4. Software Architectures Software architecture: structure or structures of the system which comprise software components, externally visible properties of these components, and relationships among them. Not the operational software!
41
Purposes: enables software engineer to … 1.Analyse effectiveness of design in meeting requirements.. 2.Because small intellectually graspable model of how components collaborate 3.So, consider architectural alternatives at a stage when making changes is still easy 4.Hence, reduces project risks associate with software construction.
42
Lets see a classification of architectures, taken from M. Shaw & D. Garlan, Software Architecture, Prentice Hall, 1996. Note: our list is just a sample!
43
4.1 Data Flow Data flows between static functional elements which transform it, aka pipeline architecture e.g. compiler C code parse tree assemblerMachine code procedure calls assembler ParseTranslate Library Link & Assemble
44
4.2. Model-View-Controller MVC An architectural model that solves many user interface (UI) problems, e.g. shared data, multiple users, different views of the same data, updating information across all users, changes to underlying data, changes to representation
45
Architecture Goals Same information capable of being presented in different formats in different windows Changes made in one view should be immediately reflected in other views Changes in the UI should be easy to make Core functionality should be independent of the interface to enable multiple interface styles
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.