🎉 سال نو، مهارت نو، مشاوره رایگان نقشه راه برنامه نویسی (آفر ویژه ثبت نام قبل از افزایش قیمت 🔥)
۰ ثانیه
۰ دقیقه
۰ ساعت
۰ فراز صالحی
پیوست کد Adapter
جامعه پی اچ پی ایجاد شده در ۲۱ اردیبهشت ۱۴۰۰

سلام , 

 

کد پترن آداپتر اینجا می‌زارم.

 

امیدوارم مفید واقع شود.

 

<?php
// third party api
class Kavenegar
{
    public function BySms()
    {
        echo 'Sms Has sent' . PHP_EOL;
    }
    public function ByEmail()
    {
        echo 'Email has sent' .PHP_EOL;
    }
}
// Adapter class :
class Notification
{       
    private $notifiy;
    public function __construct(Kavenegar $kavenegar)
    {
        $this->notifiy = $kavenegar;
    }
    public function sendSms()
    {
        $this->notifiy->BySms();
    }
    public function sendEmail()
    {
        $this->notifiy->ByEmail();
    }
}
// Local App  :
class User
{
    private $notifiy;
    public function __construct(Notification $notifiy)
    {
        $this->notifiy = $notifiy;
    }
    public function create()
    {
        echo 'User has craeted' . PHP_EOL;
        $this->notifiy->sendSms();
    }
    public function delete()
    {
        echo 'User has deleted'. PHP_EOL;
        $this->notifiy->sendEmail();
    }
}
//---------------------------------------
$kavenegar = new Kavenegar;
$user = new User(new Notification($kavenegar));
$user->create();
$user->delete();