"Java Design pattern" Mediator Pattern __java design pattern

Source: Internet
Author: User

Step one: Create an intermediary mediator

Public interface Mediator {
	//create mediator public
    void Createmediator ();
    
    public void Workall ();
}

Step two: Establish a specific intermediary to implement a person

Public class Mymediator implements mediator {

	 private User user1;
	 Private User user2;
	 
	 Public User GetUser1 () {return
		user1;
	}
	 Public User GetUser2 () {return
		user2;
	}
	 
	
	@Override public
	void Createmediator () {
		user1=new User1 (this);
		User2=new User2 (this);
	}

	@Override public
	void Workall () {
     user1.work ();
     User2.work ();
	}

Step three: Create a colleague class interface User

Public abstract class User {
   private mediator mediator;
   
   Public Mediator Getmediator () {return
	mediator;
}
   Public User (mediator mediator) {
	   this.mediator=mediator;
   }
   public abstract void work ();
}

Step four: Build the concrete implementation class of the colleague class

public class User1 extends User {public

	User1 (mediator mediator) {
		super (mediator);
	}

	 public void work () {
      System.out.println ("User1 execute Work");		
	}

public class User2 extends User {public

	User2 (mediator mediator) {
		super (mediator);
	}

	 public void work () {
      System.out.println ("User2 execute Work");		
	}


Step Five: Test

public class Test {public
   static void Main (string[] args) {
	Mediator mediator=new mymediator ();
	Mediator.createmediator ();
	Mediator.workall ();
}


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.