Recently, I have been entangled in the hierarchical architecture of projects. The most tangled issue is how to introduce a third-party class library in the project. For example, a msg controller needs to send an email {code ...} this controller implements the email sending function by introducing an email tool class, but if you want to send emails at the same time... recently, we have been entangled in the hierarchical architecture of projects. The most tangled issue is how to introduce third-party class libraries in the project.
For example, a msg controller needs to send an email.
class MsgConstroller extends Constroller { public function send() { $email = new email([options....]); $email->send(); }}
This controller implements the email sending function by introducing an email tool class. But what if you want to send text messages at the same time?
class MsgConstroller extends Constroller { public function send() { $email = new email([options....]); $sms = new sms([options....]); $email->send(); $sms->send(); } }
This is not elegant, so I thought of using a service layer to solve it.
Class MsgConstroller extends Constroller {public function send () {$ msg = service ('msg '); // instantiate a Msg service class $ Msg-> send ('message, email, sms '); // parameters passed through the send interface will send both insite emails, emails, and text messages }}
However, this will transfer the new operation to the service layer, and it doesn't seem to be ideal. So what simple and elegant solutions do you have?
Reply content:
Recently, we have been entangled in the hierarchical architecture of projects. The most tangled issue is how to introduce third-party class libraries in the project.
For example, a msg controller needs to send an email.
class MsgConstroller extends Constroller { public function send() { $email = new email([options....]); $email->send(); }}
This controller implements the email sending function by introducing an email tool class. But what if you want to send text messages at the same time?
class MsgConstroller extends Constroller { public function send() { $email = new email([options....]); $sms = new sms([options....]); $email->send(); $sms->send(); } }
This is not elegant, so I thought of using a service layer to solve it.
Class MsgConstroller extends Constroller {public function send () {$ msg = service ('msg '); // instantiate a Msg service class $ Msg-> send ('message, email, sms '); // parameters passed through the send interface will send both insite emails, emails, and text messages }}
However, this will transfer the new operation to the service layer, and it doesn't seem to be ideal. So what simple and elegant solutions do you have?
Your practice is acceptable. Optimization can enclose a message class with dependency injection. All types of email messages are converted into private variables. The input parameters in the operation method control whether to instantiate and assign values to private variables, the send method supports concatenation, which is similar
$message = new Message();$result = $message->send(array('mail', 'mail_address'))->send(array('sms', 'tel'));
There is no way to switch between the design model and maintain it when you want to upgrade it. The encapsulation granularity is too coarse and easy to use, and too fine and easy to use.
Another one, nothing is related to elegance, just two lines of programs which are so elegant.
How can I combine the send () method interface with the Composite design mode by using sms and email?
class MsgConstroller extends Constroller { public function send() { $msg = new MsgComposite(); $msg->add(new email()); $msg->add(new sms()); $msg->send(); }}
Now, the new operating framework seems to like to use the unified ServiceLocator solution, which facilitates the unified replacement and facilitates unit testing.
This requirement is more suitable for services. You can use the Service in Symfony2. You only need to configure the Service to call it anywhere in the world:
$mailer = $this->get('mailer');
Likewise:
$sms = $this->get('sms');
Refer:
Http://symfony.com/doc/current/book/service_container.html