begin process at 2012 05 26 06:30:05
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Class et Objet ( POO )

 > OBSERVER PATTERN ORIENTÉ DÉVELOPPEMENT ÉVÉNEMENTIEL DÉCLARATIF

OBSERVER PATTERN ORIENTÉ DÉVELOPPEMENT ÉVÉNEMENTIEL DÉCLARATIF


 Description

L'Observer / Observable pattern est super pratique car il vous permet de faire du code évènementiel. Contrairement à la majorité des implémentations, j'ai utilisé quelques tricks afin de simplifier le côté déclaratif côté gestion d'événements.

Exemple de déclaration :

class car extends ObservableBase {
  const evtAccelerate = 1; // declare event
  public function speedUp() {
    $this->raise(self::evtAccelerate);
  }
}

Exemple d'attachement

$car = new car();
$car->onAccelerate('myFunction');
function myFunction(&$sender, &$args) { ...

Un exemple plus complet :

Source

  • <?php
  • /**
  • * Observer Design Pattern
  • * @author I. CHIRIAC
  • * @package Events
  • */
  • require_once('events.lib.php');
  • class MyEventArg extends EventArgs {
  • public $counter = 0;
  • }
  • /**
  • * Defines a basic observable object
  • */
  • class observable extends ObservableBase {
  • /**
  • * Defines event const
  • * Important : events names definition must start with evt
  • */
  • const evtDoSomeThing = 1;
  • // define a simple instance variable
  • public $name;
  • // constructor
  • public function __construct($name) { $this->name = $name; }
  • /**
  • * Defines an object action who will raise an event
  • */
  • public function doSomeThing() {
  • // Raising event by passing constant definition
  • $this->raiseEvent(self::evtDoSomeThing, new MyEventArg());
  • }
  • }
  • /**
  • * Defines a basic observer
  • */
  • class observer {
  • public function __construct(observable $observable) {
  • $observable->onDoSomeThing(
  • array(
  • $this,
  • 'HandleSomeThing'
  • )
  • );
  • }
  • public function HandleSomeThing(observable &$sender, MyEventArg &$args) {
  • $args->counter ++;
  • echo 'Observer can catch that '.$sender->name.' was doing something / '.$args->counter.' <br />';
  • }
  • }
  • /**
  • * DEFINES A STATIC EVENT : FOR ALL INSTANCES
  • */
  • // FOR PHP 5.x to 5.3
  • observable::attach(observable::evtDoSomeThing, 'HandleDoSomeThing');
  • // FOR PHP 5.3 and SUP
  • // observable::onDoSomeThing('HandleDoSomeThing');
  • // DO ACTIONS
  • $toto = new observable('toto');
  • /**
  • * Defines a specific instance event for toto
  • */
  • $toto->onDoSomeThing('TotoDoSomeThing');
  • /**
  • * Creating an object
  • */
  • $observer = new observer($toto);
  • // CALLING
  • $toto->doSomeThing();
  • /**
  • * Create another instance
  • */
  • $titi = new observable('titi');
  • $titi->doSomeThing();
  • /**
  • * DEFINE EVENTS
  • */
  • function HandleDoSomeThing(observable &$sender, MyEventArg &$args) {
  • $args->counter ++;
  • echo $sender->name.' was doing some thing / '.$args->counter.'...<br />';
  • }
  • function TotoDoSomeThing(observable &$sender, MyEventArg &$args) {
  • $args->counter ++;
  • echo'Go on toto / '.$args->counter.' !<br />';
  • $args->stopBubble();
  • }
  • ?>
<?php

	/**
	 * Observer Design Pattern
	 * @author I. CHIRIAC 
	 * @package Events	 	 
	 */	 	 	
	require_once('events.lib.php');

	class MyEventArg extends EventArgs {
		public $counter = 0;
	}

	/**
	 * Defines a basic observable object   
	 */	 	
	class observable extends ObservableBase {
		/**
		 * Defines event const 
		 * Important : events names definition must start with evt		 
		 */		 		
		const evtDoSomeThing = 1;
		// define a simple instance variable
		public $name;
		// constructor
		public function __construct($name) { $this->name = $name; }		 
		/**
		 * Defines an object action who will raise an event
		 */		 		
		public function doSomeThing() {
			// Raising event by passing constant definition
			$this->raiseEvent(self::evtDoSomeThing, new MyEventArg());
		}	
	}
	
	/**
	 * Defines a basic observer
	 */	 	
	class observer {
		public function __construct(observable $observable) {
			$observable->onDoSomeThing(
				array(
					$this, 
					'HandleSomeThing'
				)
			);
		}
	  public function HandleSomeThing(observable &$sender, MyEventArg &$args) {
			$args->counter ++;
			echo 'Observer can catch that '.$sender->name.' was doing something / '.$args->counter.' <br />';
		}
	}
	
	/**
	 *	DEFINES A STATIC EVENT : FOR ALL INSTANCES
	 */
	
	// FOR PHP 5.x to 5.3
	observable::attach(observable::evtDoSomeThing, 'HandleDoSomeThing');
  // FOR PHP 5.3 and SUP
	// observable::onDoSomeThing('HandleDoSomeThing');


	// DO ACTIONS
	$toto = new observable('toto');

	/**
	 * Defines a specific instance event for toto
	 */	 	
	$toto->onDoSomeThing('TotoDoSomeThing');

	/**
	 * Creating an object
	 */	 	
	$observer = new observer($toto);
	
	// CALLING 
	$toto->doSomeThing();
	
	/**
	 * Create another instance
	 */	 	
	$titi = new observable('titi');
	$titi->doSomeThing();

	/**
	 * DEFINE EVENTS
	 */	 	
	function HandleDoSomeThing(observable &$sender, MyEventArg &$args) {
		$args->counter ++;
		echo $sender->name.' was doing some thing / '.$args->counter.'...<br />';
	}
	function TotoDoSomeThing(observable &$sender, MyEventArg &$args) {
		$args->counter ++;
		echo'Go on toto / '.$args->counter.' !<br />';
		$args->stopBubble();
	}


?>

 Conclusion

Fonctionnalités :

- Attachement d'un callback
  * par instance
  * par class

- Gestion de l'événement
  * Avoir la référence de l'appellant
  * Avoir la référence de l'argument
  * Pouvoir stopper la propagation

- Gestion de la déclaration
  * On déclare les événements avec une constante
  * On vérifie si l'événement existe et est déclaré

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

CLASSE WIKILOC
Source avec Zip SERVICE DESIGN PATTERN / CHARGEMENT DYNAMIQUE DE SERVICES D'...
Source avec Zip Source avec une capture WHOIS DOMAIN CLASS : LOOKUP & AVAIBILITY
Source avec Zip Source avec une capture XML MAPPING TO CLASS OBJECTS / CHARGEMENT / PARSING / MODIFI...
Source avec Zip SQLDIFF : MIGRATION, VERSIONNING ET SYNCHRONISATION DE LA ST...

 Sources de la même categorie

Source avec Zip GÉNÉRATION AUTOMATIQUE DE FICHIER .CLASS.PHP EN FONCTION D'U... par ig3
CLASSE D'OBJET DE CRYPTAGE ET DÉCRYPTAGE DE CHAINES DE CARAC... par 8Tnerolf8
Source avec Zip MY.DEVIANTART API par inwebo
CLASSE DE GESTION DE "VARIABLES GLOBALES D'ENVIRONNEMENT" par pifou25
Source avec Zip COLLECTION.CLASS.MIN.PHP par thunderhunter

 Sources en rapport avec celle ci

Source avec Zip [PHP5] OBSERVER DESIGN PATTERN par malalam

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

XML insérer un élément tout au dessus [ par Straw ] Bonjour &#224; tous, je pose cette question dans le cadre de mon m&#233;moire... que je dois rendre dans 12jours Je d&#233;veloppe un site flash-x Problème avec mon tewte défilant [ par LilSiM ] Bonjour à tous ! Voilà j'ai un petit problème avec mon code html : je veux faire défiler du texte dans un block avec une image de fond fixe, j'ai don


Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

Photothèque

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,092 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales