<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Latest snippets tagged pattern</title>
  <link rel="alternate" href="http://snippets.prendreuncafe.com/snippets/tagged/pattern/order_by/date"></link>
  <id>http://snippets.prendreuncafe.com/snippets/tagged/pattern/order_by/date</id>
  <updated>2006-12-26T12:53:53Z</updated>
  <author>
    <name>Symfony</name>
    <author_email>noreply@symfony-project.com</author_email>
  </author>
<entry>
  <title>Multiton en PHP5</title>
  <link href="http://snippets.prendreuncafe.com/snippet/30"></link>
  <updated>2006-12-26T12:53:53Z</updated>
  <id>30</id>
  <summary type="html">[code]
class Multiton
{
  private $instances = array();
  
  public function getInstance($var1, $var2, $var3)
  {
    $uid = md5((string)$var1.(string)$var2.(string)$var3);
    if (!isset(self::$instances[$uid])) 
    { 
      self::$instances[$uid] = new Multiton($var1, $var2, $var3);
    }
    return self::$instances[$uid];
  }

}
[/code]</summary>
</entry>
<entry>
  <title>Le pattern Strategy en PHP5</title>
  <link href="http://snippets.prendreuncafe.com/snippet/17"></link>
  <updated>2006-11-07T21:56:03Z</updated>
  <id>17</id>
  <summary type="html">[code]
&lt;?php
interface IStrategy
{
  function filter( $record );
}

class FindAfterStrategy implements IStrategy
{
  private $_name;

  public function __construct( $name )
  {
    $this-&gt;_name = $name;
  }

  public function filter( $record )
  {
    return strcmp( $this-&gt;_name, $record ) &lt;= 0;
  }
}

class RandomStrategy implements IStrategy
{
  public function filter( $record )
  {
    return rand( 0, 1 ) &gt;= 0.5;
  }
}

class UserList
{
  private $_list = array();

  public function __construct( $names )
  {
    if ( $names != null )
    {
      foreach( $names as $name )
      {
        $this-&gt;_list []= $name;
      }
    }
  }

  public function add( $name )
  {
    $this-&gt;_list []= $name;
  }

  public function find( $filter )
  {
    $recs = array();
    foreach( $this-&gt;_list as $user )
    {
      if ( $filter-&gt;filter( $user ) )
        $recs []= $user;
    }
    return $recs;
  }
}

$ul = new UserList( array( &quot;Andy&quot;, &quot;Jack&quot;, &quot;Lori&quot;, &quot;Megan&quot; ) );
$f1 = $ul-&gt;find( new FindAfterStrategy( &quot;J&quot; ) );
print_r( $f1 );

$f2 = $ul-&gt;find( new RandomStrategy() );
print_r( $f2 );
?&gt;
[/code]</summary>
</entry>
<entry>
  <title>Le pattern ChainOfCommands en PHP5</title>
  <link href="http://snippets.prendreuncafe.com/snippet/16"></link>
  <updated>2006-11-07T21:54:35Z</updated>
  <id>16</id>
  <summary type="html">[code]
&lt;?php
interface ICommand
{
  function onCommand( $name, $args );
}

class CommandChain
{
  private $_commands = array();

  public function addCommand( $cmd )
  {
    $this-&gt;_commands []= $cmd;
  }

  public function runCommand( $name, $args )
  {
    foreach( $this-&gt;_commands as $cmd )
    {
      if ( $cmd-&gt;onCommand( $name, $args ) )
        return;
    }
  }
}

class UserCommand implements ICommand
{
  public function onCommand( $name, $args )
  {
    if ( $name != 'addUser' ) return false;
    echo( &quot;UserCommand handling 'addUser'\n&quot; );
    return true;
  }
}

class MailCommand implements ICommand
{
  public function onCommand( $name, $args )
  {
    if ( $name != 'mail' ) return false;
    echo( &quot;MailCommand handling 'mail'\n&quot; );
    return true;
  }
}

$cc = new CommandChain();
$cc-&gt;addCommand( new UserCommand() );
$cc-&gt;addCommand( new MailCommand() );
$cc-&gt;runCommand( 'addUser', null );
$cc-&gt;runCommand( 'mail', null );
?&gt;
[/code]</summary>
</entry>
<entry>
  <title>Le pattern Observer en PHP5</title>
  <link href="http://snippets.prendreuncafe.com/snippet/15"></link>
  <updated>2006-11-07T21:53:28Z</updated>
  <id>15</id>
  <summary type="html">[code]
&lt;?php
interface IObserver
{
  function onChanged( $sender, $args );
}

interface IObservable
{
  function addObserver( $observer );
}

class UserList implements IObservable
{
  private $_observers = array();

  public function addCustomer( $name )
  {
    foreach( $this-&gt;_observers as $obs )
      $obs-&gt;onChanged( $this, $name );
  }

  public function addObserver( $observer )
  {
    $this-&gt;_observers []= $observer;
  }
}

class UserListLogger implements IObserver
{
  public function onChanged( $sender, $args )
  {
    echo( &quot;'$args' added to user list\n&quot; );
  }
}

$ul = new UserList();
$ul-&gt;addObserver( new UserListLogger() );
$ul-&gt;addCustomer( &quot;Jack&quot; );
?&gt;
[/code]</summary>
</entry>
<entry>
  <title>Le pattern Factory en PHP5</title>
  <link href="http://snippets.prendreuncafe.com/snippet/14"></link>
  <updated>2006-11-07T21:50:28Z</updated>
  <id>14</id>
  <summary type="html">[code]
&lt;?php
interface IUser
{
  function getName();
}

class User implements IUser
{
  public static function Load( $id ) 
  {
        return new User( $id );
  }

  public static function Create( ) 
  {
        return new User( null );
  }

  public function __construct( $id ) { }

  public function getName()
  {
    return &quot;Jack&quot;;
  }
}

$uo = User::Load( 1 );
echo( $uo-&gt;getName().&quot;\n&quot; );
?&gt;
[/code]</summary>
</entry>
<entry>
  <title>Singleton en PHP5</title>
  <link href="http://snippets.prendreuncafe.com/snippet/3"></link>
  <updated>2006-10-11T16:28:17Z</updated>
  <id>3</id>
  <summary type="html">Instance unique d'un objet.

[code]
&lt;?php 
class Singleton 
{ 
    private static $_instance ; 
 
    private function __construct() { }
     
    public static function GetInstance() 
    { 
        if (!isset(self::$_instance)) 
        { 
            self::$_instance = new Singleton() ; 
        } 
        return self::$_instance; 
    }
}
?&gt;
[/code]</summary>
</entry>
</feed>