• Aucun résultat trouvé

Protocoles Standards Fonctions Génériques Instanciation

N/A
N/A
Protected

Academic year: 2022

Partager "Protocoles Standards Fonctions Génériques Instanciation"

Copied!
29
0
0

Texte intégral

(1)

Approches Objet de la Programmation

e CLOS Avancé E Didier Verna

EPITA / LRDE

didier@lrde.epita.fr

lrde/~didier @didierverna didier.verna in/didierverna

(2)

Plan

Protocoles Standards Fonctions Génériques Instanciation

eql Specializers Méta-Classes

Principe

Applications

Redéfinitions

(3)

Plan

Protocoles Standards Fonctions Génériques Instanciation

eql Specializers

Méta-Classes

Redéfinitions

(4)

Fonctions Génériques

Protocole standard

before methods

primary methods

after methods around methods (call-next-method)

Combinaison impérative Combinaison déclarative

I Rôles des méthodes :

I primary: travail principal, retour de valeur I before/ after: effets de bords

(5)

Application : Accumulation Déclarative

(defmethod hello ((human human))

(format t "Hello! I'm ~A, ~Am, ~Ayo.~%"

(name human) (size human) (age human)) (values))

(defmethod hello :after ((employee employee)) (call-next-method)

(format t "Working at ~A for ~A euros, started at the age of ~A.~%"

(company employee) (salary employee) (hiring-age employee)) (values))

(6)

Combinaisons de Méthodes

I Utilisation d’une ou plusieurs méthodes (primaires) applicables I Spécification déclarative plutôt qu’impérative

I Combinaison standard (par défaut) : méthode la plus spécifique + before, after et around

I Combinaisons disponibles (built-in)

I +,min,max,list,nconc, append,and,or,progn I Nibefore, niafter

I call-next-methodinterdit

I Programmation de nouvelles combinaisons

I define-method-combination(macro) I Deux formes

I Voir aussi [Verna, 2018]

(7)

Application

Rappel

Platypus Mammal

-weight : unsigned +nurse () : void +weight () : unsigned

Oviparous -weight : unsigned +nurse () : void +weight () : unsigned

I weight : problème traité (fusion des définitions de slot)

I nurse ?

(8)

Application

La combinaison progn

(defgeneric nurse (animal) (:method-combination progn))

(defmethod nurse progn ((mammal mammal)) (when (next-method-p) (call-next-method)) (format t "I suckle my children.~%") (values))

(defmethod nurse progn ((oviparous oviparous)) (when (next-method-p) (call-next-method)) (format t "I brood my eggs.~%")

(values))

I Note : pas de méthode spécifique aux platypus requise

(9)

Instanciation

I make-instance :

calcul et validité des options d’initialisation

I allocate-instance :

allocation d’un objet non initialisé I initialize-instance :

appel à shared-initialize I shared-initialize :

initialisation des slots I Note : fonctions génériques

spécialisables

Protocole

make-instance

allocate-instance initialize-instance

shared-initialize Couche 2 Couche 3

(10)

Application : Vérification Post-Initialisation

(defmethod initialize-instance :after ((human human) &key) (slot-value human 'name)

(slot-value human 'size)

(incf (slot-value human 'population))) ;; bonus

(defmethod initialize-instance :after ((employee employee) &key) (slot-value employee 'company)

(slot-value employee 'salary) (slot-value employee 'hiring-year))

I Voir également :

I slot-boundp (fonction)

I slot-unbound(fonction générique) I unbound-slot(condition)

I Remarque : vérification dynamique (run-time)

(11)

eql Specializers

I Spécialisation sur des objets particuliers Example : pattern matching

(defgeneric product (x y)

(:method (x (y (eql 0))) 0)

(:method ((x (eql 0)) y) 0)

(:method (x y) (* x y)))

(12)

eql Specializers

I Les objets peuvent être des classes Example : une classe singleton (defclass singleton (#|...|# )

(#|...|#)) (let (instance)

(defmethod make-instance

((class (eql (find-class 'singleton))) &key)

(or instance (setf instance (call-next-method)))))

(13)

eql Specializers

I Les objets peuvent être des (noms de) classes Example : un faux slot partagé

(defclass human (#|...|#) (#|...|#))

(let ((population 0))

(defmethod initialize-instance :after ((human human) &key) (incf population))

(defgeneric census (obj)

(:method ((human human)) population)

(:method ((class (eql (find-class 'human)))) population) (:method ((symbol (eql 'human))) population)))

(14)

Plan

Protocoles Standards Méta-Classes

Principe

Applications

Redéfinitions

(15)

Méta-Classes Utilisateur

I Principe :

I Spécialiser le comportement d’un ensemblede classes I Classe de classes (méta-classe)

I Processus :

1. Créer une méta-classe (sous-classerstandard-class) 2. Macrologie (couche 1)

3. Déclarer sa validité (couche 3 / MOP) 4. Spécialiser (option :metaclassdedefclass)

(16)

Application : Classes Abstraites

(defclass abstract-class (standard-class) ()) (defmethod validate-superclass

((class abstract-class) (superclass standard-class)) (defmethodt) validate-superclass

((class standard-class) (superclass abstract-class)) t)

(defmethod make-instance :before ((class abstract-class) &key) (error "~A is an abstract class." (class-name class)))

;; (defclass foobar (#...#)

;; (#...#)

;; (:metaclass abstract-class))

(17)

Application : Classes Finales

(defclass final-class (standard-class) ()) (defmethod validate-superclass

((class final-class) (superclass standard-class)) (defmethodt) validate-superclass

((class standard-class) (superclass final-class)) (defmethodnil) validate-superclass

((class final-class) (superclass final-class)) nil)

;; (defclass foobar (#...#)

;; (#...#)

;; (:metaclass final-class))

(18)

Application : Classes Singleton

(defclass singleton-class (standard-class) ((instance :initform nil)))

(defmethod validate-superclass

((class singleton-class) (superclass standard-class)) (defmethodt) validate-superclass

((class standard-class) (superclass singleton-class)) nil)

(defmethod make-instance ((singleton-class singleton-class) &key) (or (slot-value singleton-class 'instance)

(setf (slot-value singleton-class 'instance) (call-next-method))))

;; (defclass foobar (#...#)

;; (#...#)

;; (:metaclass singleton-class))

(19)

Application : Classes Comptables

(defclass counting-class (standard-class) ((counter :initform 0)))

(defmethod validate-superclass

((class counting-class) (superclass standard-class)) (defmethodt) validate-superclass

((class standard-class) (superclass counting-class)) t)

(defmethod make-instance :after ((counting-class counting-class) &key) (incf (slot-value counting-class 'counter)))

;; (defclass foobar (#...#)

;; (#...#)

;; (:metaclass counting-class))

(20)

Application : un (Autre) Faux Slot Partagé

(defclass population-class (standard-class) ((population :initform 0 :accessor population))) (defmethod validate-superclass

((class population-class) (superclass standard-class)) (defmethodt) validate-superclass

((class standard-class) (superclass population-class)) t)

(defclass human () (#|...|#) (:metaclass population-class)) (defmethod initialize-instance :after ((human human) &key)

(incf (population (find-class 'human)))) (defgeneric census (object)

(:method ((class population-class)) (population class))

(:method ((symbol (eql 'human))) (population (find-class 'human))) (:method ((human human)) (population (find-class 'human))))

(21)

Plan

Protocoles Standards Méta-Classes

Redéfinitions

(22)

Redéfinitions

I Rappels (Langages statiques / MOB1) :

I Classe ⇔Type

Fixe, connue à la compilation I Objet⇔Valeur

Variables de type fixe, connu à la compilation

I Nouveau contexte :

I Langage dynamique

Les valeurs (6=variables) portent leur propre information de type I MOP

Chaque composant du système objet est lui-même un (méta-)objet, donc modifiable

I Nouvelles fonctionnalités :

I Ajout / Suppression / Modification de classes, métodes, fonctions génériques

I Changement de classe d’une instance

(23)

Rappel : Héritage par Restriction

I Le problème cercle/ellipse (carré/rectangle) I Un carré est un rectangle…

I …mais avec des contraintes statiques…

I …et dynamiques

I Programmation Différentielle :

I Hériter de manière additive

et non restrictive I Problème surtout lié à la

mutation

I Cf. le principe de substitution de Liskov

Exemple UML

Rectangle

#width : unsigned

#height : unsigned +set_width (unsigned) : void

Square

(24)

Changement de Classe

I change-class :

modification destructive d’un objet (sans changement d’identité) I

update-instance-for-different-class

:

validité des options d’initialisation I shared-initialize :

initialisation des slots I Note : fonctions génériques

spécialisables

I Attention : bien choisir son moment !

Protocole

change-class

u-i-f-d-c

shared-initialize Couche 2 Couche 3

(25)

Application : Carrés / Rectangles

Pseudo-UML

shape center : point

rectangle width, height

square width point

x, y

object

«instanceof» «instanceof»

(26)

Côté Rectangle

(defun make-rectangle (width height) (if (= width height)

(make-instance 'square :width width)

(make-instance 'rectangle :width width :height height))) (defmethod (setf width) :after (width (rectangle rectangle))

(when (= (width rectangle) (height rectangle)) (change-class rectangle 'square)))

(defmethod (setf height) :after (height (rectangle rectangle)) (when (= (width rectangle) (height rectangle))

(change-class rectangle 'square)))

(27)

Côté Carré

(defclass square (shape)

((width :initarg :width :reader width :reader height :accessor side))) (defmethod (setf width) (width (square square))

(let ((side (side square))) (unless (= width side)

(change-class square 'rectangle :width width :height side))) width)

(defmethod (setf height) (height (square square)) (unless (= height (side square))

(change-class square 'rectangle :height height)) height)

(28)

Plan

Bibliographie

(29)

Bibliographie

Didier Verna

Method Combinators

ELS, 2018

Références

Documents relatifs

Plus formellement, le problème peut se décrire par un hypergraphe encodant les relations de dépendance entre les tâches d’entrée et tâches de sortie ainsi que des temps

We constructively rewrite bounded-treewidth (resp., bounded- pathwidth) circuits to d-SDNNFs (resp., uOBDDs) in time linear in the circuit and singly exponential in the treewidth,

While upper bounds on wdim ε X are obtained by writing down explicit maps to a space of the proper dimension (these constructions use Hadamard matrices), lower bounds are found

By transforming the three phase quantities to appropriate de components, the proportional and integral (PI) controllers are able to lead the estimated dq

In [5], it was shown that the Check problem of ghw and fhw becomes tractable if the underlying hypergraphs satisfy certain properties such as bounded (multi-)intersection width,

Besides the theoretical guarantees on the cost of evaluating class of queries having bounded (fractional) hypertree width, experimental evidence of the benefits gained from

SHUTDOWN OPTIONS (see Block Diagram) Since both the compensation and soft-start termi- nals (Pins 9 and 8) have current source pull-ups, either can readily accept a pull-down

Abstract: We describe a method for the formal determination of signal bit width in fixed points VLSI implementations of signal processing algorithms containing loop nests.. The