Appedix (RASD 3/e) MACIASZEK, L.A. (2007): Requiremets Aalysis ad System Desig, 3 rd ed. Addiso Wesley, Harlow Eglad ISBN 978-0-321-44036-5 Appedix Fudametals of Object Techology Pearso Educatio Limited 2007 Topics Istace object Class Variables, methods, ad costructors Associatio Aggregatio ad compositio Geeralizatio ad iheritace Abstract class Appedix (Maciaszek - RASD 3/e) 2 Fudametals of OT BOOCH, G. (1994): Object Orieted Aalysis ad Desig with Applicatios, 2 d ed, The Bejami/Cummigs Publ Object has State Behavior Idetity (equal idetical) Objects ad atural systems Istace object Class a abstractio (geeric descriptio) for a set of objects a class for objects (but better to avoid the term object class because a class ca be istatiated ito a object object class object would soud a bit strage) Istace object a istace of a class object, istace, but ot object istace Class object a istatiated class (everythig i a object orieted system is a BOOCH, G. (1994): Object Orieted Aalysis ad Desig with Applicatios, 2 d ed, object) The Bejami/Cummigs Publ Appedix (Maciaszek - RASD 3/e) 3 Appedix (Maciaszek - RASD 3/e) 4 Object otatio objectame: classame attributeame: [type] = value How do objects collaborate? : Orde r a role that may objects ca play c1 : Course course_umber = COMP227 course_ame = Requiremets Aalysis ad Systems Desig c1 : Course :Course c1 aoymous object classame suppressed No compartmet for operatios i objects! Exceptio prototypical (delegatio-based) laguages, such as Self or Newto 1. ship(ord : ) 2. getproducts(ord : ) : Shipmet : Stock a message ivokig a operatio o a object (umbers do ot imply ay sequece of executio) 4. reorder(prd : Product) 3. aalyzelevels( ) : Purchase Appedix (Maciaszek - RASD 3/e) 5 Appedix (Maciaszek - RASD 3/e) 6 MACIASZEK (2007): Req Aalysis & Syst Desig 1
Appedix (RASD 3/e) How objects idetify each other? object idetifier - OID Object logevity Persistet object outlives the executio of the program swizzlig to covert persistet OID (disk address) to trasiet OID (memory address) Trasiet object c1 : Course CCC888 course_umber = COMP227 course_ame = Requiremets Aalysis ad Systems Desig teacher: idetity = TTT999 OIDs to implemet associatios c1 : Course course_umber = COMP227 course_ame = Requiremets Aalysis ad Systems Desig c2 : Course teacher teacher course course t1 : Teacher Appedix (Maciaszek - RASD 3/e) 7 Appedix (Maciaszek - RASD 3/e) 8 Trasiet lik How does a object kow the OID of aother object if there is o persistet lik? Previous access to a object still memorized i some program variable Search o the database A map object that associates objects to other objects by logical idetifiers (primary keys) or similar meas Creatig a ew object Message passig crs_ref.getcoursename(out crs_ame) i, out, ad i/out argumets Most OO laguages do ot make such distictio explicit I Java: message argumets of primitive data types are passed by value act as iput argumets the chage of argumet values ot possible because the operatio acts o a copy of the argumet pass by value for o-primitive data types results i the operatio receivig the referece of the argumet, ot its value the referece ca be used to access ad possibly modify the attribute values withi the passed object the above elimiates the eed for explicit i/out argumets retur type of a operatio substitutes a eed for explicit out argumets operatio ca retur oly oe retur value or o value at all (void) however, it ca retur o-primitive type (i.e. a object of ay complexity) Appedix (Maciaszek - RASD 3/e) 9 Appedix (Maciaszek - RASD 3/e) 10 Class, attributes Role ames / attributes desigatig a class Class a overloaded term! Class ame ordernumber : Iteger orderdate : Date ordervalue : Currecy the theshipmet Shipmet ship(ord : ) Attributes aalysis model Operatios() Course course_umber : Strig course_ame : Strig order_umber : Iteger order_date : Date order_value : Currecy ordernumber : Iteger orderdate : Date ordervalue : Currecy theshipmet : Shipmet Shipmet the : ship(ord : ) implemetatio model Appedix (Maciaszek - RASD 3/e) 11 Appedix (Maciaszek - RASD 3/e) 12 MACIASZEK (2007): Req Aalysis & Syst Desig 2
Appedix (RASD 3/e) Attribute visibility private attributes ad public operatios operatios ecapsulate attributes Purchase - purchasenumber : Strig - purchasedate : Date - purchasevalue : Currecy + reorder(prd : Product) Operatios i object collaboratio : 1. ship(ord : ) 2. getproducts(ord : ) 3. aalyzelevels( ) : Shipmet : Stock 4. reorder(prd : Product) : Purchase Purchase purchasenumber : Strig purchasedate : Date purchasevalue : Currecy Product Stock getproducts(ord : ) : Collectio aalyzelevels() reorder(prd : Product) Shipmet Purchase BOOCH, G. (1994): Object Orieted Aalysis ad Desig with Applicatios, 2 d ed, The Bejami/Cummigs Publ ship(ord : ) reorder(prd : Product) Appedix (Maciaszek - RASD 3/e) 13 Appedix (Maciaszek - RASD 3/e) 14 Operatio, visibility, scope, class object method procedure that implemets a operatio message ame = method ame sigature = list of formal argumets of a method visibility scope istace scope whe operatio ivoked o istace object (fidempage()) class scope whe operatio ivoked o class object (fidaverageage()) Class object most prog. lag. do ot istatiate class object they oly provide a sytax to refer to the class ame i order to access a class-scope attribute or call a classscope operatio static attribute/operatio Example ext slide Class-scope scope attributes ad operatios - example Studet studetid : S trig studetname : S trig umberofs tudets : it maxcoursesp ersemester : it <<static>> averagestudetage() : double public class Studet private Strig studetid; //accessible via Studet s operatios private Strig studetname; //accessible via Studet s operatios private static it umberofstudets; //accessible oly to Studet s static methods, such as averagestudetage() public static it maxcoursespersemester; //accessible via Studet:: maxcoursespersemester public static double averagestudetage() implemetatio code here //callable by referrig to the class ame Studet::averageStudetAge() //callable also with a object of the class std.averagestudetage() Appedix (Maciaszek - RASD 3/e) 15 Appedix (Maciaszek - RASD 3/e) 16 Variables, methods, costructors Associatio Variable ame for a storage space data member variable declared i a class istace variable (istace scope) class variable (class scope) local variable variable declared i a method body Method implemetatio of a operatio method prototype = ame + sigature + retur type overloaded methods same ames, differet sigatures Costructor special method to istatiate objects of the class costructor ame = class ame costructor has o retur type class must have at least oe costructor ivoked with the ew keyword Studet std22 = ew Studet(); //default costructor ordernumber : Iteger orderdate : Date ordervalue : Currecy the theshipmet OrdShip Shipmet shipmetid : Strig shipmetdate : Date carrier : Strig ship(ord : ) Appedix (Maciaszek - RASD 3/e) 17 Appedix (Maciaszek - RASD 3/e) 18 MACIASZEK (2007): Req Aalysis & Syst Desig 3
Appedix (RASD 3/e) Associatio degree Biary Uary (sigular) Terary Associatio multiplicity 0..1 0.. 1..1 1.. maagedby 0..1 Employee maagerof 0.. Teacher taughtby 1.. 1..1 ismaagedby isichargeof 0.. teaches 0.. CourseOfferig Appedix (Maciaszek - RASD 3/e) 19 Appedix (Maciaszek - RASD 3/e) 20 Associatio lik ad extet Associatio class Lik associatio istace represets a roleame ca be a collectio of refereces Extet set of associatio istaces i the figure, the exted of the associatio is five (five liks) CourseOfferig Studet 1 Lik 1 with 3 refereces Lik 2 Lik 3 Shipmet 1 Shipmet 2 Assessmet mark : List(Number) totalmark : Number grade : Byte Parameterized type 2 Lik 5 Lik 4 with 2 refereces Shipmet 23 Appedix (Maciaszek - RASD 3/e) 21 Appedix (Maciaszek - RASD 3/e) 22 Compositio ad aggregatio Compositio aggregatio by value Aggregatio aggregatio by referece Properties: Trasitivity Asymmetry Existece depedecy Implemeted by buried refereces or ier classes ext slides Book Chapter Sectio is_part_of has Crate BeerBottle Implemetig aggregatio by buried referece ot differet to associatio by meas of variable with private visibility however, i Java, classes caot have private visibility subset classes must have public or package visibility title : Strig Book search(what : Strig) : boolea Book() -thebook -thechapter Chapter text : Text figure : Graphics search(what : Strig) : boolea Chapter() -thebook public class Book private Strig title; private Chapter thechapter[]; private TableOfCotets thetableofcotets; public Book() //default costructor public boolea search(strig what) retur true; -thetableofcotets 1 TableOfCotets g eerate() : V oid TableOfCotets() Appedix (Maciaszek - RASD 3/e) 23 Appedix (Maciaszek - RASD 3/e) 24 MACIASZEK (2007): Req Aalysis & Syst Desig 4
Appedix (RASD 3/e) Implemetig aggregatio by ier classes title : S trig <<ier>> TableOfCotets geerate() : Void TableOfCotets() -toc 1 Book displaytoc() : Void search(what : S trig) : bo olea Book() -ch <<ier>> Chapter text : Text figure[ ] : Graphics search(what : Strig) : boolea Chapter(chNum : it) outer class istatiates ier objects i its ow costructor ier class ca be private (accessible oly from outer class) public class Book private Strig title; private Chapter[] ch; private TableOfCotets toc; public Book(...)... toc = ew TableOfCotets(); ch = ew Chapter[umberChapters]; for (it i=0; i<umberchapters; i++) ch[i] = ew Chapter(); public Void displaytoc() toc.geerate(); retur; private class TableOfCotets private Void geerate()... Geeralizatio A subclass iherits the structure ad behavior of its superclass BOOCH, G. (1994): Object Orieted Aalysis ad Desig with Applicatios, 2 d ed, The Bejami/Cummigs Publ Not a good visualizatio of geeralizatio. Subclasses iherit types, ot values (a ose ot a log ose)! Appedix (Maciaszek - RASD 3/e) 25 Appedix (Maciaszek - RASD 3/e) 26 Geeralizatio Polymorphism Perso fullname : Strig dateofbirth : java.util.date age() : it getyear() : it getyear(date : java.util.date) : it Perso() Employee datehired : java.util.da te salary : it leaveetitlemet : it leavetake : it remaiigleave() : it Employee() public class Perso private Strig fullname; private Date dateofbirth; public Perso()... public it age() retur getyear() - getyear(dateofbirth); public class Employee exteds Perso private Date datehired; private it salary; private it leaveetitlemet; private it leavetake; public Employee()... public it remaiigleave() retur leaveetitlemet - leavetake; Perso fullname : Strig dateofbirth : java.util.date age() : it P erso() Employee datehired : java.util.date salary : it leaveetitlemet : it leavetake : it remaiigleave() : it Employee() public class Maager exteds Employee private Date dateappoited; private it leavesupplemet; public Maager()... public it remaiigleave() it mrl; mrl = super.remaiigleave() + leavesupplemet; retur mrl; Maager dateappoited : java.util.date leavesupplemet : it remaiigleave() : it Maager() The same sigature (operatio ame ad the umber ad type of argumets) Appedix (Maciaszek - RASD 3/e) 27 Appedix (Maciaszek - RASD 3/e) 28 Multiple iheritace Perso fullname : Strig dateofbirth : Date age() : Iteger Multiple classificatio Multiple iheritace A class may have may superclasses, but a sigle class must be defied for each object Multiple classificatio A object is simultaeously the istace of two or more classes Teacher Tutor S tudet PostgraduateStudet The problem arises if Perso is specialized i few orthogoal hierarchies Perso ca be Employee or Studet, Male or Female, Child or Adult,, etc. Without multiple classificatio eed to defie classes for each legal combiatio betwee the orthogoal hierarchies ChildFemaleStudet etc. Appedix (Maciaszek - RASD 3/e) 29 Appedix (Maciaszek - RASD 3/e) 30 MACIASZEK (2007): Req Aalysis & Syst Desig 5
Appedix (RASD 3/e) Dyamic classificatio A object does ot oly belog to multiple classes but it ca gai or lose classes over its lifetime A Perso object ca be just a employee oe day ad a maager (ad employee) aother day I most curret object-orieted programmig eviromets, a object caot chage its class after it has bee istatiated (created) Appedix (Maciaszek - RASD 3/e) 31 Abstract class Paret class that will ot have direct istace objects Abstract class caot istatiate objects because it has at least oe abstract operatio public abstract class public abstract Double retalcharge(); V ideotape V ideotape() VideoDisk public class VideoDisk exteds public... public Double retalcharge() retur ull; Appedix (Maciaszek - RASD 3/e) 32 Iterface vs abstract class Iterface a defiitio of a sematic type with attributes (costats oly) ad operatios but without actual declaratios (implemetatios) of operatios classes that implemet the iterface provide the declaratios Abstract class has udesirable effect of the fragile base class problem Ulike abstract classes iterfaces are helpful for modelig situatios that ask for multiple iheritace iterface does ot implemet (eve partially) ay of its methods but still, pure abstract class iterface i case of iterface, ay class i the system ca implemet it, ot just the subclasses a class ca implemet ay umber of iterfaces iterface defies a referece type that separates cliet objects from the implemetatio chages i the supplier objects the implemetatio of the iterface ca chage ad the cliet object may ot be affected Multiple implemetatio iheritace ot allowed i Java VideoTape VideoTape() Not allowed i Java! VideoDisk VideoEquipmet purchaseprice : Double dateofpurchase : java.sql.date VideoPlayer VideoCamera Appedix (Maciaszek - RASD 3/e) 33 Appedix (Maciaszek - RASD 3/e) 34 Implemetig Java iterface public iterface Double retalcharge(); VideoPlayer VideoPlayer() SoudSystem VideoEquipmet purchaseprice : Double dateofpurchase : java.sql.date VideoCamera Usig iterface to elimiate depedecy to supplier ChargeCalculator getretalcharge() : Double ChargeCalculator() <<uses>> thevideo public class ChargeCalculator thevideo; public Double getretalcharge() retur thevideo.retalcharge(); VideoEquipmet purchaseprice : Double dateofpurchase : java.sql.date VideoTape VideoTape() VideoDisk public class VideoPlayer exteds VideoEquipmet implemets public VideoPlayer()... public Double retalcharge() retur ull; VideoTape V ideodisk VideoTape() VideoPlayer V ideop layer() SoudSystem VideoCamera Appedix (Maciaszek - RASD 3/e) 35 Appedix (Maciaszek - RASD 3/e) 36 MACIASZEK (2007): Req Aalysis & Syst Desig 6
Appedix (RASD 3/e) Summary Each object has a state, behavior ad idetity There are istace objects ad class objects Class defies attributes ad operatios There are three kids of relatioships associatio, aggregatio, geeralizatio Geeralizatio provides the basis for polymorphism ad iheritace Commercial programmig eviromets support multiple iheritace directly (C++, C#) or by meas of iterfaces (Java) Multiple ad dyamic classificatio is still ot supported commercially Abstract classes ad iterfaces are importat i modelig Appedix (Maciaszek - RASD 3/e) 37 MACIASZEK (2007): Req Aalysis & Syst Desig 7