Design Patterns-How do you gracefully invoke the tool class library in a PHP project?

Source: Internet
Author: User
I've been wrestling with the project's layered architecture. The most tangled issue is how to introduce a third-party class library in a project

For example, there's a MSG controller to send mail

class MsgConstroller extends Constroller {    public function send() {       $email = new email([options....]);       $email->send();    }}

This controller implements the ability to send email by introducing an email tool, but if you want to send a text message at the same time.

class MsgConstroller extends Constroller {        public function send() {           $email = new email([options....]);           $sms = new sms([options....]);           $email->send();           $sms->send();        }    }

It's not so elegant, so I thought about using a service layer to solve

class MsgConstroller extends Constroller {        public function send() {          $msg = service('Msg');  //实例化一个Msg服务类           $msg->send('message,email,sms'); //通过send接口传的参数 会同时发送站内信、邮件、短信        }    }

But this will also transfer the new operation to the service layer feeling is not how ideal so what are the simpler and more elegant solutions?

Reply content:

I've been wrestling with the project's layered architecture. The most tangled issue is how to introduce a third-party class library in a project

For example, there's a MSG controller to send mail

class MsgConstroller extends Constroller {    public function send() {       $email = new email([options....]);       $email->send();    }}

This controller implements the ability to send email by introducing an email tool, but if you want to send a text message at the same time.

class MsgConstroller extends Constroller {        public function send() {           $email = new email([options....]);           $sms = new sms([options....]);           $email->send();           $sms->send();        }    }

It's not so elegant, so I thought about using a service layer to solve

class MsgConstroller extends Constroller {        public function send() {          $msg = service('Msg');  //实例化一个Msg服务类           $msg->send('message,email,sms'); //通过send接口传的参数 会同时发送站内信、邮件、短信        }    }

But this will also transfer the new operation to the service layer feeling is not how ideal so what are the simpler and more elegant solutions?

You've done your thing. Re-optimization can be wrapped in a message class with dependency injection, e-mail message all kinds of classes into private variables, the operation method of the parameter control is instantiated and assigned to a private variable, the Send method supports concatenating, used similar to

$message = new Message();$result = $message->send(array('mail', 'mail_address'))->send(array('sms', 'tel'));

Re-cut design mode is also original aim, and want to use the cool and want to upgrade the changes when good maintenance is no way out. Package granularity is too coarse and easy to change, too fine to be difficult to use.

One more thing, don't have anything to do with elegance, just two lines of programming, which is so much elegance.

How does SMS and e-mail implement the Send () method interface, and then use the composite design pattern to combine them?

class MsgConstroller extends Constroller {    public function send() {      $msg = new MsgComposite();      $msg->add(new email());      $msg->add(new sms());      $msg->send();    }}

New operation now the framework seems to like to use a unified servicelocator solution, convenient for unified replacement for unit testing

Similar to this requirement, the service can be used in Symfony2, and only the service can be configured to be called anywhere in the world:

$mailer = $this->get('mailer');

Similarly:

$sms = $this->get('sms');

Reference:

Http://symfony.com/doc/current/book/service_container.html

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.