Вы находитесь на странице: 1из 44

OBJECT ORIENTED PROGRAMMING

Course 1 Loredana STANCIU loredana.stanciu@aut.upt.ro Room B613

A SURVEY OF PROGRAMMING TECHNIQUES


Unstructured

programming

UNSTRUCTURED PROGRAMMING
Small and simple programs consisting only of one main program A sequence of commands or statements which modify data (global) throughout the whole program

UNSTRUCTURED PROGRAMMING

Disadvantage: in large programs using the same statement sequence


Idea

to extract these sequences sequences, name them and offering a technique to call and return from these procedures

A SURVEY OF PROGRAMMING TECHNIQUES


Unstructured

programming Procedural programming

PROCEDURAL PROGRAMMING
g To combine returning sequences of statements into one single place A procedure call is used to invoke the procedure With parameters and subprocedures programs can now be written more structured and error free

PROCEDURAL PROGRAMMING
A single program divided into small pieces called procedures To enable usage of general procedures or groups of procedures in other programs, programs they must be separately available

A SURVEY OF PROGRAMMING TECHNIQUES Unstructured programming Procedural programming Modular M d l programming g i g


MODULAR PROGRAMMING
Procedures of a common functionality are grouped together into separate modules A program is now divided into several smaller parts which interact through procedure d calls ll and d which form the whole program

EXAMPLE HANDLING SINGLE LISTS

To program a list in a modular programming language:


the

interface definition the th implementation i l t ti fil file

EXAMPLE HANDLING SINGLE LISTS


/* * Interface definition for a module which implements * a singly linked list for storing data of any type. */ MODULE Singly-Linked-List-1

BOOL list_initialize(); BOOL list_append(ANY data); BOOL list_delete(); list_end(); ANY list_getFirst(); _g (); ANY list_getNext(); BOOL list_isEmpty();

Hiding information

END Singly-Linked-List-1 Singly Linked List 1

EXAMPLE HANDLING MULTIPLE LISTS


/* * A list module for more than one list. */ MODULE Singly-Linked-List-2

DECLARE TYPE list_handle_t; list_handle_t list_create(); list_destroy(list_handle_t this); BOOL list_append(list_handle_t li d(li h dl this, hi ANY d data); ) ANY list_getFirst(list_handle_t this); ANY list_getNext(list_handle_t list getNext(list handle t this); BOOL list_isEmpty(list_handle_t this); List objects

END Singly Singly-Linked-List-2; Linked List 2;

MODULAR PROGRAMMING PROBLEMS


1)

Explicit Creation and Destruction

PROCEDURE foo() BEGIN


myList; myList <- list_create(); /* Do D something thi with ith myList Li t */ ... list_destroy(myList);
list list_handle_t handle t

END

MODULAR PROGRAMMING PROBLEMS


2)

Decoupled Data and Operations A structure based on the operations rather th than th th the d data t defined d fi d operations specify the data to be used In object-orientation, structure is organized by the data modules group data representations together

MODULAR PROGRAMMING PROBLEMS


3)

Missing Type Safety the compiler cannot guarantee for type safety f t a mechanism h i which hi h allows ll to t specify on which data type the list should be defined
list_handle_t<Apple> _ _ pp list1; ;/ /* a list of apples pp */ / list_handle_t<Car> list2; /* a list of cars */

MODULAR PROGRAMMING PROBLEMS


4)

Strategies and Representation a cursor is used to point to the current element l t a traversing t i g strategy t t g which hi h defines the order in which the elements of the data structure are to be visited to separate the actual representation or shape of the data structure from its traversing strategy

A SURVEY OF PROGRAMMING TECHNIQUES Unstructured programming Procedural programming Modular M d l programming g i g Object-oriented programming

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


solves some of the mentioned problems a web of interacting objects, each house-keeping its own state

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


HANDLING PROBLEMS

to obtain ones own , or abstract view, model, of the p problem

abstraction

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


HANDLING PROBLEMS
to t

define d fi properties ti of f the th problem: bl

the

data which are affected and the operations which are identified

an example: the administration of employees in an institution:


name,

size, date of birth, shape, social number, room number, hair colour, hobbies

p problem specific p properties p p data the abstract employees operations


OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


HANDLING PROBLEMS
abstraction b t ti

is i the th structuring t t i g of fa nebulous problem into well-defined entities by defining their data and operations p
combine data and operations which are not decoupled from each other

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


PROPERTIES OF ABSTRACT DATA TYPES
with ith

abstraction b t ti one can create t a wellll defined entity:


Define

the data structure of a set of items The data structure can only be accessed with defined operations interface exported by the entity abstract data type

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


PROPERTIES OF ABSTRACT DATA TYPES

1) It exports a type 2) It exports a set of operations. This set is called interface 3) Operations of the interface are the one and only access mechanism to the type's data structure 4) Axioms and preconditions define the pp domain of the type yp application

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


IMPORTANCE OF DATA STRUCTURE ENCAPSULATION

Encapsulation = The principle of hiding the used data structure and to only provide a well defined interface well-defined The separation of data structures and operations and the constraint to only access the data structure via a well-defined interface allows you to choose data structures appropriate for the application environment

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


IMPLEMENTATION OF ABSTRACT DATA TYPES

A class:
an actual representation of an Abstract Data Type provides implementation details for the data structure used and operations

class Integer { attributes: int i methods: setValue(int n) Integer addValue(Integer j) }

Definition ( (Class) ) A class is the implementation p of an abstract data type yp ( (ADT). ) It defines attributes and methods which implement the data structure and operations of the ADT, respectively.

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


An object:
uniquely

identifiable by a name the set of values at a particular time is the state of the object
Definition (Object) An object is an instance of a class. It can be uniquely id tifi d by identified b its it name and d it defines d fi a state t t which hi h i is represented t db by th the values l of its attributes at a particular time

Definition (Behaviour) The behaviour of an object is defined by the set of methods which can be applied on it.

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


A running program is a pool of objects where h objects bj are created, d destroyed d d and d interacting through messages
Integer

i; /* Define a new integer object */ i.setValue(1); /* Set its value to 1 */


Definition (Message) A message is a request to an object to invoke one of its methods. A message therefore contains: the th name of f the th method th d and d the arguments of the method. (Method) ) A method is associated with a class. An object j invokes Definition ( a method as a reaction to receipt of a message.

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


Relationships
between

two similar classes Is-A relationship

Between

objects Part-Of relationship

OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING


Packaging
collections

of classes and interfaces that are related to each other in some useful way benefits:
the

ability to organize many class definitions into a single unit. unit the "friendly" instance variables and methods are available to all classes within the same package, but not to classes defined outside the package

SUMMARY
A fundamental principle in object-oriented programming i to view i a program as a collection of interacting objects Objects in a collection:

react

upon receipt of messages, change their state according to invocation of methods which might cause other messages sent to other objects

WHAT IS JAVA?
Late 1980s C++ C was widely used to write OOP: not a platform independent needed d dt to b be recompiled il d f for each h different diff t CPUs 1991 a team of Sun Microsystems make a platform independent software and named it Oak 1995 Java

WHAT IS JAVA?

Java influenced by C, C++, Smalltalk


slogan

named Write Once Run Anywhere it can develop and run on any device equipped with Java Virtual Machine (JVM). applicable in all kinds of operating systems associated with the World Wide Web a platform independent programming language can be found in a variety of devices like cell phones, e-commerce application, PCs and almost all ll network t k or computing ti devices d i

THE JAVA LANGUAGE


all source code written in plain text files ending with the .java extension. source files compiled into .class class files by the javac compiler. .class l fil contains file t i bytecodes b t d the th machine hi language of the Java Virtual Machine. java launcher tool then runs your application with an instance of the Java Virtual Machine.

THE JAVA LANGUAGE

Java VM available on many different operating systems, the same .class files are p of running g on Microsoft Windows, , capable the Solaris TM Operating System (Solaris OS), Linux, or Mac OS

THE JAVA LANGUAGE

THE JAVA PLATFORM


A platform the hardware or software environment in which a program runs. Java a software-only software only platform that runs on top of other hardware-based platforms:

The

Java Virtual Machine The Java Application Programming Interface (API)


a

large collection of ready-made ready made software components that provide many useful capabilities grouped in packages

THE JAVA PLATFORM

JAVA CHARACTERISTICS

Simple, Object Oriented, and Familiar:


Simple

language, easy to learn Provides a clean and efficient object-based development platform Keeping the Java programming language looking like C++ as far as possible results in it being a familiar language

JAVA CHARACTERISTICS

Robust and Secure


Designed

for creating highly reliable software The memory management model is extremely simple: objects are created with a new operator
The

system will find many errors quickly Security features designed into the language and run-time run time system Java technology constructs applications that can't be invaded from outside

JAVA CHARACTERISTICS

Architecture Neutral and Portable


the

Java CompilerTM product generates bytecodes-an architecture neutral intermediate format designed to transport code efficiently to multiple hardware and software platforms programs are the same on every platform--there are no data type incompatibilities across hardware and software architectures

JAVA CHARACTERISTICS

High Performance
The

interpreter can run at full speed without needing to check the run run-time time environment The automatic garbage collector runs as a lowpriority p y background g thread

JAVA CHARACTERISTICS

Interpreted, Threaded, and Dynamic


The

Java interpreter can execute Java bytecodes directly on any machine to which the interpreter and run-time system have been ported Java technology's gy multithreading g capability p yp provides the means to build applications with many concurrent threads of activity (results in a high degree of interactivity for the end user) The language and run-time system are dynamic in their h i li linking ki stages

REFERENCES

, Introduction to Object-Oriented j Peter Muller, Programming Using C++, Chapters 1, 2, 3 and 4, http://www.desy.de/gna/html/cc/Tutorial/tutorial.html The Java Tutorials. Getting Started. http://java.sun.com/docs/books/tutorial/getStarted/i ndex.html

SCHEDULER
W 1 W 2 W 3 W 4 W 5 W 6 W 7 W 8 W 9 W 1 0 C W 1 1 E2 P1 W 1 2 E2 P2 W 1 3 W 1 4

C o u r s L a b

E1 P1

C E1 P2

L1

L2

L3

L4

L5

L6

L7

T1

L8

L9

L10 L11 T2

L12

Вам также может понравиться