1 / 24

Lecture 3 Object Orientation

SWE 316: Software Design and Architecture. Lecture 3 Object Orientation. Ch 2. NOTE: most slides are adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. 2/24. Object Orientation. Classes and Objects. Example. Polymorphism.

hasana
Download Presentation

Lecture 3 Object Orientation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SWE 316: Software Design and Architecture Lecture 3Object Orientation Ch 2 NOTE: most slides are adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  2. 2/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Before Object Orientation Real world concepts Skljkvjkvjfkavjafkk saovjsdvjfvkfjvkfjk Skljkvjkvjfkavjafkk saovjsdvjfvkfjvkfjk Skljkvjkvjfkavjafkk saovjsdvjfvkfjvkfjk Skljkvjkvjfkavjafkk saovjsdvjfvkfjvkfjk Software Design Entities

  3. 3/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Goals of Object Orientation AJAX BANK How Do We Express Ourselves? "Customers Montague and Susan entered the Ajax bank and were served by teller Andy ..... " Sec 2.1page 44

  4. 4/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered How We Express Ourselves "Customers Montague and Susan entered the Ajaxbank and were served by tellerAndy ..... " CLASSES OBJECTS Note that Java code convention reverses this capitalization.

  5. 5/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Object Orientation Real world concepts Skljkvjkvjfkavjafkk saovjsdvjfvkfjvkfjk Skljkvjkvjfkavjafkk saovjsdvjfvkfjvkfjk Skljkvjkvjfkavjafkk saovjsdvjfvkfjvkfjk Skljkvjkvjfkavjafkk saovjsdvjfvkfjvkfjk Direct correspondence Customer getFirstName() Transaction execute() Account getDetails() Software design entities Graphics reproduced with permission from Corel.

  6. 6/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Benefits of OO Object orientation provides a direct mapping between concepts and code

  7. 7/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Classes and Objects Real world Class in Design (UML notation) Class in Source code (Java) Sec 2.2page 46 class AjaxCustomer { . . . . } AjaxCustomer class PermissionToPay { . . . . } Mental concept PermissionToPay

  8. 8/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered The Members of a Class Non-static variable: One version for every object Objects of Auto Class model aliceBrownBMW:Auto … Auto public int vehicleID … protected int mileage … private myPrivateVariable … static int numAutosMade … 33024 mileage Static variable: One version only 12390924850984 numAutosMade Subclasses have these members too myToyota:Toyota … jaynesCar:Auto … 2105 83402 mileage mileage Toyota …

  9. 9/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Attribute Types (Shlaer & Mellor) • Naming: • fixed for each object • distinguishes individuals • Descriptive: • varies through life of object • Referential: • ties instance of one class to instance(s) of another • == aggregation Auto vehicleID mileage owner

  10. 10/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Classes and Objects • A class expresses a concept such as “HondaCivic.” • An object is an instance of a class such as “the Honda Civic with vehicle ID 89R783HJD894.”

  11. 11/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered The Clients of a Class class Customer { . . . String getName() { … } intcomputeBalance() { … } . . . } class AjaxAssets { . . . intcomputeAssets() { . . . Customer c = customers[ i ]; assets += c.computeBalance(); . . . } . . . } class AjaxWebsiteGenerator { . . . void makeProfile( Customer c ) { … String name = c.getName() … } . . . } Client of Customer Client of Customer

  12. 12/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Why OO is Useful for Application Development? • Class(Sec 2.2.1) • basic motive of Object Orientation • identifying parts that corresponds to the real world • Instantiation(Sec 2.2.2) • creating instances of encapsulated concepts • Inheritance(Sec 2.3.1) • capturing the way concepts occur in hierarchy • Polymorphism(Sec 2.3.2) • capturing use of single action word to represent different things, depending on context

  13. 13/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Requirements For e-Mail Creation Example Page 1 of 4 1. Summary: Produces e-mail text for various types of customers. 2. Detailed requirements 2.1 The application displays choices to the console, as shown in figure 2.13. 2.2 For customers delinquent more than 90 days, the e-mail message generated is the statement shown in figure 2.12.

  14. 14/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Requirements For e-Mail Creation Example Page 3 of 4 2.3 All non-delinquent customers receive a tailored e-mail messages as follows.  2.3.1 Mountain customers: This month we have a special on West Face tents. Only $700. ... lots more output specialized to mountaineering customers ... 2.3.2 Regular customers: All items are marked down 20% for this month only. ... lots more output for regular customers ...

  15. 15/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Requirements For e-Mail Creation Example Page 4 of 4 2.4 The e-mail is to be displayed on the console. 3. Future enhancements We anticipate that the text is likely to change frequently, and that new kinds of customers are likely to be specified, each with its own new set of requirements.

  16. 16/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Disadvantages of Branching • Code for each case not cohesive (“cohesive”: forms a comprehensible unity) • All types of customers coded together in single class Expensive to … • … add new functionality • bloat switch or if - then code • … remove functionality • hunt for all parts that must be removed • … change functionality • hunt for all parts that must be changed

  17. 17/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Aspects of the Customer Design Needing Improvement • We need to visualize the design • Code not an effective way to understand design • The design’s maintainability still has flaws • As the application grows, specialized class(es) will be required to interact with the user

  18. 18/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered What’s Needed to Specify Functionality • Name of the function Example: add • Argument types(if any) Example: • First parameter: integer • Second parameter: float • Return type Example: double, reference type, void • Exceptions (if any) Example: IOException • More(?) • Are parameters inputs and/or outputs? • Describe what the function does (natural language)

  19. 19/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Polymorphism • the use of several versions of a method, one in each derived class. • This enables objectOfBaseClass.theMethod() to be interpreted variously at runtime, depending on what derived class objectOfBaseClass belongs to.

  20. 20/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered The Need For Interfaces: Simplify … • class Draw • { … • intsetColor( String ) { … } • Pen getStandardPen() { … } • intgetLogoStyle() { … } • void setColor( int ) { … } • void drawLogo( int, int ) { … } • void speedUpPen( int ) { … } • … • }

  21. 21/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Interface Example: a Draw Class } • Functions dealing with the pen used • Pen getStandardPen() • void speedUpPen( int ) • . . . • Functions dealing with the colors available • void setColor( int ) • intsetColor( String ) • . . . • Functions covering the drawing of the company’s logo • void drawLogo( int, int ) • intgetLogoStyle() • . . . Pen interface } Color interface } Logo interface

  22. 22/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Interfaces An interface is a set of function prototypes (each with name, parameter types, return type, exception type).

  23. 23/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Issues to be Addressed • How do we visualize a set of classes? • How can classes relate to each other? • How should classes relate to each other? • How can we describe functionality occurring among several classes? • How do we describe the manner in which objects respond to events occurring on them? • Are there patterns of class usage that recur? • So we can existing reuse design parts

  24. 24/24 Object Orientation Classes and Objects Example Polymorphism Interfaces Things to be considered Summary of This Chapter • A Class represents a concept • Example: House • An Object is an instance of a class • Example: 23 Main Street, Springfield • Classes can relate in several ways: Mainly … • A Client of a class refers to that class in one of its methods • Inheritance: “kind of” relationship • Aggregation: “has a” relationship, explained in chapter xx • Polymorphism means “action depends on context” • Executing anObject.aMethod() actually executes the version of aMethod() in the subclass that anObject belongs to

More Related