Programowanie Obiektowe Marek Moszyński p.744 http://knot805.eti

Transkrypt

Programowanie Obiektowe Marek Moszyński p.744 http://knot805.eti
JPO
Programowanie Obiektowe
Marek Moszyński p.744
http://knot805.eti.pg.gda.pl/po
Rys historyczny
•Abak (3000pne –Babilonia; 500pne -Rzym,Grecja)
•Arytmometr Pascala (XVIIw – 1644r)
•Maszyna Babbage’a (XIXw – 1820r)
•Ada Lovelass (córka Byron’a) – pierwsza programistka
•Z3,Z4 (Konrad Zuse, Niemcy 1941)
•COLOSSUS (Wielka Brytania 1943)
•ENIAC (Filadelfia 1943-1945)
•Fortran (1957)
•Microprocessor (1971)
Paradygmaty programowania
•functional - LISP, Scheme, ML, Haskel
•imperative/procedural - Pascal, FORTRAN, COBOL, Tcl
•logical - Prolog
•modular – Modula 2, Ada
•concurrency – Ada
•object oriented - Smalltalk, C++, Java, Python,
ML
fun fac : (int -> int) 0 = 1
| fac n = n * fac (n-1);
1. Functional
Lisp is the classic example of a functional language (although by
no means the only one for example, ML is a very widely used
functional language).
These languages place far more emphasis on applying a function
(often recursively) to a set of one or more data items.
The function would then return a value - the result of evaluating
the function. If the function changed any data items then this was a
side effect of the function. There is (was) limited support for more
algorithmic solutions which might rely on repetition via iteration.
The functional approach turned out to be extremely useful as a
way of implementing complex systems for early AI researchers.
2. Procedural
Exemplified by languages such as Pascal and C.
These were an attempt to move programming languages to a higher
level (than the earlier assembler languages).
The emphasis was now on algorithmic solutions and on procedures
which operated on data items.
They were extremely effective, but software developers still
encountered difficulties. This was partly due to the increased
complexity of the systems typically being developed.
It was also because, although high level procedural languages
removed the possibility of certain types of error occurring and
increased productivity, developers could still cause problems for
themselves. For example, the interfaces between different parts of the
system might be incompatible, but this might not become obvious
until integration testing or system testing.
3. Modular
•Languages such as Modula-2 and Ada employ modularization.
•In these languages a module hides its data from module users.
•The users of the module can only access that data via defined
interfaces.
•These interfaces are “published” so that users know what
interfaces are available (and their definitions) and can check
that they are using the correct versions.
4. Object oriented
This is the most recent “commercial” programming paradigm.
This approach can be seen as taking modularization a step
further.
Not only do you have explicit modules (in this case objects) but
these objects can inherit features from one another.
We can of course ask “why another programming paradigm?”.
The answer to this partly lies in the failure of many software
development projects to keep to budget, remain within time
scales and to give the user what they want.
Of course, it should never be assumed that object orientation is
the answer to all these problems, it is really just another tool
available to software developers.
Początki OOPL
„
Programowanie obiektowe wywodzi się z pewnych własności języka
SIMULA, który powstał w latach sześćdziesiątych.
„
Pierwszym językiem programowania obiektowego lub jak nazywają to
jego twórcy - językiem obiektowo zorientowanym (Object Oriented
Language) był Smalltalk skonstruowany na początku lat 80 -tych
zeszłego stulecia. Głównym jego twórcą jest Adele Goldberg.
Historia OOPL
Języki programowania
Compiled by François Labelle from statistics on open-source projects at SourceForge
What’s new in programming languages
• Commercial trend over past 5 years
– Increasing use of type-safe languages: Java, C#, …
– Scripting languages, other languages for web applications
• Perl is widely known as “the duct-tape of the Internet”
• Teaching trends
– Java replacing C as most common intro language
• Less emphasis on how data, control represented in machine
• Research and development trends
– Modularity
• Java, C++: standardization of new module features
– Program analysis
• Automated error detection, programming env, compilation
– Isolation and security
• Sandboxing, language-based security, …
Cechy dobrych PL
•Readability
- easy to read and understand the code - COBOL
•Writeability
- easy to write: easy to understand syntax, fewer keystrokes - C
•Othogonality
-commands are consistent and usually only one way to do something
- Algol-68
•Platform independence
- can be used across multiple platforms - Java
•Reliability
- programs once debugged usually work correctly
- Ada is a good example, C++ is a bad example
•Low cost
- compiler is inexpensive, execution and compiling of programs is
relatively inexpensive
- Java has $0 cost compiler
Programowanie obiektowe
Tradycyjne podejście do programowania, zwane programowaniem proceduralnym
charakteryzuje się rozdzieleniem danych od operujących na nich funkcji.
Odizolowanie danych od kodu może prowadzić do przypadkowych zmian danych przez
funkcje, które nie są z nimi logicznie związane. Ponadto modyfikacja programu napisanego
w ten sposób jest trudna do realizacji, gdyż nawet niewielka zmiana działania programu
może spowodować konieczność wprowadzenia poprawek w wielu miejscach.
Takich wad pozbawione jest programowanie obiektowe, będące po programowaniu
strukturalnym kolejnym etapem w rozwoju języków programowania.
Programowanie obiektowe jest stylem programowania, w którym do
tworzenia programów używa się obiektów.
obiekt = dane + metody
„ Styl taki powstał w wyniku postrzegania rzeczywistości jako zbioru
obiektów różnego typu, które mogą wykonywać określone czynności,
potrafią się ze sobą komunikować i na siebie wzajemnie oddziaływać.
„ Obiekty w programie często odzwierciedlają cechy i umiejętności
swoich odpowiedników ze świata rzeczywistego.
„
What is an Object?
An object is a software bundle of related variables and methods.
Software objects are often used to model real-world objects you
find in everyday life.
Visual representation of a software object
A bicycle modeled as a software object
What is a Class?
A class is a blueprint or prototype that defines the variables and
the methods common to all objects of a certain kind.
¾ Class is an implementation of an abstract data type and
so encapsulates both data and operations.
¾ Object is run-time instance of class.
¾Classes just sit there, objects do the real work.
¾Memory is allocated for Objects not for Classes.
What is a Message?
Software objects interact and communicate with each other
using messages.
¾ The object to which the message is addressed (YourBicycle)
¾ The name of the method to perform (changeGears)
¾ Any parameters needed by the method (lowerGear)
Mechanizmy OOP
definiowanie nowego typu danych, łączącego dane
i działające na nich metody w jedną całość.
„ definiowanie nowego typu obiektowego, zwanego potomnym, na
podstawie typu istniejącego, zwanego bazowym. Operacja
dziedziczenia powoduje przeniesienie danych i metod
z typu bazowego do typu potomnego.
„ definiowanie metod wirtualnych, które mają tę samą nazwę w typie
bazowym i potomnym, ale określają różne działanie.
„
Przykazania OO
1. Encapsulation (ukrycie danych)
2. Inheritance (dziedziczenie)
3. Polymorphism (wiele+kształt, metody wirtualne)
4. Abstraction, Protection (abstrakcja)
•Dynamic Binding – właściwość języka
1.Encapsulation or data hiding
•Encapsulation is the process of hiding all the details of an
object that do not contribute to its essential characteristics .
•Essentially, it means that what is inside the class is hidden;
only the external interfaces are known (by other objects).
•That is, as the user of an object you should never need to
look inside the box!
2. Inheritance
•In many cases objects may have similar (but not identical)
properties.
•One way of managing (classifying) such properties is to have
a hierarchy of classes.
•In this hierarchy of classes a class inherits both from its
immediate parent class, above it in the hierarchy, and from
classes above the parent.
•This inheritance mechanism permits common characteristics
of objects to be defined once, but used in many different
places. Any change is thus localized.
3. Abstraction
•An abstraction denotes the essential characteristics of an
object that distinguishes it from all other kinds of objects
and thus provides crisply defined conceptual boundaries,
relative to the perspective of the viewer.
•That is, it states how a particular object differs from all
others.
4. Polymorphism
•This is the ability to send the same message to different
instances which may appear to perform the same function.
•However, the way in which the message is handled will
depend on the class of which the instance is an example.