formations/algo/poo/cours/mechanism.txt

97 lines
3.9 KiB
Plaintext
Raw Normal View History

2017-09-04 17:33:56 +02:00
Mechanismes objets
===================
L'impossible définition
------------------------
Pour être qualifié de Orienté Objet, un langage doit être **pur**.
2017-09-04 17:33:56 +02:00
.. glossary::
2017-09-04 17:33:56 +02:00
langage pur
2017-09-04 17:33:56 +02:00
Langage dans lequel tout est objet. Exemples : smalltalk, python
2017-09-04 17:33:56 +02:00
- We should stop using the term "Object Oriented".
- plusieurs définitions : http://wiki.c2.com/?DefinitionsForOo
- on ne peut pas en choisir une seule : http://wiki.c2.com/?NobodyAgreesOnWhatOoIs
2017-09-04 17:33:56 +02:00
Quelques essais de définition technique :
2017-09-04 17:33:56 +02:00
- Les objets sont des dictionnaires, des clefs-valeurs qui peuvent contenir
des pointeurs sur des fonctions. Le reste n'est que sucre syntaxique.
2017-09-04 17:33:56 +02:00
- Variante: "objects are maps that can map interfaces and/or references to other such maps."
2017-09-04 17:33:56 +02:00
- OO is the view of **EverythingIsa** a behavior (including data structures)
2017-09-04 17:33:56 +02:00
- OO is the "natural extension" of programmer-defined types (sometimes known as user-defined types).
2017-09-04 17:33:56 +02:00
- Inheritance + Encapsulation (no polymorphism): "Inheritance is what separates abstract data type (ADT) programming from OO programming."
2017-09-04 17:33:56 +02:00
class based programming
------------------------
2017-09-04 17:33:56 +02:00
In a class-based language (like Python JavaLanguage or SmalltalkLanguage), every object is an instance of a class.
An object contains its own data (instance variables), but its class holds its behaviour (methods). To make a new object, you ask a class to "instantiate" itself.
2017-09-04 17:33:56 +02:00
An Object Oriented system, language, or environment should include at least Encapsulation, Polymorphism, and Inheritance.
There is a largely accepted definition of OO: roughly the style encouraged by
Java (classes, encapsulation, inheritance, subtype polymorphism, and even
sometimes genericity).
https://en.wikipedia.org/wiki/Class-based_programming
encapsulation
--------------
- d'attributs
- de méthode (exposer une interface mais on ne sait pas comment cette interface est codée)
Delegation
2017-09-04 17:33:56 +02:00
Than please define what you think delegation is. Let's start with something concrete:
If the method call::
2017-09-04 17:33:56 +02:00
targetObject.method(x1,x2,...,x_n)
2017-09-04 17:33:56 +02:00
Is then resolved in the body of TargetClass.method() to:
delegate.method(x1,x2,...,x_n)
Yet another reason is the popularity of languages that brand themselves "OO".
C++, Java, Python, Javascript, Self… New ones pop up daily. That doesn't help
define "OO", but it does makes it ever more present.
les méthodes spéciales
----------------------
Les méthodes spéciales permettent de changer la manière dont Python
réagit à certains opérateurs et accède aux attributs dune instance.
Les méthodes spéciales sont entourées de deux "__"
2017-09-04 17:33:56 +02:00
La liaison retardée
-------------------
Internal polymorphism is the kind of polymorphism that you see in most OO computer languages. When you call a method on an object the actual function called is based on the dynamic type of the object.
Prototypes
----------
A prototype is an object that is used as a template to create other objects. 2 mechanisms are used for this: copy and delegation. When creating a derived object from a prototype, you can copy part of the prototype. The part that isn't copied can still be accessed through a delegation pointer stored in the derived object. When you try to access a member of the derived object, and it isn't there, we walk up the delegation pointer to ask the prototype instead.
Prototype based programming have lots of interesting implications, that I have no experience with. I don't know how if it helps you write shorter or more modular code, especially compared to other mechanisms. I will just note that prototypes are easily implemented (or emulated) in any dynamic language with first class functions and associative maps.
https://en.wikipedia.org/wiki/Prototype-based_programming
In a prototype-based language, an object can contain both data and behaviour. It's a self-contained thing. To make a new object, you just call the "copy" method on an existing object.
http://wiki.c2.com/?PrototypeBasedProgramming