19 of 23 Design Patterns in Java-Mediator mode (mediator pattern)

Source: Internet
Author: User

Abstract class Abstractcolleague
{
public abstract void workself ();
public abstract void Requestothershelp ();
}

Class Colleaguea extends Abstractcolleague
{
Mediator amediator=new Mediator ();
public void Workself ()
{
System.out.println ("Colleaguea-->work");
}
public void Requestothershelp ()
{
System.out.println ("Colleaguea-->request colleagueb help");
Amediator.dorequest (this);
}
}

Class Colleagueb extends Abstractcolleague
{
Mediator amediator=new Mediator ();
public void Workself ()
{
System.out.println ("Colleagueb-->work");
}
public void Requestothershelp ()
{
System.out.println ("Colleagueb-->request Colleaguea help");
Amediator.dorequest (this);
}
}

Class Mediator
{
Abstractcolleague Aabstractcolleague=null;
public void DoRequest (Abstractcolleague abstractcolleague)
{
if (Abstractcolleague.getclass (). Equals (Colleaguea.class))
{
Aabstractcolleague=new Colleagueb ();
Aabstractcolleague.workself ();
}
Else
{
Aabstractcolleague=new Colleaguea ();
Aabstractcolleague.workself ();
}
}

}

public class Mediatorpatterntest
{
public static void Main (string[] args)
{
Abstractcolleague acolleaguea=new Colleaguea ();
Abstractcolleague acolleagueb=new Colleagueb ();
Mediator amediator-new Mediator ();

Acolleaguea.workself ();
Acolleaguea.requestothershelp ();
System.out.println ("Work done");

Acolleagueb.workself ();
Acolleagueb.requestothershelp ();
System.out.println ("Work done");
}

}

19 of 23 Design Patterns in Java-Mediator mode (mediator pattern)

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.