------Broker Pattern for Java 23 design Patterns

Source: Internet
Author: User

One Broker Mode (mediator)

The mediator pattern is also used to reduce the coupling between class classes, because if there is a dependency between class classes, it is not conducive to the expansion and maintenance of functions, because as long as you modify an object, the other associated objects will have to be modified. If you use the mediator mode, just care about the relationship with the Mediator class, the relationship between the specific class classes and scheduling to mediator on the line, which is a bit like the role of spring container. First look at the picture:

User Class Unified interface, User1 and User2 are different objects, there is a correlation between the two, if you do not use the intermediary mode, you need to hold a reference to each other, so that the coupling is very high, for the understanding of decoupling, introduced the Mediator class, provides a unified interface, Mymediator for its implementation class, which holds User1 and User2 instances, used to achieve User1 and User2 control. So User1 and User2 two objects are independent of each other, they just need to keep good and mediator relationship between the line, the rest of all by the Mymediator class to maintain! Basic implementation:

    1. Public interface Mediator {
    2. public void Createmediator ();
    3. public void Workall ();
    4. }
 
  1. Public class Mymediator implements mediator {
  2. Private User user1;
  3. Private User User2;
  4. Public User GetUser1 () {
  5. return user1;
  6. }
  7. Public User GetUser2 () {
  8. return user2;
  9. }
  10. @Override
  11. public void Createmediator () {
  12. User1 = new User1 (this);
  13. User2 = new User2 (this);
  14. }
  15. @Override
  16. public void Workall () {
  17. User1.work ();
  18. User2.work ();
  19. }
  20. }
 
    1. Public abstract class User {
    2. Private mediator mediator;
    3. Public Mediator Getmediator () {
    4. return mediator;
    5. }
    6. Public User (mediator mediator) {
    7. This.mediator = Mediator;
    8. }
    9. public abstract void work ();
    10. }
 
    1. public class User1 extends User {
    2. Public User1 (mediator mediator) {
    3. Super (mediator);
    4. }
    5. @Override
    6. public void work () {
    7. System.out.println ("User1 exe!");
    8. }
    9. }
 
    1. public class User2 extends User {
    2. Public User2 (mediator mediator) {
    3. Super (mediator);
    4. }
    5. @Override
    6. public void work () {
    7. System.out.println ("User2 exe!");
    8. }
    9. }

Test class:

    1. public class Test {
    2. public static void Main (string[] args) {
    3. Mediator Mediator = new Mymediator ();
    4. Mediator.createmediator ();
    5. Mediator.workall ();
    6. }
    7. }

Output:

User1 exe!
User2 exe!

Reprinted from Https://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html

------Broker Pattern for Java 23 design Patterns

Related Article

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.