OO Design 1Object OrientedDesignKenneth M. AndersonLecture 20CSCI 5828: Foundations ofSoftware EngineeringOO Design 2Object-Oriented Design Traditional procedural systems separate data andprocedures, and model these separately Object orientation combines data and methodstogether into a cohesive whole data abstraction The purpose of Object-Oriented (OO) design is todefine the classes (and their relationships) that areneeded to build a system that meets therequirements contained in the SRSOO Design 3OO A&D OO techniques can be used in analysis(requirements) as well as design The methods and notations are similar In OO analysis we model the problem domain, whilein OO design we model the solution domain Often structures created during OO analysis aresubsumed (reused, extended) in the structuresproduced by OO design The line between OO analysis and OO design is blurry, asanalysis structures will transition into model elements of thetarget systemOO Design 4Relationship of OO A&DOO Design 5OO Concepts Encapsulation grouping of related ideas into one unit which wecan refer to by a single name For example, methods, classes, packages Provides information hiding by restricting theexternal visibility of a unit’s information In OO A&D, the object is the unit ofencapsulation An object’s data is hidden behind the publicinterface (methods) of the objectOO Design 6OO Concepts… State Retention the functions of function-oriented design do not retain state;an object, on the other hand, is aware of its past andmaintains state across method invocations Identity – each object can be identified and treatedas a distinct entity very important issue, see lecture 10 Behavior – state and methods together define thebehavior of an object, or how an object responds tothe messages passed to itOO Design 7OO Concepts.. Classes – a class is a stencil from whichobjects are created; defines the structure andservices of a “class” of objects. A class has An interface which defines which parts of anobject can be accessed from outside A body that implements the operations Instance variables to hold object state Objects and classes are different; a class is atype, an object is an instance State and identity is associated with objectsOO Design 8OO Concepts – access Operations in a class can be Public: accessible from outside Private: accessible only from within the class Protected: accessible from within the class andfrom within subclassesOO Design 9Inheritance Inheritance is unique to OO and not availablein function-oriented languages/models If class B inherits information from class A, itimplicitly acquires the attributes and methodsof A Attributes and methods of A are reused by B When B inherits from A, B is the subclass orderived class and A is the base class orsuperclassOO Design 10Inheritance.. A subclass B generally has a derived part(inherited from A) as well as new attributes(new instance variables or methods) B’s specification only defines the new attributes This creates an “is-a” relationship objects of type B are also objects of type AOO Design 11Inheritance…OO Design 12Inheritance… The inheritance relationship between classesforms a class hierarchy In models, hierarchy should represent the naturalrelationships present in the problem domain In a hierarchy, all the common features of a set ofobjects can be accumulated in a superclass This relationship is also known as ageneralization-specialization relationship since subclasses specialize (or extend) the moregeneric information contained in the superclassOO Design 13OO Design 14Inheritance… There are several types of inheritance Strict inheritance: a subclass uses all of the features of itsparent class without modification The subclass only adds new attributes or methods Non-strict inheritance: a subclass may redefine features ofthe superclass or ignore features of the superclass Strict inheritance supports “is-a” cleanly and hasfewer side effects If a subclass redefines a method of the parent, it canpotentially break the contract that the superclass offers itsusersOO Design 15Inheritance… Single inheritance – a subclass inherits fromonly one superclass Class hierarchy is a tree Multiple inheritance – a class inherits frommore than one class Can cause runtime conflicts Repeated inheritance - a class inherits from aclass but from two separate pathsOO Design 16Inheritance and Polymorphism Inheritance enables polymorphism, i.e. anobject can be of different types An object of type B is also an object of type A Hence an object has a static type and adynamic type Implications on type checking Also brings dynamic binding of operations whichallows writing of general code where operationsdo different things depending on the typeOO Design 17Module Level Concepts Basic modules are classes During OO design, a key activity is to specify theclasses in the system being built In creating our design, we want it to be “correct” (i.e.cover its requirements) But a design should also be “good” – efficient, modifiable,stable, … We can evaluate an OO design using threeconcepts coupling, cohesion, and open-closed principleOO Design 18Coupling In OO design, three types of coupling exists interaction component inheritanceOO Design 19Coupling… Interaction coupling occurs when themethods of a class invoke methods ofanother class this can’t be avoided, obviously… but we want to ensure that an object’s publicinterface is used a method of class A should NOT directly manipulatethe attributes of another class B Why?OO Design 20Coupling… Component coupling – when a class A hasvariables of another class C A has instance variables of type C A has a method with a parameter of type C A has a method with a local variable of type C When A is coupled with C, it is coupled withall subclasses of C as well Component coupling will generally imply thepresence of interaction coupling alsoOO Design 21Coupling… Inheritance coupling – two classes arecoupled if one is a subclass of the other again, can’t be avoided, inheritance is a usefuland desirable feature of OO approaches however, a subclass should strive to only addfeatures (attributes, methods) to its superclass as opposed to modifying
View Full Document