SlideShare uma empresa Scribd logo
1 de 14
Class report




                   CLASS ASSIGNMENT- 3
                    MEDIATOR PATTERN




          A CHATTING APPLICATION:



Introduction:
At first, we have to see what happens in this sort of chatting software, i.e. what
features we have to focus on.

Now, let’s see the criteria that we have to maintain in our program. One user
should be able to send message & receive message simultaneously in this sort
of chatting software, i.e. a two-way communication. Earlier, in the observer
pattern, we were concerned about one-way communication. So, let’s see
what we usually do in such cases.



There must be a User class. Let’s, see what may be there…..




                                                                        Page 1 of 14
Class report

User:


Class User {

Name:

Description:

ID:

List <user> Friend list;

Methods like;

AddFriend (user U) {

Friend list. Add (U);

}

SendMessage (String msg ,User U)

{

Print (msg to U.name);

}

                           -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”);

                                                     Page 2 of 14
Class report



U1.AddFriend (U2);

U2.AddFriend (U3);

U2.AddFriend (U1);

U3.AddFriend (U2);

……..

………

……….

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() method
then it will only be seen on my screen, not on my friend’s which is actually not
our goal. So, the message should have not been there at all.



                                                                      Page 3 of 14
Class report

SendMessage (String msg ,User U)

{

Print (msg to U.name);

}




So, the solution should be like this until now.

Solution:

SendMessage (msg.User U) {

U.ReceiveMessage (msg,this);

}

ReceiveMessage (msg,User U) {



Print (msg to U.name);

}

Now, another question may come in mind while giving this solution that why
we have used ‘this’? This is because we have to make him understand from
whom the message is coming as he is getting many more messages at the
same time from some other distinguished users.




Secondly;

          What is the profit we have gained only by changing the position of
printing option inside the same user?

                                                                  Page 4 of 14
Class report

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.




Thirdly;

         Once we added U2 as U1’s friend, which means U1 is also U2’s
friend. But, here we have done the later part again which is completely
redundant.

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:

                                                                      Page 5 of 14
Class report

AddFriend (User U) {

Friend list. Add (U);

U.AddFriend (this);

}



But, point to be noted, this has become a recursive loop, i.e. a recursive
method. 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 be
offline/inactive, what will happen to the sent message?




                                                                 Page 6 of 14
Class report

Solution:

SendMessage (msg, U)

{

///checking……….

If (U is not null)

U.ReceiveMessage (msg , this);

}

So, 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 in
between the communication system that we have supported earlier.



That is, we have to make a chat server.




                                                                     Page 7 of 14
Class report


                                    User
                                     U1
                    User...                     User
                                                 U2
                                   Chat
                                  Server
                     User                       User
                      U5                         U3
                                    User
                                     U4




For 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);

}

                                                                        Page 8 of 14
Class report



GetUser (User ID){

…………….

…………….

}

}



User Class:

User {

Name;

Chatserver server;

Friend list;

User (name, cs) {

………

………

Server=cs;

}



SendMsg (msg, receiver ID){

Server.SendMsg (msg, this.Id, receiver ID);

}

ReceiveMsg (msg, sender ID) {


                                               Page 9 of 14
Class report



Print (“……….”);

}



Main() {

Chatserver cs = new Chat server ();

User U1 = new User (“User ID”, cs);

U1.SendMsg (msg, “user ID”);

………..

………..

………...}



So, whatever we got is;

     Message History, User Status (offline/online) and Location of
message; all this information is found in server class.



To be noted;

       This solution can easily work in multiple machines, which the
previous solution can’t. The previous solution can work in more than
one window, but confined in one machine. Hence, that was inextensible.

Now,

       Answer to some possible questions is given below:




                                                            Page 10 of 14
Class report

   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-way
communication 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);

    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 going
to add will be in the server class.

Hence, chat server acts here as an intermediate media & mediates among the
users. That’s why; this pattern is named as “Mediator Pattern”.



Though,

       Both Observer Pattern & Mediator Pattern is concerned with
message passing, they have some distinct dissimilarities.


                                                                   Page 11 of 14
Class report

   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;
  string 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) {


                                                                 Page 12 of 14
Class report

       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);
   }
   }




User Class:

 Class User
 {
  ChatServer server;

  string name;


  public User(string name,ChatServer cs)
  {
     this.name = name;
     server = cs;
   }




                                                             Page 13 of 14
Class report

   public void SendMsg(string m , string RecID) {

   server.SendMsg(m,this.name,RecID);
    }

   public void RecMsg(string s, string SenderID) {

   Console.WriteLine(" " + SenderID + " send " + s );
   }
   }



MainDemo Class:


 class MainDemo
 {

  static void Main(string[] args)
  {
  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 14 of 14

Mais conteúdo relacionado

Destaque

Destaque (12)

Bengali optical character recognition system
Bengali optical character recognition systemBengali optical character recognition system
Bengali optical character recognition system
 
R with excel
R with excelR with excel
R with excel
 
Apache hadoop & map reduce
Apache hadoop & map reduceApache hadoop & map reduce
Apache hadoop & map reduce
 
Map reduce
Map reduceMap reduce
Map reduce
 
Nmdl final pp 1
Nmdl final pp 1Nmdl final pp 1
Nmdl final pp 1
 
Job search_resume
Job search_resumeJob search_resume
Job search_resume
 
Interviews 1
Interviews 1Interviews 1
Interviews 1
 
Big data
Big dataBig data
Big data
 
New microsoft office word 97 2003 document
New microsoft office word 97   2003 documentNew microsoft office word 97   2003 document
New microsoft office word 97 2003 document
 
Icons presentation
Icons presentationIcons presentation
Icons presentation
 
Strategy pattern.pdf
Strategy pattern.pdfStrategy pattern.pdf
Strategy pattern.pdf
 
Understanding Your Credit Report
Understanding Your Credit ReportUnderstanding Your Credit Report
Understanding Your Credit Report
 

Mais de Md. Mahedi Mahfuj

Mais de Md. Mahedi Mahfuj (18)

Parallel computing(1)
Parallel computing(1)Parallel computing(1)
Parallel computing(1)
 
Message passing interface
Message passing interfaceMessage passing interface
Message passing interface
 
Advanced computer architecture
Advanced computer architectureAdvanced computer architecture
Advanced computer architecture
 
Matrix multiplication graph
Matrix multiplication graphMatrix multiplication graph
Matrix multiplication graph
 
Strategy pattern
Strategy patternStrategy pattern
Strategy pattern
 
Database management system chapter16
Database management system chapter16Database management system chapter16
Database management system chapter16
 
Database management system chapter15
Database management system chapter15Database management system chapter15
Database management system chapter15
 
Database management system chapter12
Database management system chapter12Database management system chapter12
Database management system chapter12
 
Strategies in job search process
Strategies in job search processStrategies in job search process
Strategies in job search process
 
Report writing(short)
Report writing(short)Report writing(short)
Report writing(short)
 
Report writing(long)
Report writing(long)Report writing(long)
Report writing(long)
 
Job search_interview
Job search_interviewJob search_interview
Job search_interview
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language
 
R language
R languageR language
R language
 
Chatbot Artificial Intelligence
Chatbot Artificial IntelligenceChatbot Artificial Intelligence
Chatbot Artificial Intelligence
 
Cloud testing v1
Cloud testing v1Cloud testing v1
Cloud testing v1
 
Distributed deadlock
Distributed deadlockDistributed deadlock
Distributed deadlock
 
Paper review
Paper review Paper review
Paper review
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 

Último (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Mediator pattern

  • 1. Class report CLASS ASSIGNMENT- 3 MEDIATOR PATTERN A CHATTING APPLICATION: Introduction: At first, we have to see what happens in this sort of chatting software, i.e. what features we have to focus on. Now, let’s see the criteria that we have to maintain in our program. One user should be able to send message & receive message simultaneously in this sort of chatting software, i.e. a two-way communication. Earlier, in the observer pattern, we were concerned about one-way communication. So, let’s see what we usually do in such cases. There must be a User class. Let’s, see what may be there….. Page 1 of 14
  • 2. Class report User: Class User { Name: Description: ID: List <user> Friend list; Methods like; AddFriend (user U) { Friend list. Add (U); } SendMessage (String msg ,User U) { Print (msg to U.name); } -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”); Page 2 of 14
  • 3. Class report U1.AddFriend (U2); U2.AddFriend (U3); U2.AddFriend (U1); U3.AddFriend (U2); …….. ……… ………. 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() method then it will only be seen on my screen, not on my friend’s which is actually not our goal. So, the message should have not been there at all. Page 3 of 14
  • 4. Class report SendMessage (String msg ,User U) { Print (msg to U.name); } So, the solution should be like this until now. Solution: SendMessage (msg.User U) { U.ReceiveMessage (msg,this); } ReceiveMessage (msg,User U) { Print (msg to U.name); } Now, another question may come in mind while giving this solution that why we have used ‘this’? This is because we have to make him understand from whom the message is coming as he is getting many more messages at the same time from some other distinguished users. Secondly; What is the profit we have gained only by changing the position of printing option inside the same user? Page 4 of 14
  • 5. Class report 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. Thirdly; Once we added U2 as U1’s friend, which means U1 is also U2’s friend. But, here we have done the later part again which is completely redundant. 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: Page 5 of 14
  • 6. Class report AddFriend (User U) { Friend list. Add (U); U.AddFriend (this); } But, point to be noted, this has become a recursive loop, i.e. a recursive method. 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 be offline/inactive, what will happen to the sent message? Page 6 of 14
  • 7. Class report Solution: SendMessage (msg, U) { ///checking………. If (U is not null) U.ReceiveMessage (msg , this); } So, 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 in between the communication system that we have supported earlier. That is, we have to make a chat server. Page 7 of 14
  • 8. Class report User U1 User... User U2 Chat Server User User U5 U3 User U4 For 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); } Page 8 of 14
  • 9. Class report GetUser (User ID){ ……………. ……………. } } User Class: User { Name; Chatserver server; Friend list; User (name, cs) { ……… ……… Server=cs; } SendMsg (msg, receiver ID){ Server.SendMsg (msg, this.Id, receiver ID); } ReceiveMsg (msg, sender ID) { Page 9 of 14
  • 10. Class report Print (“……….”); } Main() { Chatserver cs = new Chat server (); User U1 = new User (“User ID”, cs); U1.SendMsg (msg, “user ID”); ……….. ……….. ………...} So, whatever we got is; Message History, User Status (offline/online) and Location of message; all this information is found in server class. To be noted; This solution can easily work in multiple machines, which the previous solution can’t. The previous solution can work in more than one window, but confined in one machine. Hence, that was inextensible. Now, Answer to some possible questions is given below: Page 10 of 14
  • 11. Class report  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-way communication 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); 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 going to add will be in the server class. Hence, chat server acts here as an intermediate media & mediates among the users. That’s why; this pattern is named as “Mediator Pattern”. Though, Both Observer Pattern & Mediator Pattern is concerned with message passing, they have some distinct dissimilarities. Page 11 of 14
  • 12. Class report  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; string 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) { Page 12 of 14
  • 13. Class report 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); } } User Class: Class User { ChatServer server; string name; public User(string name,ChatServer cs) { this.name = name; server = cs; } Page 13 of 14
  • 14. Class report public void SendMsg(string m , string RecID) { server.SendMsg(m,this.name,RecID); } public void RecMsg(string s, string SenderID) { Console.WriteLine(" " + SenderID + " send " + s ); } } MainDemo Class: class MainDemo { static void Main(string[] args) { 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 14 of 14