Download presentation
Presentation is loading. Please wait.
Published byEthan Parsons Modified over 9 years ago
1
Object Oriented Analysis and Design Class and Object Diagrams
2
Contents 2 Class Diagrams Object diagrams
3
Class Diagrams 3 Shows the relationships between classes We have several types of relationships A class implements an interface A class inherits from another class A class is composed of another class A class is an aggregate of another class
4
Sample Class Diagram 4 implements composition inheritance aggregation
5
Why Use Class Diagrams? 5 When classes are defined as text You must read the text to find the associations You must remember all the associations You can then assimilate all the class relationships With Diagrams The associations are shown as lines They are obvious and understood more quickly As a result, you understand the program more quickly
6
Uses of Class Diagrams 6 As a notation for program design alternatives As a way to capture the design of a program As a way of explaining how a program works As a way for programmers to discuss a design As part of the documentation for a program
7
Modeling Classes 7 Select a class name Define the attributes Define the methods
8
Method Categories 8 Constructors – create objects Destructors – destroy objects Accessors – return all or part of object state Modifiers – change all of part of object state Iterators – visit objects in a collection of objects
9
Defining Methods 9 Define Accessors to get and/or set private variables Methods necessary to manipulate the class Methods which are convenient to manipulate the class Keep the number of methods to a minimum offer convenience methods only when warrented
10
Associations 10 An association indicates a structural relationship between two classes Normally, this means that one class has-a instance of the other class You can specify various amounts of information about an association The most general association is represented as a line between two classes
11
Associations 11 This is a bidirectional association that indicates A car has an engine, And an engine has a car
12
Directed Associations 12 A directed association indicates that the association is only in one direction We can use a directed association to indicate that a car has an engine, but an engine does not have a car This is indicated by adding an arrow head
13
Roles 13 Each entity in an association can play a role You can label none, either, or both roles For example, an engine acts in the role of the power source for a car
14
Multiplicity 14 Sometimes the relationship between two entities is not 1 to 1 For example, a car has 4 wheels This is indicated by placing numbers at the ends of the associations
15
Multiplicity 15 In addition to a fixed number you can have 1- single instance 1.. *- one or more instances 0.. 1- zero or one instance *- zero or more instances You can label both ends of the association with their multiplicity if desired
16
Multiplicity 16 When the multiplicity is greater than one, it indicates the use of a collection to hold the members A collection of members can be ordered or unordered If ordered, this annotation is added
17
Labeling Associations 17 You can label an association to Provide additional information about the association Give it a name for convenience
18
Labeling Associations 18 An association name can have an optional arrow indicating the direction of the association Person Car owns
19
Association Roles 19 The ends of an association can also be labeled with the role one or both participants plays in the association Employee Project Participates in programmer Role played by employee
20
Qualified Associations 20 An association can be qualified to show how one object can access another This usually involves specifying how one object can index or locate another We use a rectangle on the association to indicate this Customer Order -orderNum: integer -quantity:integer = 0 orderNum:integer places
21
Composition 21 Composition is a special type of association that indicates a part-of or has-a relationship By using composition, we are specifying that one object is physically contained in the other For example, the bank account and the customer are related by composition since the account has-a owner The symbol for composition is an solid diamond at the composite end of the association line
22
Composition 22
23
Composition 23 Account Customer Branch Composition indicates physical containment
24
Aggregation 24 Aggregation is like composition, but one object is not physically contained in the other It is represented as an empty diamond
25
Aggregation 25 Account Customer Branch In aggregation, the objects in the aggregate are not physically contained, but are external to the aggregate and referenced by the aggregate
26
Inheritance 26 Inheritance is one of the key features of the object- oriented approach This allows us to take a class and create a specialized subtype of the class Specialization is usually done by adding attributes to the parent class A specialized class is often called a derived class The class from which it is derived is the parent class or base class
27
Inheritance 27 A derived class inherits The attributes of the base class The methods of the base class A derived class adds Attributes to represent its special information Methods to handle the new attributes and capabilities
28
Inheritance 28 We can create a special type of account – the savings account This has everything an account has, plus more An empty arrow head is used to indicate inheritance
29
Inheritance 29 Several classes can be derived from the same base class They each inherit the same attributes and methods of the base class
30
Generalization 30 Generalization is the opposite of specialization It allows you to take several classes, abstract what they have in common, Move this to a superclass This has the advantage that the declarations for the common attributes and methods only need to be done once This is one example of code reuse, although a minor one
31
Abstract Classes 31 During the process of class design we sometimes create a superclass that can never be instantiated Such classes are called abstract classes Abstract classes exist purely to act as base classes The names of abstract classes are italicized
32
Abstract Classes 32 Banks have chequing accounts and savings accounts They do not just have plain accounts Therefore, we can make the Account class abstract
33
Visibility 33 Visibility determines where the members of a class can be seen Visibility is indicated by a symbol in front of the member name +PublicVisible to all classes #ProtectedVisible to derived classes ~PackageVisible only with the package -PrivateVisible only within the class
34
Attribute Type 34 Denotes the type of data that will be stored in this attribute Analysis – How the user sees the data Design – represents the data type used in the specific programming language Example String Integer
35
Attribute Initial Value 35 An optional initial value can be assigned to an attribute Balance:float = 0.0
36
Derived Attribute 36 A slash “/” preceding the name of the attribute indicates that it is a derived attribute that comes from the parent class
37
Class Variables 37 A class variable is a single variable shared among all instances of the class All members of the class can access the variable A class variable can have any degree of visibility Class variables are like globals, but are not in the global name space You can also have class level methods which can be invoked on the class, rather than an object Class-level objects have their names underlined
38
Object Diagrams 38 An object diagram shows several objects and their associations This shows a snapshot of how objects are connected in an actual system This is a static snapshot at a specific point in time Object diagrams are useful to explain how the classes work together by showing an example of their use The object symbols are represented with a name and class name
39
Object Diagrams 39
40
Using Object Diagrams 40 Object diagrams can be used for Sketching the relationship between objects to help discover classes As examples of how classes can be combined To show the relationship of objects before and after an operation
41
Object Notation 41 Object format is Object Name : Class Name : Class Name Indicates an anonymous object Attributes have actual values Address = 123 Anywhere St. Operations are omitted
42
Additional Object Notation 42 The name of an object can be omitted if it is not needed You can add the values of key variables if this is important to the example
43
Class/Object Diagram Differences 43
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.