بالا
گزارش خرابی با ارسال پیامک
رویای نیم روز
web@noondreams.com NoonDreams.com, The Web Solutions Conmpany همراه: 09177085527

چند ریختی-Polymorphism

در این مقاله به این موضوع توجه داشته باشید که مسئولیت نوشتن از کلاس Article جدا شده. به همین خاطر دو دسته کلاس ایجاد شده. دسته کلاس Writer ها و دسته کلاس Article ها. به این جدا سازی Strategy Pattern می گویند.

This article is a simplfied version of this URL:  http://net.tutsplus.com/tutorials/php/understanding-and-applying-polymorphism-in-php/

Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.

One important principle of OOP is that a class should do one thing, and it should do it well.

Interfaces

An integral part of polymorphism is the common interface. There are two ways to define an interface in PHP: interfaces and abstract classes. Both have their uses, and you can mix and match them as you see fit in your class hierarchy. Ex: Button inteface (????) , Button is clickable so you can implement different buttons making sure all of them are clickable by implementing Button class.

====== The Tut ==============

Ex: You have an Article class and you want to write it in several formats:

So you need an Interface like

WriterI : To make all subclasses implement(????) a method called write

WriterXXX : a bunch of classes like XMLWriter , TEXTWriter etc who implemet WriterI

Artcicle : a class of which the client makes objects and then call write method on it. like $article -> write()

FactoryWriter:  I'll be resposible for replacing XXX in WriterXXX and return classes like XMLWriter, TEXTWriter etc using lets say a server requests like post or get or whatever.

 

class Article
{

    public $title ;
    public $author ;
    public $date ;
    public $category ;

    public function __construct( $title , $author , $date , $category = 0 )
    {
        $this -> title = $title ;
        $this -> author = $author ;
        $this -> date = $date ;
        $this -> category = $category ;
    }

    //It means send me a button, I will press it for you

    public function write( WriterI $writer )
    {
        return $writer -> write ( $this ) ;
    }

}
Ok let's put together all the classes mentioned above:
interface WriterI
{

    public function write( Article $obj ) ;
}
class FactoryWriter
{

    public static function getWriter()
    {
        // grab request variable  
        $format = $_REQUEST[ 'format' ] ;  // A string like XML , JSON
        // construct our class name and check its existence  
        $class = 'Writer' . $format ;
        if ( class_exists ( $class ) )
        {
            // return a new Writer object  
            return new $class() ;
        }
        // otherwise we fail  
        throw new Exception ( 'Unsupported format' ) ;
    }

}
class WriterJSON implements WriterI
{

    public function write( Article $obj )
    {
        $array = array( 'article' => $obj ) ;
        return json_encode ( $array ) ;
    }

}
And the last section is a bootstrap like
$article = new Article ( 'Polymorphism' , 'Steve' , time () , 0 ) ;

try
{
    $writer = FactoryWriter::getWriter () ;
}
catch ( Exception $e )
{
    $writer = new WriterXML() ;
}

echo $article -> write ( $writer ) ;

In this tut you've created a WriterI interface and then you made sure any implementing class can write it. Actually this is the way polymorphism works for this example: Hey Factory give me the  sutaible writer, I have an Article and I want that sutaible Writer to write it for me. So the Article class decouples from the writing strategy, and simply asks it's writer object to do it.

نظرات
نام:
ایمیل(پنهان):
نظر:
کدامنیتی:
 تاریخ: 2013/01/26 22:17:15
 نام: Mohammad Rahmani
 نظر: خواهش می کنم لطفا اگه نظری دارید بنویسید تا اعمال بشه
 تاریخ: 2013/01/26 22:04:47
 نام: احسان
 نظر: ممنون، عالی بود
 تاریخ: 2013/01/26 14:32:09
 نام: مهران احدی
 نظر: راحت باش محمد جان! ;)
 تاریخ: 2013/01/26 13:33:27
 نام: Mohammad Rahmani
 نظر: آقا مهران ببخشید در حال تست هستیم
 تاریخ: 2012/09/10 00:46:06
 نام: مهران احدی
 نظر: مثال ساده و قابل فهم و خوبی زده بودید. همونطور که گفتین، interface و Abstract class، از هر دوی اونها میشه استفاده کرد، هر چند تو این مثال استفاده از Interface صحیحه و شما هم دقیقاً همین کار رو کردید. ممنون.
با آرزوی موفقیت برای وبسایت شما.
 تاریخ: 2012/09/08 23:18:40
 نام: محمد رحمانی
 نظر: البته یکی از دوستان می گه این polymorphism نیست. میگه فقط یکی از حالت های polymorphism هست