[Design Patterns]: Adapter

The adapter pattern adjusts the interface of one class to match that of another.

Drawback:

  • You’re just hiding a bad design
class NotificationAdapter
{
   protected $username = '';
   protected $password = '';

   public function __construct($username, $password)
   {
      $this->username = $username;
      $this->password = $password;
   }

   public function send($to, $from, $body, $subject = '')
   {
      if ('' == $subject) {
         return $this->sendSMS($to, $from, $body);
      } else {
         return $this->sendEmail($to, from, $body, $subject);
      }
   }

   protected function sendSMS($to, $from, $body)
   {
      echo 'Sending SMS Implementation ...';
   }

   protected function sendEmail($to, $from, $body, $subject)
   {
      echo 'Sending Email Implementation ...';
   }
}

$notificationAdapter = new NotificationAdapter();

$notificationAdapter->send('test@test.com', 'me@test.com', 'hello!', 'test message');

Majd Arbash

Leave a Reply