Mediator pattern


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Mediator pattern

  1. 1. Class report CLASS ASSIGNMENT- 3A CHATTING APPLICATION (MEDIATOR PATTERN) Submitted by: Md.Mahedi Mahfuj -BIT 0207 Submitted to: Maeenul Islam Lecturer,IIT Date of submission: 29th January,2012Institute of Information Technology Page 1 of 15
  2. 2. Class report University of Dhaka A CHATTING APPLICATION:Introduction:At first, we have to see what happens in this sort of chatting software, i.e. whatfeatures we have to focus on.Now, let’s see the criteria that we have to maintain in our program. One usershould be able to send message & receive message simultaneously in this sortof chatting software, i.e. a two-way communication. Earlier, in the observerpattern, we were concerned about one-way communication. So, let’s seewhat we usually do in such cases.There must be a User class. Let’s, see what may be there…..User:Class User {Name:Description: Page 2 of 15
  3. 3. Class reportID:List <user> Friend list;Methods like;AddFriend (user U) {Friend list. Add (U);}SendMessage (String msg ,User U){Print (msg to;} -may be present.The main method should be as follows in such case:Main () {User U1 = new user (“U1”);User U2 = new user (“U2”);User U3 = new user (“U3”);U1.AddFriend (U2);U2.AddFriend (U3);U2.AddFriend (U1);U3.AddFriend (U2); Page 3 of 15
  4. 4. Class report……..……………….U1.SendMessage (“Hi”, U2);U2.SendMessage (“Hello”, U3);………………………}Now, let’s see what are the problems that we may face if we solve in this way.Problems:Firstly, If we print the following message inside the SendMessage() methodthen it will only be seen on my screen, not on my friend’s which is actually notour goal. So, the message should have not been there at all.SendMessage (String msg ,User U){Print (msg to;} Page 4 of 15
  5. 5. Class reportSo, the solution should be like this until now.Solution:SendMessage (msg.User U) {U.ReceiveMessage (msg,this);}ReceiveMessage (msg,User U) {Print (msg to;}Now, another question may come in mind while giving this solution that whywe have used ‘this’? This is because we have to make him understand fromwhom the message is coming as he is getting many more messages at thesame time from some other distinguished users.Secondly; What is the profit we have gained only by changing the position ofprinting option inside the same user?Solution:To be noted, here both U1 & U2 are instances of same class, i.e. User class. So,the SendMessage option that we are using is for U1 & ReceiveMessage for U2.And the message should be in the ReceiveMessage method in logical way. Page 5 of 15
  6. 6. Class reportThirdly; Once we added U2 as U1’s friend, which means U1 is also U2’sfriend. But, here we have done the later part again which is completelyredundant.Main () {User U1 = new user (“U1”);User U2 = new user (“U2”);User U3 = new user (“U3”);U1.AddFriend (U2);U2.AddFriend (U3);U2.AddFriend (U1);U3.AddFriend (U2);……..………}Solution:AddFriend (User U) {Friend list. Add (U);U.AddFriend (this);} Page 6 of 15
  7. 7. Class reportBut, point to be noted, this has become a recursive loop, i.e. a recursivemethod. So, this can’t be the exact solution.The exact solution for this certain problem be,AddFriend (User U) {………………Friend list. Add (U);U. Friend list. Add (this);}Hence, there is no recursive loop now.Fourthly, If any user sends message to another user, the later one may beoffline/inactive, what will happen to the sent message?Solution:SendMessage (msg, U){///checking……….If (U is not null)U.ReceiveMessage (msg , this);} Page 7 of 15
  8. 8. Class reportSo, in this way we can ensure the receipt of the very message accordingly.Fifthly, The question comes; where will I get the user list? How will I know “A”is a user & I can make him my friend?Solution:For solving the very problem, we have to make an intermediate step inbetween the communication system that we have supported earlier.That is, we have to make a chat server. User U1 User... User U2 Chat Server User User U5 U3 User U4 Page 8 of 15
  9. 9. Class reportFor instance;Chat Server Class:Chat Server {User list; //global user list. When anyone signs up, added here.Hashmap Friend list ;SendMsg (msg, sender ID, receiver ID) {Receiver= get User (receiver ID);Receiver. ReceiveMsg (msg , sender ID);}GetUser (User ID){…………….…………….}}User Class:User {Name;Chatserver server; Page 9 of 15
  10. 10. Class reportFriend list;User (name, cs) {………………Server=cs;}SendMsg (msg, receiver ID){Server.SendMsg (msg, this.Id, receiver ID);}ReceiveMsg (msg, sender ID) {Print (“……….”);}Main() {Chatserver cs = new Chat server ();User U1 = new User (“User ID”, cs);U1.SendMsg (msg, “user ID”);………..………..………...} Page 10 of 15
  11. 11. Class reportSo, whatever we got is; Message History, User Status (offline/online) and Location ofmessage; all this information is found in server class.To be noted; This solution can easily work in multiple machines, which theprevious solution can’t. The previous solution can work in more thanone window, but confined in one machine. Hence, that was inextensible.Now, Answer to some possible questions is given below:  How is it a two-way communication system? Point to be noted, User has both SendMessage() & ReceiveMessage() method.So, one can send & receive message at the same time. Hence, it is a two-waycommunication system.  How do we search friends? Chat server =new Chat server (); User ma = new User (“ma”, cs); cs. SearchFriend(“Mahedi”); //return list that we are looking for. Friend ID = list.getItem (s); cs.AddFriend (“ma”, friend ID); Page 11 of 15
  12. 12. Class report ma.SendMsg (“Hello”, friend ID);Moreover, What we can see is that user class only has SendMsg() &ReceiveMSg() method. All other features that we have added & we are goingto add will be in the server class.Hence, chat server acts here as an intermediate media & mediates among theusers. That’s why; this pattern is named as “Mediator Pattern”.Though, Both Observer Pattern & Mediator Pattern is concerned withmessage passing, they have some distinct dissimilarities.  Observer pattern is for one-way communication & Mediator pattern for two-way communication.  Observer pattern has no intermediate layer whereas Mediator pattern has so, i.e. chat server.Thus,The main code to solve the problem is shown below:Main Code:Chat Server Class: Class ChatServer { User Receiver; Page 12 of 15
  13. 13. Class reportstring s, r,m; Hashtable hs; Hashtable hs1; public ChatServer() { hs = new Hashtable(); hs1 = new Hashtable(); } public void AddName(string m, User u) { hs1.Add(m,u); } public void SendMsg(string msg, string SenderID,string RecID) { s = SenderID; r = RecID; m = msg; Receiver = (User)hs1[r]; Receiver.RecMsg(m,s); } public void AddFriend(string UserID, string FriendID) { s = UserID; r = FriendID; hs.Add(s,r); } } Page 13 of 15
  14. 14. Class reportUser Class: Class User { ChatServer server; string name; public User(string name,ChatServer cs) { = name; server = cs; } public void SendMsg(string m , string RecID) { server.SendMsg(m,,RecID); } public void RecMsg(string s, string SenderID) { Console.WriteLine(" " + SenderID + " send " + s ); } }MainDemo Class: class MainDemo { static void Main(string[] args) Page 14 of 15
  15. 15. Class report { ChatServer cs = new ChatServer(); User mahedi = new User("mahedi",cs); cs.AddName("mahedi",mahedi); cs.AddFriend("Mahedi","Mamun"); monir.SendMsg("hi", "Mamun"); Console.ReadKey(); } } }…………………………………………………X………………………………………………………….. Page 15 of 15