Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Modeling Design Class diagram. Classes The term “class ” refers to a group of objects that share a common attributes and common behaviour (operations).

Similar presentations


Presentation on theme: "Class Modeling Design Class diagram. Classes The term “class ” refers to a group of objects that share a common attributes and common behaviour (operations)."— Presentation transcript:

1 Class Modeling Design Class diagram

2

3 Classes The term “class ” refers to a group of objects that share a common attributes and common behaviour (operations). Objects are instances of a class. A class is like a mold and an instance of a class is like a molded object. Example Mary is an object instance, while Student is an object class.

4 Classes All objects that exists within a class inherit its attributes and the operations that are available to manipulate the attributes. A super class is a collection of classes A subclass is a specialized instance of a class.

5 Classes A class has methods and attributes while object instances have behaviour and states Example: Bank account is a class which covers many different account types. John’s and Mary accounts are instances of the bank account class.

6 Bank Account -name -balance +debit(in amount) +credit(in amount) Object1 : Bank Account Name = John Smith Balance = 1,000.00 Object2 : Bank Account Name = Robert Jones Balance = -1,000.00

7 Classes The two instances are in different states. John’s account is in the credit state (positive balance), while Robert’s account is in the withdrawn state (negative balance). The state of the objects can be changed by calling the credit or debit operations. Robert’s account can be changed to the credit state if a credit operation is invoked with a parameter greater than 1000.

8

9 Attributes Things in the real world have properties. An attribute is a property of a class. Attributes attached to classes and objects describe the class or object. An attribute is a data item where an object holds its own state information. Attributes have a name, value and data type e.g. integer, Boolean.

10 Example : A class automobile has an attribute colour. The domain of values for colour is (white, black, silver, gray, blue, red, yellow, green). A book can be described in terms of its author, ISBN, publisher, etc.

11 Operations Each object can perform a set of functions in order to provide a number of services in a software system. A service is defined by one or more operations. An operation is a function or a procedure which can access the object’s data. An operation consists of two parts – name – argument(s) Thus each object must define its operation for each of its services. The collection of operations is the object’s interface. Other objects only need to know the interface of an object in order to invoke the operations provided by the object.

12 Operations An operation is sometimes called a method or a member function. Examples – withdraw(amount) – deposit (amount) – getbalance()

13 Encapsulation : Information Hiding It is only through such operations that other objects can access or manipulate the information stored in an object. The collection of operations provides an external interface to a class. The interface presents the outside view of the class without showing its internal structure or how the operations are implemented. It is only the producer, developer of that object that knows the details of the internal construction of that object. The users of an object are denied knowledge of the inner working of the object. Other objects only need to know the interface of an object in order to invoke the operations provided by the object. – Public interface – Protected interface Thus, objects are treated as black boxes. The implementation of objects are hidden from those that use them. This is a powerful concept called “Information Hiding”, better known as encapsulation principle.

14 Class diagram A class is depicted graphically in a class diagram. In UML, a class is represented by a rectangle with three compartments separated by horizontal lines. The class name appears in the top compartment, the list of attributes in the middle compartments and the list of operations in the bottom compartment of a box.

15 Shape -origin -color +move() +resize() BankAccount -account Name -customerName +getbalance(): float +setbalance() Examples

16 Naming Classes Classes are named as noun or a noun phrase. When using name phrases, eliminate the spaces and concatenate the words with their first letter in uppercase e.g. SavingAccount, BankAccount

17 Relationships Relationships exist among objects which links the type of link that exists between objects. Through the link it is possible to discover the other objects that are related to it. Association Generalization Aggregation

18 Association An association is a structural relationship that specifies that objects of one thing are connected to objects of another. A plain association is between two peer classes which means that both classes are conceptually at the same level, no one more important than the other. Given an association connecting two classes, one can navigate( send messages) from an object of one class to an object of the other class and vice versa. It is possible that an object of the class can be linked to other objects of the same class.

19 Association If an association connects between two objects instead of classes, it is called a link. A link is an instance of an association.

20 BillGates:Person Microsoft:Company Name of link WorkFor Links provide a convenient way to trace the relationship between objects. Specify only those relationships that are necessary for the system

21 Person Company Name of association WorkFor

22 Association There are four adornments that apply to associations. – Name – Role – Multiplicity – Note

23 Reflexive Association and Roles A reflexive association is an association that relates one object of a class to another object of the same class. In other words, a class can be associated with itself

24 Aggregation : Aggregation is a stronger form of association. To model a “whole/part or part-of relationship in which one class represents a larger thing ( the “whole”), which consists of smaller things (“ the parts”) is called aggregation. It represents a “has- a” relationship, meaning that an object of the whole has objects of the part. In UML, a link is placed between the whole and parts classes with a diamond head, attached to the whole class to indicate that this is an aggregation.

25 Company Department whole part aggregation 1 *****p Aggregation : * Part

26 Aggregation : Multiplicity can be specified at the end of the association for each of the part-of classes to indicate the quantity of the constituent parts. Aggregations are not named Keywords used to identify aggregations are “consists of”, contains, is part of.

27 A stronger form of aggregation is called composition, which implies whole class has exclusive ownership of the parts classes. This means parts may be created after a composite (whole) is created, but such parts will be explicitly removed before the destruction of the composite. In UML, a filled diamond indicates the composition relationship. Composition

28 Person Company EmployeeEmployer 1..n1 Example WorkFor Position -title -starting date -salary Division Department 1..n

29 School Instructor Course Department Student has attends Assigned to teaches 0…1 1..* ** * 1 1

30 Comparison AssociationAggregationComposition Is a relationship where all object have their own lifecycle Is a specialize form of Association where all object have their own lifecycle It is a strong type of Aggregation. Child object dose not have their lifecycle. There is no ownerThere is ownership and child object can not belongs to another parent object Sole ownership, parent object deletes all child object will also be deleted Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy Multiple choice questions: Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically delete

31 Inheritance The attributes and operations common to a group of subclasses are attached to a super- class and inherited by its sub-classes. Each subclass may also include new features on its own.

32 Properties of inheritance Generalization – The purpose of this property is to distribute the commonalities from the superclass among a group of similar sub classes. The subclass ( derived class) inherits all the super class's ( base class) operation and attributes.

33 Bank Account -account number -password -balance +getBalance() : float +setBalance() CheckingAccount +checkClearing() SavingsAccount -interest +addinterestToBalance()

34 Generalization BankAccount (superclass) has an attribute account_number and an operation getBalance(), the CheckingAccount (subclass) will also have the same attribute, account_number and the operation getBalance() as it is a subclass of BankAccount. It is unnecessary and inappropriate to show the superclass attributes and operations in the subclasses

35 Specialization Specialization allows subclasses to extend the functionalities of superclass. A subclass can introduce new operations and attributes of its own. Example SavingsAccount inherits attributes account_number, password and balance from BankAccount and extends the functionalities of BankAccount with an additional attribute, interest and an additional operation, addInterestToBalance. A SavingsAccount has the attribute interest that BankAccount does not because not all bank accounts earn interest.

36 Example - specialization A subclass Y inherits all of the attributes and operations associate with its super-class X. This means that all data structures and algorithms originally designed and implemented for X are immediately available for Y- no further work need be done. Reuse has been accomplished directly. Any change to the data or operations contained within a super-class is immediately inherited by all subclasses that have inherited from the super- class. Therefore a change in the super class is immediately propagated through a system.

37 Inheritance Also, at each level of the class hierarchy, new attributes and operations may be added to those classes that have been inherited from higher levels in the hierarchy.

38 X1 CHAR1 CHAR2 CHAR3 X1 X2 CHAR1 CHAR2 CHAR3 CHAR4 CHAR5 X3 CHAR1 CHAR2 CHAR3 CHAR4 CHAR5 CHAR6 X4 CHAR1 CHAR2 CHAR3 CHAR4 CHAR5 CHAR7 CHAR 4 + CHAR 5 CHAR 7 CHAR 6

39 Library class hierarchy

40 User class hierarchy

41 Multiple inheritance Rather than inheriting the attributes and services from a single parent class, a system which supports multiple inheritance allows object classes to inherit from several super- classes Can lead to semantic conflicts where attributes/services with the same name in different super-classes have different semantics Makes class hierarchy reorganisation more complex

42 Multiple inheritance

43 Abstract Classes An abstract class is used to specify the required behaviour ( operations) of a class providing their actual implementation. An operation without the implementation (body) is called an abstract operation. A class with one or more abstract operations is an abstract class. An abstract class can act as a repository of shared operation signatures for its subclasses and so those methods must be implemented by subclasses according to the signatures.

44 Shape -origin -color +move() +resize() +draw() Rectangle +draw() Circle +draw() Polygon +draw()

45


Download ppt "Class Modeling Design Class diagram. Classes The term “class ” refers to a group of objects that share a common attributes and common behaviour (operations)."

Similar presentations


Ads by Google