490 likes | 781 Views
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
E N D
Object-Oriented Programming and Design Ralph Johnson johnson@cs.uiuc.edu Object-oriented programming and design
Goals: to learn • Principles • Patterns • Practices • “Object think” • Design for reuse Object-oriented programming and design
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
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
Smalltalk • Pure object-oriented language • Live objects • Readable library • Influencial, unknown • Fun! Object-oriented programming and design
From Smalltalk • WIMP - Windows, Mice, Pointing • Model/View/Controller • Browsers • CRC cards • eXtreme Programming • Automated refactoring tools Object-oriented programming and design
Before objects Computer system consists of data and programs. Programs manipulate data. Programs organized by • functional decomposition • dataflow • modules Object-oriented programming and design
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
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
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
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
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
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
Assumption about Modeling Basing system design on structure of problem makes system • more reusable • more extensible Object-oriented programming and design
Modeling Claim: people model the world with "objects" • objects • classes • relationships between objects • relationships between classes Object-oriented programming and design
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
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
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
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
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
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
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
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
Summary Objects • have identity • have attributes • have behavior • have relationships with other objects Object-oriented programming and design
Summary Classes • describes the attributes, behavior, and relationships of a set of objects • subclasses/superclasses form graph of generalizations Object-oriented programming and design
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
Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses Object-oriented programming and design
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
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
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
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
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
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
EmployeeTransaction date check1 07/09/95 Paycheck aPaycheck date
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
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
Smalltalk Method Syntax Returns ^socialSecurity + federalTaxes + stateTaxes Object-oriented programming and design
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
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
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
(continued) ralph changeSalaryFor: day1 to: 20. payroll addEmployee: ralph. self employee: ralph hours: self aLittleOvertime starting: day1. ^payroll Object-oriented programming and design
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
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
Instance initializing method For class Date day: dayInteger year: yearInteger day := dayInteger. year := yearInteger Object-oriented programming and design
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
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
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
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
Applications • No programs, just objects • No “main” routine • Most applications create new classes, reuse a lot of existing ones Object-oriented programming and design