1 Code Walkthrough
1.1 Code Walkthrough 1
1 Code Walkthrough
1.1 Code Walkthrough 1
demand:
Cattle course back qq discussion groups Li X after each person to send a message, others can see
Note :( personal development did not notice the place)
processing regarding a message (e.g., sensitive verification information, etc.), can be placed in the intermediary, user need not encapsulated.
b qq group chat rooms and this process usually mediator (such applications more), the relationship may be more complex.
The spread between c class by themselves, see the user class;
uml 类图:
Test categories:
package com.geely.design.pattern.behavioral.mediator; /** * Created by geely */ public class Test { public static void main(String[] args) { User geely = new User("Geely"); User tom= new User("Tom"); geely.sendMessage(" Hey! Tom! Let's learn Design Pattern"); tom.sendMessage("OK! Geely"); } }
qq group messaging classes:
package com.geely.design.pattern.behavioral.mediator; import java.util.Date; /** * Created by geely * / Public class studygroup {
/ **
* core method, the user will send a message, sent by the mediator to
* / public static void showMessage (the User User, String Message) { System.out.println(new Date().toString() + " [" + user.getName() + "] : " + message); } }
User categories:
package com.geely.design.pattern.behavioral.mediator; /** * Created by geely */ public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public User(String name) { this.name = name; }
/ **
* to pass over his own
* / public voidsendMessage (the Message String) { StudyGroup.showMessage(this, message); } }
Print log:
Tue Mar 03 05:51:45 CST 2020 [Geely] : Hey! Tom! Let's learn Design Pattern Tue Mar 03 05:51:45 CST 2020 [Tom] : OK! Geely Process finished with exit code 0