• organize the code
• identify the purpose of procedures (what a block of code can do)
• isolate (modularize) the procedures (as individual functions)
• reuse the procedures (by function calls)
You basically learned those in the C class.
Object Oriented Programming:
A New Way of Modularizing
• a running computer in front of you: a computing “component”
• an (interactive) program: a computing “component”
• program A interacting with program B: another computing
“component”
• sub-program A.1 interacting with sub-program A.2: two mini-computing “components”
What does a computing “component” need?
• code: input → output
• associated data
Google Search Engine
• code: “search words” → “ranked list”
• associated data: user profile, web snapshot, etc
String Processor
• code: “string processing request” → “processed result”
• associated data: string content
Object-Oriented Programming: Program by
Object Oriented Programming
Computing Component
• associated data
• code: input → output
Object
• properties
• actions (methods): message → return value (and status change)
Object-Oriented Programming: Program by Letting Objects Interact with Each Other
• group related data together in design
1 / ∗ C ∗ /
2 t y p e d e f s t r u c t{
3 char dept [ 1 0 0 ] ;
4 char ID [ 1 0 0 ] ;
5 char name [ 1 0 0 ] ;
6 } Record ;
1 / ∗ Java ∗ /
2 c l a s s Record {
3 S t r i n g dept ;
4 S t r i n g ID ;
5 S t r i n g name ;
6 }
Object Oriented Programming 101-2
• use the struct/class to generate objects
1 / ∗ C ∗ /
2 Record r ;
3 Record∗ r p = ( Record ∗ ) m a l l o c (s i z e o f( Record ) ) ;
4 s t r c p y ( r . dept , " CSIE " ) ;
5 s t r c p y ( rp −>name , " HTLIN " ) ;
6 f r e e ( r p ) ;
1 / ∗ Java ∗ /
2 Record r = new Record ( ) ;
3 r . dept = " CSIE " ;
4 r . name = " HTLIN " ;
• don’t “do something on” the object; let the object “take some action”
1 / ∗ Java ∗ /
2 P r i n t S t r e a m ps = System . o u t ;
3 ps . p r i n t l n ( " CSIE " ) ;
4 S t r i n g s = " a , b , c " ;
5 t o k e n s = s . s p l i t ( " , " ) ;
From Noodle to Procedural to Object
• NOP: spaghetti code + (possibly spaghetti) data
• You can write NOP with virtually ANY languages
• Some easier to NOP (e.g. assembly), some harder
• POP: organized CODE + (possibly organized) data
• using procedures as the basic module –maintain, reuse
• action as separated procedures from data (do on the data)
• C, Pascal
• OOP: organized DATA + organized code (ACTION) grouped together
• using classes as the basic module
• action are closely coupled with data (data do something)
• Java, C++ (?), C#
• OOP: organized DATA + organized code (ACTION)
• using classes as the basic module
• action are closely coupled with data (data do something)
• Java, C++ (?), C#
• You can write virtually any-OP with any language
• OO design: think in the OO way
• OO language: help (force) you to think in the OO way
Three Levels of OO
• Object-Oriented Analysis (OOA):
what the system does
• from (customer/system) needs to software models
• an important topic in Software Engineering class
• Object-Oriented Design (OOD):
how the system does it
• from software model to class diagrams
• an important topic in Design Pattern class
• Object-Oriented Programming (OOP):
how to implement such a system
• from class diagrams to class implementations
• learn from this class
this class: justfundamentalOOP