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 )...