Westward Java design patterns learning summary (xxv) --- intermediary model

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/a770794164/article/details/90731094

Intermediary model (Mediator), with an intermediary object that encapsulates set of objects interact. Mediator keeping objects refer to each other to display, so that it loosely coupled, and can be changed independently of the interaction between them.
Westward from design patterns
Example:
When a company needs to do a Web site, but he did not have time to do, you need to outsource, outsourcing usually need to find a company to do, and need to find a Management Company, responsible for the progress made and to see the effect, outsourcing firms and Management Company communication between, need to be carried out by the company. This scenario can be used to achieve the intermediary mode.

  1. Intermediary abstract class
abstract class AbstractCompany {
    AbstractMediator abstractMediator;
    public AbstractCompany(AbstractMediator abstractMediator) {
        this.abstractMediator = abstractMediator;
    }
    abstract void send(String content);
    abstract void get(String msg);
}
  1. Agency implementation class
class ConcreteMediator extends AbstractMediator {
    
    OutsourcedCompany outsourcedCompany;
    SupervisionCompany supervisionCompany;

    @Override
    void send(String msg, AbstractCompany company) {
        if (company instanceof OutsourcedCompany) {
            System.out.println("中介收到来自外包公司的消息: " + msg + ",准备发送给监理公司");
            if (supervisionCompany != null) {
                supervisionCompany.get(msg);
            }
        } else if (company instanceof SupervisionCompany) {
            System.out.println("中介收到来自监理公司的消息: " + msg + ",准备发送给外包公司");
            if (outsourcedCompany != null) {
                outsourcedCompany.get(msg);
            }
        }
    }

    public void setOutsourcedCompany(OutsourcedCompany outsourcedCompany) {
        this.outsourcedCompany = outsourcedCompany;
    }

    public void setSupervisionCompany(SupervisionCompany supervisionCompany) {
        this.supervisionCompany = supervisionCompany;
    }

}
  1. Colleagues abstract class
abstract class AbstractCompany {
    AbstractMediator abstractMediator;
    public AbstractCompany(AbstractMediator abstractMediator) {
        this.abstractMediator = abstractMediator;
    }
    abstract void send(String content);
    abstract void get(String msg);
}
  1. Colleagues abstract class that implements the class
class OutsourcedCompany extends AbstractCompany {
    public OutsourcedCompany(AbstractMediator abstractMediator) {
        super(abstractMediator);
    }
    @Override
    void send(String content) {
       abstractMediator.send(content, this);
    }
    @Override
    void get(String msg) {
        System.out.println("外包公司收到消息:" + msg);
    }
}

class SupervisionCompany extends AbstractCompany {
    public SupervisionCompany(AbstractMediator abstractMediator) {
        super(abstractMediator);
    }
    @Override
    void send(String content) {
       abstractMediator.send(content, this);
    }
    @Override
    void get(String msg) {
        System.out.println("监理公司收到消息:" + msg);
    }
}
  1. The main program
class Test {
    public static void main(String[] args) {
        ConcreteMediator myCompany = new ConcreteMediator();
        OutsourcedCompany companyA = new OutsourcedCompany(myCompany);
        SupervisionCompany companyB = new SupervisionCompany(myCompany);
        
        myCompany.setOutsourcedCompany(companyA);
        myCompany.setSupervisionCompany(companyB);
        
        companyA.send("软件做完了,交给监理费检查吧!");
        System.out.println("======");
        companyB.send("检查完了,文档不够完善!");
    }
}
结果:
中介收到来自外包公司的消息: 软件做完了,交给监理费检查吧!,准备发送给监理公司
监理公司收到消息:软件做完了,交给监理费检查吧!
======
中介收到来自监理公司的消息: 检查完了,文档不够完善!,准备发送给外包公司
外包公司收到消息:检查完了,文档不够完善!

to sum up:

Mediator each coupling reduces the emergence of Colleague, and makes it possible to independently vary each reuse Colleague and Mediator classes, since the abstract how collaboration objects, the intermediary as an independent concept and encapsulated in an object, so that the object of attention shifts from their own behavior object to the interaction between them up, we are standing in a broader perspective on the system. However, due to the centralized control ConcreteMediator, then put the complexity of the interaction becomes the complexity of the Mediator, which makes any Mediator becomes larger than a class ConcreteColleague complex. So intermediary model generally applied to a set of well-defined but complex ways of communication occasion, and want to customize the behavior of a distributed across multiple classes, but do not want to generate too many sub-categories of occasions.

Guess you like

Origin blog.csdn.net/a770794164/article/details/90731094