1 / 38

Object-Oriented Design

Object-Oriented Design. A Historical P erspective. Programs Procedures to manipulate data Earlier: procedural Think actions/procedures first, data second 197 0’s : “object-oriented” Think data/objects first, procedures/actions second

pahana
Download Presentation

Object-Oriented Design

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. Object-Oriented Design

  2. A Historical Perspective • Programs • Procedures to manipulate data • Earlier: procedural • Think actions/procedures first, data second • 1970’s: “object-oriented” • Think data/objects first, procedures/actions second • Different perspectives on how to organize problem solving • and hence how to write programs

  3. Object-oriented (OO) Design • What are the different kinds of objects/data? • Design decision on how data are organized • OO terminology: class, object • Example: • Class Person: object mary, object john • Class Building: object empireStateBuilding, object olinEngineering

  4. Object-oriented (OO) Design • What are the different kinds of objects/data? • Design decision on how data are organized • OO terminology: class, object • Example: • Class Person: object mary, object john • Class Building: object empireStateBuilding, object olinEngineering • How can the objects/data be manipulated? • Design decision on how data can be manipulated • OO terminology: method (action) • Example: • Class Person: method getAddress, method changeAddress • Class Building: method getAddress • empireStateBuilding.getAddress() • john.changeAddress(newAddress)

  5. OO vs Procedural Design • Depending on the problems • OO is more appropriate • Many different types of data • Many interactions among different types of data • Procedural is more appropriate • Few types of data • Few interactions among different types of data

  6. OO and Java • Defining a class • In one .java file • Class name is the same as the file name • E.g. class Person is defined in Person.java • Each class has attributes (instance variables) • E.g. class Person has attributes: name, address, phone

  7. OO and Java • Defining a class • In one .java file • Class name is the same as the file name • E.g. class Person is defined in Person.java • Each class has attributes (instance variables) • E.g. class Person has attributes: name, address, phone • Defining methods • Methods are defined within a class • I.e. can’t define a method without a class • Name, parameters, return type

  8. class Person public class Person { private String _name; // attributes (instance variables) private String _address; private int _phone; }

  9. class Person public class Person { private String _name; // attributes (instance variables) private String _address; private int _phone; public Person(String name, String address, int phone)//constructor { _name = name; _address = address; _phone = phone; } }

  10. class Person public class Person { private String _name; // attributes (instance variables) private String _address; private int _phone; public Person(String name, String address, int phone)//constructor { _name = name; _address = address; _phone = phone; } public String getName() // method { return _name; } }

  11. Class PersonTest import java.util.*; class PersonTest { public static void main(String[] args) { // object jane created from class Person Person jane = new Person(”Jane Doe”, ”Melbourne”, 5551234 ); // get name from object jane and print it System.out.println(jane.getName()); } }

  12. Exercise • Create a Person class • Attributes (instance variables) • name, height (inches), salary (annual salary) • Methods • “constructor”, getName, getSalary, • getHeightInInches // return inches in int • getHeightInFeet// return feet in double • getHeightInMeters // return meters in double • getHeightInFeetAndInches // return an int array with feet and inches • Create a PersonTest class • Jane Doe, 61 inches, $55000 • Mike Smith, 71 inches, $44000 • Print Jane’s and Mike’s hourly rate--52 weeks, 40 hours/week • Print Jane’s height in feet, meters, and feet and inches. Same for Mike

  13. Abstraction (Information Hiding) • The Person class • Hides the internal attributes • private • Provides external access via its methods • public

  14. Hiding Some Info about Height • Look closer at height • What information are we hiding?

  15. Hiding some info about Height • Look closer at height • What information are we hiding? • how height is stored – different methods to get height

  16. Hiding some info about Height • Look closer at height • What information are we hiding? • how height is stored – different methods to get height • What can be changed in Person without changing PersonTest? • Imagine: • Person is written by a team in Florida • PersonTest is written by a team in California • Changes by the Florida team don’t affect the team in California

  17. Hiding some info about Height • Look closer at height • What information are we hiding? • how height is stored – different methods to get height • What can be changed in Person without changing PersonTest? • Imagine: • Person is written by a team in Florida • PersonTest is written by a team in California • Changes by the Florida team don’t affect the team in California • Attribute _height could be in feet and inches instead of inches • Changes in Person [by the Florida team] • But No changes in PersonTest! [by the California team]

  18. Hiding Height Better The constructor seems to imply the height must be stored in inches How can we improve this?

  19. Hiding Height Better The constructor seems to imply the height must be stored in inches How can we improve this? Multiple constructors

  20. class Person public class Person { private String _name; // attributes (instance variables) private int _height;// inches private int_salary;// annual salary public Person(String name, intheightInches, int salary)//constructor { } public Person(String name, intheightFeet, intheightInches, int salary) { } }

  21. Abstraction (Information Hiding) • Which constructor is more natural for external use? • Person(String name, intheightInches, int salary) • Person(String name, intheightFeet, intheightInches, int salary)

  22. Abstraction (Information Hiding) • Which constructor is more natural for external use? • Person(String name, intheightInches, int salary) • Person(String name, intheightFeet, intheightInches, int salary) • Which internal representation of attribute height uses less memory? • int height // in inches • intheightFeet, heightInches

  23. Abstraction (Information Hiding) • Which constructor is more natural for external use? • Person(String name, intheightInches, int salary) • Person(String name, intheightFeet, intheightInches, int salary) • Which internal representation of attribute height uses less memory? • int height // in inches • intheightFeet, heightInches • Abstraction (information hiding) allows: • a more natural interface for external use and • a more memory-efficient internal representation

  24. Wait a minute • Two constructors with the same name • Can we do that?

  25. Wait a minute • Two constructors with the same name • Can we do that? • Yes, if the parameter types are different • “overloading” • same name, different behavior • Applies to methods as well

  26. Overloading—same name,different sequence of parameter types • Given • Person(String name, intheightInches, int salary) • Person(String name, intheightFeet, intheightInches, int salary) • Can we also have • Person(String name, intheightInches, intheightFeet, int salary) • Person(String name, int salary, intheightFeet, intheightInches) • Person(intheightFeet, intheightInches, String name, intsalary)

  27. Exercise • class Person • Add a constructor that allows feet and inches • class PersonTest • Create a third Person object • Michael Jordan, 6 feet, 6 inches, $1,000,000 • Print his hourly rate, height in feet, meters, and feet and inches

  28. “this” object public intgetHeightInInches() { return _height; // return this._height; } ------PersonTest--- jane.getHeightInInches();

  29. Parameter is an object of the same class • Consider a method that compares two Person objects • Whether one is taller than another • In procedural thinking, we would want • isTaller(jane, mike) • In OO thinking, we think object first • jane.isTaller(mike) // yep, it looks ugly, but closer to English

  30. “this” object public booleanisTaller(Person other) { // _height refers to jane’s height // other._height refers to mike’s height return _height > other._height; // return this._height > other._height; } -------PersonTest--------- jane.isTaller(mike)

  31. A Second Class • Non-simple problems usually involve objects from different classes. • Let’s say we want to see how many minutes Michael Jordan need to work to buy an iPhone. • What would be another class in this problem? • What are the attributes? • What are the methods?

  32. Exercise • class Phone • Attributes: model, price • constructor, getPrice • class PersonTest • Create a Phone object: “iPhone 5c 16 GB unlocked”, $549 • Create another Phone object: “iPhone 5s 16GB unlocked”, $649 • Print how many minutes Michael Jordan needs to work to buy each of the iPhones

  33. A Note on Checking Invalid Input • You probably have learned about checking for invalid input • Same with methods, particularly with constructor • Invalid values for attributes cause problems later

  34. Checking Invalid Parameter Values public Person(String name, int height, int salary) { // if name is null, attribute _name should be what? // if height is negative, attribute _height should be what? // if salary is negative, attribute _salary should be what? }

  35. Checking Invalid Parameter Values public Person(String name, int height, int salary) { if (name != null) _name = name; else _name = ”Unknown”; if (height > 0) _height = height; else _height = 0; if (_salary > 0) _salary = salary; else _salary = 0; }

  36. Remember we have another constructor • Person(String name, intheightFeet, intheightInches, int salary) • We want to check for invalid parameter values too • Repeating instructions? • How to not repeat instructions?

  37. Remember we have another constructor • Person(String name, intheightFeet, intheightInches, int salary) • We want to check for invalid parameter values • Repeating instructions! • How to not repeat instructions? • Abstract the checking into a method

  38. “set” method with checking public Person(String name, intheight, int salary) { set(name, height, salary); } public Person(String name, intheightFeet, intheightInches, int salary) { set(name, heightFeet * 12 + heightInches, salary); } private void set(String name, int height, int salary) // public if you allow changes externally { if (name != null) _name = name; else _name = ”Unknown”; if (height > 0) _height = height; else _height = 0; if (_salary > 0) _salary = salary; else _salary = 0; }

More Related