1Object-Oriented Design 2Nelson Padua-PerezChau-Wen TsengDepartment of Computer ScienceUniversity of Maryland, College ParkOverviewObject-oriented designObjects, methods ⇒ Last lectureClasses, inheritance ⇒ This lectureApplying object-oriented design2Elements of Object-Oriented DesignObjectsEntities in programMethodsFunctions associated with objectsClassesGroups of objects with similar propertiesInheritanceRelationship between classesClasses DefinitionGroup of objects with same state & behaviorAbstract description of a group of objectsSimilar to data typesType is a set of data values & their operationsExample ⇒ integer, real, boolean, stringCan view classes as types for objects3Classes PropertiesClasses provides classification for objectsEvery object belongs to some classObjects ⇒ instances (instantiations) of a classExample ClassGiven a class CarObjects can includeMyHonda, YourHonda,HerMiniCooper, HisSUVAll Car objectsShare same properties & behaviorMay have different values for propertiesCar4Inheritance DefinitionRelationship between classes when state and behavior of one class is a subset of another classTerminologySuperclass / parent ⇒ More general classSubclass ⇒ More specialized classSuperclassSubclassInheritancePropertiesSubclass inherits state & behavior of superclass“Is-a” relationship exists between inherited classesExample – train is a type of transportation5Inheritance Inheritance forms a hierarchyHelps organize classesInheritance is transitiveClass inherits state & behavior from all ancestorsInheritance promotes code reuseReuse state & behavior for classInheritance Hierarchy ExampleClassesThermostatAnalog thermostatDigital thermostatProgrammable thermostat6Forms of Inheritance SpecificationDefines behavior implemented only in subclassGuarantees subclasses implement same behaviorSpecializationSubclass is customizedStill satisfies all requirements for parent classSpecialization Example7Forms of Inheritance ExtensionAdds new functionality to subclassLimitationRestricts behavior of subclassCombinationInherits features from multiple superclassesAlso called multiple inheritanceNot possible in JavaMultiple Inheritance Example CombinationAlarmClockRadio has two parent classesState & behavior from both Radio & AlarmClockSuperclasses8Applying Object-Oriented Design 1. Look at objects participating in systemFind nouns in problem statement (requirements & specifications)Noun may represent class needed in design2. Look at interactions between objectsFind verbs in problem statementVerb may represent message between objects3. Design classes accordinglyDetermine relationship between classesFind state & methods needed for each class1) Finding ClassesThermostat uses dial setting to control a heaterto maintain constant temperature in roomNounsThermostatDial settingHeaterTemperatureRoom9Finding ClassesAnalyze each nounDoes noun represent class needed in design?Noun may be outside systemNoun may describe state in classAnalyzing NounsThermostatCentral class in modelDial settingState in class (Thermostat)HeaterClass in modelRoomClass in modelTemperatureState in class (Room)HeaterThermostatDial SettingRoomTemp10Finding ClassesDecision not always clearPossible to make everything its own classApproach taken in SmalltalkOverly complex2+3 = 5 vs. NUM2.add(NUM3) = NUM5Impact of designMore classes ⇒ more abstraction, flexibilityFewer classes ⇒ less complexity, overheadChoice (somewhat) depends on personal preferenceAvoid making functions into classesExamples – class ListSorter, NameFinder2) Finding MessagesThermostat uses dial setting to control a heater to maintain constant temperature in room VerbsUsesControlMaintain11Finding MessagesAnalyze each verbDoes verb represent interaction between objects? For each interactionAssign methods to classes to perform interactionAnalyzing VerbsUses“Thermostat uses dial setting…”⇒ Thermostat.SetDesiredTemp()Control“to control a heater…”⇒ Heater.TurnOn()⇒ Heater.TurnOff()Maintain“to maintain constant temperature in room”⇒ Room.GetTemperature()12Example MessagesRoomThermostatHeaterGetTemperature()TurnOn() TurnOff()SetDesiredTemp()Resulting ClassesThermostatState – DialSettingMethods – SetDesiredTemp()HeaterState – HeaterOnMethods – TurnOn(), TurnOff()RoomState – TempMethods –
View Full Document