1 / 49

Object-Oriented Programming and Design

Object-Oriented Programming and Design. Ralph Johnson johnson@cs.uiuc.edu. Goals: to learn. Principles Patterns Practices “Object think” Design for reuse. History of Objects. Simula’67 Smalltalk 72,74,76,78,80 1985 C++ (Objective C, Object Pascal) 1986 OOPSLA 1990 COM, CORBA

ovid
Download Presentation

Object-Oriented Programming and 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 Programming and Design Ralph Johnson johnson@cs.uiuc.edu Object-oriented programming and design

  2. Goals: to learn • Principles • Patterns • Practices • “Object think” • Design for reuse Object-oriented programming and design

  3. History of Objects • Simula’67 • Smalltalk 72,74,76,78,80 • 1985 C++ (Objective C, Object Pascal) • 1986 OOPSLA • 1990 COM, CORBA • 1994 Design Patterns • 1995 Java • 2001 .NET Object-oriented programming and design

  4. Design Principles • Encapsulate what varies • Code to an interace rather than to an implementation • Open-closed Principle: Classes should be open to extension and closed to modification • Don’t Repeat Yourself (DRY) • Single Responsibility Principle • Liskov Substitution Principle: Subtypes must be substitutable for their base types Object-oriented programming and design

  5. Smalltalk • Pure object-oriented language • Live objects • Readable library • Influencial, unknown • Fun! Object-oriented programming and design

  6. From Smalltalk • WIMP - Windows, Mice, Pointing • Model/View/Controller • Browsers • CRC cards • eXtreme Programming • Automated refactoring tools Object-oriented programming and design

  7. Before objects Computer system consists of data and programs. Programs manipulate data. Programs organized by • functional decomposition • dataflow • modules Object-oriented programming and design

  8. Object Paradigm Computer system consists of a set of objects. Objects are responsible for knowing and doing certain things. Objects collaborate to carry out their responsibilities. Programs organized by classes, inheritance hierarchies and subsystems Object-oriented programming and design

  9. What is an object, anyway? Mystical view Computing systems are made up of objects that communicate only by sending messages between each other. All computation is message sending. Object-oriented programming and design

  10. What is an object, anyway? Scandinavian view A program is a simulation. Each entity in the system being simulated is represented by an entity in the program. Object-oriented programming and design

  11. What is an object, anyway? Programming language view An object-oriented system is characterized by • data abstraction • inheritance • polymorphism by late-binding of procedure calls Object-oriented programming and design

  12. Heart of Object-Oriented Programming Don't make a new language, extend your old one. Objects should be abstractions of problem domain. Object-oriented programming and design

  13. Modeling All phases of software life-cycle are modeling • analysis - modeling of problem • design - modeling of solution • implementation - making model run on a computer • maintenance - fixing/extending your model Object-oriented programming and design

  14. Assumption about Modeling Basing system design on structure of problem makes system • more reusable • more extensible Object-oriented programming and design

  15. Modeling Claim: people model the world with "objects" • objects • classes • relationships between objects • relationships between classes Object-oriented programming and design

  16. Modeling Advantages of object-oriented software development • more natural - matches the way people think • single notation - makes it easy to move between software phases Object-oriented programming and design

  17. Objects and Relationships John is Mary's father. Mary is John's daughter. Bob is Mary's dog. Mary is Bob's owner. Ann is John's employer. John is Ann's employee. Object-oriented programming and design

  18. Objects and Attributes John's name is "John Patrick O'Brian". John's age is 27. John's address is 987 N. Oak St, Champaign IL 61820 What about John's job? John's wife? What is an attribute, and what is a relationship? Object-oriented programming and design

  19. Objects and Behavior John goes on a trip. John makes reservations. John buys tickets. John travels by airplane. John checks into hotel. Object-oriented programming and design

  20. What Really is an Object? Reservation -- a promise to give service to a customer Ticket -- proof that customer has paid for service in advance Flight Payment -- an event (transaction?) in which money is exchanged Object-oriented programming and design

  21. What Really is an Object? Anything we can talk about can be an object, including relationships ("the husband of the first party", "first-born son"). What are we trying to model? Models should be as simple as possible, but no simpler. Object-oriented programming and design

  22. Classification We naturally put objects into classes that have similar characteristics. • John is a man. • Mary is a woman. • Bob is a dog. • All women are people. • All people are mammals. Mammals Dogs People Women Men Bob Mary John Object-oriented programming and design

  23. Classification • John is an employee. • John is a father. • John is a sky-diver. • John is under 30. Is John an instance of Employee, Father, Sky-diver, and Under30? Object-oriented programming and design

  24. Summary Objects • have identity • have attributes • have behavior • have relationships with other objects Object-oriented programming and design

  25. Summary Classes • describes the attributes, behavior, and relationships of a set of objects • subclasses/superclasses form graph of generalizations Object-oriented programming and design

  26. The class • First month is Smalltalk • Homework, often several per week • Second half of the semester is group project • Put your name on the wiki at http://swiki.cs.uiuc.edu/cs598rej • Download Squeak from squeak.org Object-oriented programming and design

  27. Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses Object-oriented programming and design

  28. Smalltalk: Everything is an Object • Application objects: customer, inventory • GUI objects: button, text editor • Foundation: string, set, numbers, booleans • Tools: browser, debugger, compiler, GUI builder • Language implementation: class, method, context, Object-oriented programming and design

  29. Communicating with an Object All computation is done by objects. The only way to interact with an object is to "send a message" to it. Smalltalk has three kinds of syntax for sending a message. All messages have same implementation. Object-oriented programming and design

  30. Three Kinds of Message Syntax Unary messages aSalaryChange date Keyword messages earned at: date add: money Binary messages (worked - 40) max: 0 Object-oriented programming and design

  31. Sending a Message Object responds to message by looking in its class for a method with the same selector. If it doesn't find the method, it looks in its superclass. Repeat until it finds the method. If it never does, there is an error. Object-oriented programming and design

  32. Smalltalk in a nutshell • Classes have methods and variables. • Methods are a sequence of messages, assignments, returns. • Variables, literals, psuedovariables • Blocks • Metaclasses - classes have classes Object-oriented programming and design

  33. Message Lookup and Inheritance EmployeeTransaction has subclasses Paycheck, SalaryChange, and Timecard. EmployeeTransaction defines the instance variable date and the method: date ^date Object-oriented programming and design

  34. EmployeeTransaction date check1 07/09/95 Paycheck aPaycheck date

  35. Smalltalk Expression Syntax Literals 3.675, 14, 'hello', #weight, $d, #( #foo 'bar' 92) Assignments and variables v := v + 1 Messages Object-oriented programming and design

  36. Smalltalk Expression Syntax Sequences. Blocks. x < y ifTrue: [z := x] ifFalse: [z := y]. paychecks do: [:each | each post] Cascades aSet add: #blue; add: #red Object-oriented programming and design

  37. Smalltalk Method Syntax Returns ^socialSecurity + federalTaxes + stateTaxes Object-oriented programming and design

  38. Variables in Smalltalk • blockArguments and temporaries • methodArguments and temporaries • instanceVariables Can be accessed only by the object's methods. • Globals, class variables Any method in the scope can access it Variable is object of class Association Object-oriented programming and design

  39. Using Variables printOnCheckStream: aStream aStream cr; cr. aStream next: 40 put: (Character space). DateFormat print: date on: aStream. aStream cr. ... Object-oriented programming and design

  40. More variables test "PayrollSystem test" | payroll day1 ralph | day1 := Date newDay: 5 year: 1996. payroll := self new. ralph := Employee new name: 'Ralph Johnson'. Object-oriented programming and design

  41. (continued) ralph changeSalaryFor: day1 to: 20. payroll addEmployee: ralph. self employee: ralph hours: self aLittleOvertime starting: day1. ^payroll Object-oriented programming and design

  42. Initializing an Object Every object needs to be initialized. Uninitialized variables are nil. The initialization method often defines the type of a variable. Two methods: one class and one instance. Object-oriented programming and design

  43. Class Methods Class is an object. It can have methods, too. For class Date class newDay: dayInteger year: yearInteger ^self new day: dayInteger year: yearInteger Object-oriented programming and design

  44. Instance initializing method For class Date day: dayInteger year: yearInteger day := dayInteger. year := yearInteger Object-oriented programming and design

  45. Creating a Date Date newDay: 3 year: 1995 Date new day: 3 year: 1995 day day: 3 year: 1995 year 3 1995 Object-oriented programming and design

  46. Complete Smalltalk Expressions (method definition) message, assignment, sequence, block, return Variables, literals Classes, inheritance Class methods / instance methods Pseudovariables nil, true, false self, super, thisContext Object-oriented programming and design

  47. Smalltalk (the language) is trivial Complexity is class library. New language extensions fit in as well as numbers and control structures. Language extension => core language is trivial Object-oriented programming and design

  48. Implications class library = language extension => must know class library must standardize class library merging class libraries is like merging language extensions hard to make class libraries Object-oriented programming and design

  49. Applications • No programs, just objects • No “main” routine • Most applications create new classes, reuse a lot of existing ones Object-oriented programming and design

More Related