Professional Documents
Culture Documents
26.02.08
Programming Paradigms
OOP
Data Abstraction
Structured Programming
Imperative Programming
Input values
Procedure
Main block
Input values
Function
Ouput value(s)
Language syntax
Organization of code in blocks of instructions Definition of functions and procedures Extension of the language with new operations Calls to new functions and procedures
double module (double u1, double u2) { double m; m = sqrt (u1*u1 + u2*u2); return m; } void main () { printf (%lf, module (4, -2)); }
Pablo Castells
26.02.08
Data Abstraction
z x y
Language syntax
Modules are associated to data types Not necessarily new syntax w.r.t. modular programming
Operation Abstraction
Entrada Entrada struct vector { double x; double y; };
TAD Example in C
arg1
arg1
arg2
arg3
Salida
Salida
void construct (vector *u, double u1, double u2) { u >x u->x = u1; u->y = u2; } void main () { double module (vector u) vector u; { construct (&u, 4, -2); double m; printf (%lf, module (u)); m = sqrt (u.x*u.x + u.y*u.y); } return m; }
10
TAD Extensibility
... double product (vector u, vector v) { return u.x * v.x + u.y * v.y; } void main () { vector u, v; construct (&u, 4, -2); construct (&v, 1, 5); printf (%lf, product (u, v)); }
11
12
Pablo Castells
26.02.08
Functions Object
Language syntax:
Class definitions Functions explcitly associated to classes Creation of objects Access to attributes, method invocation
13 14
Variables
15
16
17
18
Pablo Castells
26.02.08
OO Languages
Simula (1967) Smalltalk (1980) C++ (1983, 1990) Object Pascal (1988) Lisp CLOS (1989) Java (1995, 1997, 1998...) Pure vs. hybrid languages
OOP Elements
Objects: attributes + methods Methods: operations on objects g j properties and operations p Classes: categories of objects with common p p Class hierarchies In some languages classes are objects
Particular case: prototype-instance paradigm
19
20
Implementation
Employee
Specific language elements for relations Regular attributes that contain references (pointers) to objects
21
22
Manipulation
Data
x . name x . net_salary ( )
Destruction
Code that is executed to perform operations
Interface Methods
23
Free memory Destroy parts, if any Remove references to the destroyed object (e.g. supervisor)
24
Pablo Castells
26.02.08
Object Hierarchy
Person
Structure Inheritance
name birth Person
Type hierarchy
Person x Employee y = create Employee division gross salary
Employee l
Customer
Employee l
Customer
Manager
category
Manager
25
26
Functionality Inheritance
show personal data
specialization
Ease of Extension
Reuse
Person
Person
specialization
Modularity y
calculate net salary
inheritance
Employee
Customer
Employee l
Customer
Administrative
Manager
Manager
27
28
Multiple Inheritance
year courses register (course) Student MSc year of graduation specialty qualifications y t
Graduate Student year of graduation specialty qualifications year courses register (course)
y z
D x y z t
29 30
Pablo Castells
26.02.08
Polimorfism
Overloading
Line l1 = create Line Line l2 = create Line Vector v = create Vector l1.parallel (r2) l1.parallel (v)
Dynamic Linking
Calculate area of selected figure
area ( ) Figure g
Dynamic linking
Person x Employee y = create Employee x=y x.show_data x.show data ( ) y.show_data ( )
Overriding (specialization)
Person x = create Person Employee y = create Employee x.show_data ( ) y.show_data ( ) Triangle area ( ) Rectangle area ( ) Ellipse area ( )
31
32
33
34
?
B f (A x)
b . f (b)
?
C c = create C D d = create D
d . f (c)
35
36
Pablo Castells
26.02.08
Class Diagram
Object Diagram
38
Formed by
Formed by
Canvas
Group
39
Pablo Castells