IRCForumları - IRC ve mIRC Kullanıcılarının Buluşma Noktası
  sohbet

 Kayıt ol  Topluluk
Yeni Konu aç Cevapla
 
LinkBack Seçenekler Stil
Alt 17 Mart 2007, 20:49   #1
Çevrimdışı
Kullanıcıların profil bilgileri misafirlere kapatılmıştır.
IF Ticaret Sayısı: (0)
IF Ticaret Yüzdesi:(%)
5 adet PHP tasarım kalıbı (kodlarıyla)




Listing 1. Factory1.php
<?phpinterface IUser{ function getName();}class User implements IUser{ public function __construct( $id ) { } public function getName() { return "Jack"; }}class UserFactory{ public static function Create( $id ) { return new User( $id ); }}$uo = UserFactory::Create( 1 );echo( $uo->getName()."\n" );?>

Figure 1. The factory class and its related IUser interface and user class

Bu forumdaki linkleri ve resimleri görebilmek için en az 25 mesajınız olması gerekir.



% php factory1.php Jack%

Listing 2. Factory2.php
<?phpinterface 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 "Jack"; }}$uo = User::Load( 1 );echo( $uo->getName()."\n" );?>

Figure 2. The IUser interface and the user class with factory methods

Bu forumdaki linkleri ve resimleri görebilmek için en az 25 mesajınız olması gerekir.


Running the script on the command line yields the same result as the code in Listing 1, as shown here:
% php factory2.php Jack%

Listing 3. Singleton.php
<?phprequire_once("DB.php");class DatabaseConnection{ public static function get() { static $db = null; if ( $db == null ) $db = new DatabaseConnection(); return $db; } private $_handle = null; private function __construct() { $dsn = 'mysql://root:password@localhost/photos'; $this->_handle =& DB::Connect( $dsn, array() ); } public function handle() { return $this->_handle; }}print( "Handle = ".DatabaseConnection::get()->handle()."\n" );print( "Handle = ".DatabaseConnection::get()->handle()."\n" );?>

Figure 3. The database connection singleton

Bu forumdaki linkleri ve resimleri görebilmek için en az 25 mesajınız olması gerekir.


% php singleton.php Handle = Object id #3Handle = Object id #3%

Listing 4. Observer.php
<?phpinterface IObserver{ function onChanged( $sender, $args );}interface IObservable{ function addObserver( $observer );}class UserList implements IObservable{ private $_observers = array(); public function addCustomer( $name ) { foreach( $this->_observers as $obs ) $obs->onChanged( $this, $name ); } public function addObserver( $observer ) { $this->_observers []= $observer; }}class UserListLogger implements IObserver{ public function onChanged( $sender, $args ) { echo( "'$args' added to user list\n" ); }}$ul = new UserList();$ul->addObserver( new UserListLogger() );$ul->addCustomer( "Jack" );?>

Figure 4. The observable user list and the user list event logger

Bu forumdaki linkleri ve resimleri görebilmek için en az 25 mesajınız olması gerekir.


If you run this on the command line, you see this output:
% php observer.php 'Jack' added to user list%

Listing 5. Chain.php
<?phpinterface ICommand{ function onCommand( $name, $args );}class CommandChain{ private $_commands = array(); public function addCommand( $cmd ) { $this->_commands []= $cmd; } public function runCommand( $name, $args ) { foreach( $this->_commands as $cmd ) { if ( $cmd->onCommand( $name, $args ) ) return; } }}class UserCommand implements ICommand{ public function onCommand( $name, $args ) { if ( $name != 'addUser' ) return false; echo( "UserCommand handling 'addUser'\n" ); return true; }}class MailCommand implements ICommand{ public function onCommand( $name, $args ) { if ( $name != 'mail' ) return false; echo( "MailCommand handling 'mail'\n" ); return true; }}$cc = new CommandChain();$cc->addCommand( new UserCommand() );$cc->addCommand( new MailCommand() );$cc->runCommand( 'addUser', null );$cc->runCommand( 'mail', null );?>

Figure 5. The command chain and its related commands

Bu forumdaki linkleri ve resimleri görebilmek için en az 25 mesajınız olması gerekir.


If you run the script, which contains some test code, you see the following output:
% php chain.php UserCommand handling 'addUser'MailCommand handling 'mail'%

Listing 6. Strategy.php
<?phpinterface IStrategy{ function filter( $record );}class FindAfterStrategy implements IStrategy{ private $_name; public function __construct( $name ) { $this->_name = $name; } public function filter( $record ) { return strcmp( $this->_name, $record ) <= 0; }}class RandomStrategy implements IStrategy{ public function filter( $record ) { return rand( 0, 1 ) >= 0.5; }}class UserList{ private $_list = array(); public function __construct( $names ) { if ( $names != null ) { foreach( $names as $name ) { $this->_list []= $name; } } } public function add( $name ) { $this->_list []= $name; } public function find( $filter ) { $recs = array(); foreach( $this->_list as $user ) { if ( $filter->filter( $user ) ) $recs []= $user; } return $recs; }}$ul = new UserList( array( "Andy", "Jack", "Lori", "Megan" ) );$f1 = $ul->find( new FindAfterStrategy( "J" ) );print_r( $f1 );$f2 = $ul->find( new RandomStrategy() );print_r( $f2 );?>
The UML for this code is shown in Figure 6.

Figure 6. The user list and the strategies for selecting users

Bu forumdaki linkleri ve resimleri görebilmek için en az 25 mesajınız olması gerekir.


% php strategy.php Array( [0] => Jack [1] => Lori [2] => Megan)Array( [0] => Andy [1] => Megan)%

 
Alıntı ile Cevapla

IRCForumlari.NET Reklamlar
sohbet odaları reklam ver Benimmekan Mobil Sohbet
Cevapla

Etiketler
5, adet, kalibi, kodlariyla


Konuyu Toplam 1 Üye okuyor. (0 Kayıtlı üye ve 1 Misafir)
 

Yetkileriniz
Konu Acma Yetkiniz Yok
Cevap Yazma Yetkiniz Yok
Eklenti Yükleme Yetkiniz Yok
Mesajınızı Değiştirme Yetkiniz Yok

BB code is Açık
Smileler Açık
[IMG] Kodları Açık
HTML-Kodu Kapalı
Trackbacks are Kapalı
Pingbacks are Açık
Refbacks are Açık


Benzer Konular
Konu Konuyu Başlatan Forum Cevaplar Son Mesaj
Buz kalıbı içinde en fazla kalma Lilith Guinness Rekorları 0 10 Ekim 2014 14:31