COM 6030 Software Analysis and Design Lecture 9 – More on objects and classes Dr Richard Clayton & Dr Marian Gheorghe Module (1st part) homepage http://www.dcs.shef.ac.uk/~marian COM6030 Systems Analysis and Design 1 © University of Sheffield 2005 Outline Class model. Association, aggregation, composition, generalisation. Class diagram. Case studies: ATM, Advertising company. Class attributes and operations. Stevens & Pooley chapters 2, 3, 5; Bennett et al. chapter 7 COM6030 Systems Analysis and Design 2 © University of Sheffield 2005 First part of theand course Remember objects classes An object is a thing you can interact with: It receives and reacts to messages. It behaves in a certain way according to its internal state which may change. It interacts with other objects, normally by using their names – identity of an object. (Booch) A class describes a set of objects with an equivalent role. Every object belongs to a class (myClock object belongs to – or is inherited from - the class Clock); classes are object factories. A class will have a name, optionally some attributes and may provide specific operations. COM6030 Systems Analysis and Design 3 © University of Sheffield 2005 A simple class Systems analysis: •user-oriented: actors and use cases; •object-oriented: classes and relationships between them. Class name Clock Attribute time: Time Type Operations reportTime(): Time Value returned resetTimeTO(newTime: Time) Formal parameter (argument) Class identification: •noun identification; •responsibility driven approach (CRC cards). COM6030 Systems Analysis and Design 4 © University of Sheffield 2005 Class model Consists of classes (with their attributes and operations) and their relationships. Formally these models are represented by class diagrams. Some class symbols: Dog {abstract} Animal Class symbols Module Module Abstract class symbol name: String Class with attributes and operations noOfStudents:int timetable:Timetable getStudents() listLectures() Bonzo: Dog COM6030 Systems Analysis and Design Object symbols 5 Com6030:Module © University of Sheffield 2005 Associations An association may be considered conceptually or from an implementation point of view: if there is a real-world association (described by a sentence relevant to the system: a library member borrows a book; a module is taken by a student); classes A and B are associated if: • an object of class A sends a message to an object of class B; • an object of class A creates an object of class B; • an object of class A has an attribute whose values are objects of class B or collections of objects of class B; • an object of class A receives a message with an object of class B as an argument. Associations are defined by verbs and CRD cards collaborations identify them. COM6030 Systems Analysis and Design 6 © University of Sheffield 2005 Remember CRC cards relationships – ATM system Card Responsibilities Collaborations BankAccount Responsibilities Collaborations Dispenser Responsibilities Collaborations Transaction Responsibilities COM6030 Systems Analysis and Design 7 Collaborations © University of Sheffield 2005 ATM system - class diagram Card dispenses BankAccount Dispenser Transaction Class diagrams is obtained directly from CRC cards by considering collaborations identified; it shows business classes. Classes are shown with no attributes and operations. Exercise. Draw up the class diagram for the Advertising company. COM6030 Systems Analysis and Design 8 © University of Sheffield 2005 Refining associations Student Tutor Association - undirected or bidirectional structural relationship. Association – directed structural relationship; students associated with university. Student University Campaign Advert Aggregation – whole/part structural association. ChessBoard Square Composition – whole/part exclusive association. Composition is a stronger version of aggregation. In composition each part lives and dies with the whole COM6030 Systems Analysis and Design 9 © University of Sheffield 2005 Refining associations Degree Lecturer Book 1..3 course 1 12..32 unit Module Association - with end roles and multiplicities. * Module Directed named association – with multiplicities and navigable direction. teaches 1..3 0..1 item borrower Loan Person Aggregation – whole/part structural association. Association class – stores attributes and operations of an association . COM6030 Systems Analysis and Design 10 © University of Sheffield 2005 ATM – refined class diagram Card 1..* * refers to dispenses 1 BankAccount 1 registers 1 * Dispenser Transaction For each Card there is one bank account, one dispenser and an arbitrary number of transactions. Each BankAccount should have one or more cards associated with. The Dispenser refers to all cards. Each Transaction has a unique card on it. All associations are named, directed and unidirectional. COM6030 Systems Analysis and Design 11 © University of Sheffield 2005 Advertising company – class diagrams Client Advert 1 * * 1 Campaign initiates contains A Client might initiates a number of campaigns. Each Campaign should have an arbitrary number of adverts (alternatively a range may be used, if known). The associations are undirected (or bidirectional) relationships. The association “contains” is an aggregation; “initiates” is only an association. COM6030 Systems Analysis and Design 12 © University of Sheffield 2005 Generalisation Generalisation indicates a “is a” relationship between classes. Saloon Car Vehicle Hatchback Remember generalisation relationships between uses cases/actors. Transaction Get balance COM6030 Systems Analysis and Design Card manager 13 Bank © University of Sheffield 2005 Generalisation Subclass Superclass Arrow shows direction of association between subclass and its superclass. Useful for showing abstraction of common behaviours. Abstract super-classes are not instantiated in the final system; they can be re-used elsewhere. Sub-classes inherit all the properties (attributes and operations) of the superclass as well as all of the associations. Avoid over use of generalisation. Large differences between subclasses and forced creation of superclass may lead to confusion. For example Helicopter and Bicycle are both vehicles, but there is little to be gained from generalising both. Are Book and Journal related by generalisation? COM6030 Systems Analysis and Design 14 © University of Sheffield 2005 Examples of generalisations SuccessfulTrans Transaction ATM system generalisation diagram FailedTrans NewspaperAdv Advert Advertising company generalisation diagram Module University system generalisation diagram TelevisionAdv UGModule MScModule COM6030 Systems Analysis and Design 15 © University of Sheffield 2005 Object’s state and behaviour The system that we build will consist of a collection of objects which interact. Interactions are shown through class diagrams. So far we have identified: classes; relationships (associations) between classes. Remember, an object is a thing which has behaviour, state and identity (Booch). The state is given by the attributes’ values and the behaviour is shown by operations. COM6030 Systems Analysis and Design 16 © University of Sheffield 2005 Operations and attributes Operations define the ways in which objects interact: an object sends a message to another, asking to perform an operation; the receiver will invoke a method to perform the operation; there may be more than one method implementing an operation. Attributes describe the data contained in an object of a class. According to the conceptual approach on defining classes and their associations, only data and messages expected to be understood by an object are considered. Attributes implementing associations are not considered in this stage. When is necessary/possible types associated with operations and attributes are identified – remember slide 4. COM6030 Systems Analysis and Design 17 © University of Sheffield 2005 ATM – Card class Card cardNo Card cardHolder Responsibilities Collaborations pin Manage (validate, change) PIN. Bank account provides bank details. dayLimit validatePIN() changePIN() Bank account provides balance and Initiate withdrawal. overdraft. Dispenser provides amount requested. startWidraw() Card class’ attributes and operations; validatePIN() validates the PIN introduced against the value in pin; changePIN() changes the current pin value with what this operation provides; startWithdraw() initiates the withdrawal by i) checking that the amount requested is within dayLimit range; ii) checks with BankAccount that there is enough in the current bank account or the overdraft limit is sufficient for this transaction; iii) it also checks that there is enough cash in dispenser; if all these are fulfilled it asks BankAccount to update the balance and Dispenser to release the cash. COM6030 Systems Analysis and Design 18 © University of Sheffield 2005 ATM – BankAccount class BankAccount accountNo BankAccount class: balance getBalance() provides the current balance; overdraft checkWithdrawal() checks the amount requested is within balance range and does not exceed the overdraft limit; getBalance() checkWithdrawal() updateBalance() updateBalance() changes the current balance (and maybe the overdraft limit) value(s) according to the amount taken out from the account. BankAccount Responsibilities Collaborations Provide balance. Check if amount available in the current balance and overdraft. Update balance. COM6030 Systems Analysis and Design 19 © University of Sheffield 2005 ATM – Dispenser class Dispenser cash checkCash() widrawCash() Dispenser has an attribute cash with the current available cash; it checks that a given request coming through checkCash() may be withdrawn (is less than cash value); withdrawCash() updates the current cash value by taking out the amount to be withdrawn. Dispenser Responsibilities Collaborations Check if cash available. Update cash available. COM6030 Systems Analysis and Design 20 © University of Sheffield 2005 ATM – Transaction class Transaction date cardNo accountNo status Transaction keeps for every transaction the date the card number and the account details as well as the status, either failed or successful; It uses recordTransaction() to store suitable values according to the attributes described. recordTransaction() No CRC card has been produced so far for Transaction, but there is the following description from the last lecture: let’s consider that transactions involving cash withdrawal, either failed or successful, are recorded. In this case, session (retained as vague) will be reconsidered and Transaction is the class that it will be identified. COM6030 Systems Analysis and Design 21 © University of Sheffield 2005 Summary Objects, classes and relationships. Class/object diagrams and their relationships to CRC cards. Class attributes and operations. Class operations obtained from CRC card responsibilities. Class diagram associations and CRC card collaborations. COM6030 Systems Analysis and Design 22 © University of Sheffield 2005