Design Pattern in php -- mediation pattern _ PHP Tutorial

Source: Internet
Author: User
Design Pattern in php-mediation pattern. Design Pattern in php-intermediary pattern Friend 1 QQ friend 2 friend 13 * abstract intermediary, QQ chat interfaceChatMediator {intermediary role publicfunctionsendMessage ($ msg, $ user) design Pattern in php -- mediation pattern
 Friend 1 I <--> QQ <----> Friend 2 <----> friend 13 * // abstract intermediary, QQ chat interface ChatMediator {// public function sendMessage ($ msg, $ user); public function addQQUser ($ user) ;}// abstract User abstract class user {protected $ mediator; protected $ name; public function _ construct ($ med, $ name) {$ this-> mediator = $ med; $ this-> name = $ name;} public abstract function send ($ msg ); public abstract function receive ($ msg);} class QQch At implements ChatMediator {// user list private $ users; public function _ construct () {$ this-> users = null;} // add user public function addQQUser ($ user) {$ this-> users [] = $ user;} // send specific QQ information to send public function sendMessage ($ msg, $ user) via QQ) {foreach ($ this-> users as $ k => $ v) {// you cannot accept if ($ v! = $ User) {// call a friend's interface $ v-> receive ($ msg );}}}} // Specific Object Role class QQUser extends User {public function send ($ msg) {$ this-> mediator-> sendMessage ($ msg, $ this );} // accept public function receive ($ msg) {echo $ this-> name. 'receive '. $ msg.'
';}} // Client // the intermediary is QQ $ QQchat = new QQchat (); $ oMe = new QQUser ($ QQchat, "James "); $ oFriend1 = new QQUser ($ QQchat, ""); $ oFriend2 = new QQUser ($ QQchat, "Wang Wei"); $ oFriend3 = new QQUser ($ QQchat, "eldest brother"); // add friends $ QQchat-> addQQUser ($ oMe); $ QQchat-> addQQUser ($ oFriend1); $ QQchat-> addQQUser ($ oFriend2 ); $ QQchat-> addQQUser ($ oFriend3); $ oMe-> send ("Hi All ");

Friend 1 my QQ friend 2 friend 13 * // abstract intermediary, QQ chat interface ChatMediator {// intermediary role public function sendMessage ($ msg, $ user )...

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.