Intermediary model--cooperation of various departments

Primer

Xiaoshuai works in a manufacturing company, and his production department is responsible for the production of products. Since their company's products are all customized, they are all produced according to the orders of the sales department. During the production process, they also need to check whether the raw materials in the warehouse are sufficient.

If the raw materials are not enough, the purchasing department should be notified to purchase. After purchasing, the purchasing department should notify the warehouse to put it into storage, and the warehouse will then notify the production department to pick up the materials for production.

After the final production is completed, the production department will also notify the warehouse to put the finished product into storage.

The entire production process involves the cooperation of multiple departments, and the various relationships are intricate. The relationship diagram between the departments is as follows:
insert image description here
Xiaoshuai found that there is strong coupling between the departments, and the objects of each department must refer to the objects of many other departments. Departmental classes are difficult to reuse. So Xiaoshuai suggested to the boss to set up a project management department to be responsible for communicating with various departments. Each department only needs to communicate with the project management department, so that the relationship between departments will be much clearer: the boss thinks Xiaoshuai’s proposal is very good
insert image description here
, Xiaoshuai's suggestion was adopted, and the project management department was established to be responsible for all aspects related to production. By the way, Xiaoshuai was promoted as the manager of the project management department, so that he could work hard.

Xiaoshuai was secretly delighted, this model is not my original, I just applied the mediator model.

mediator pattern

Mediator pattern: Use an intermediary object to encapsulate a series of object interactions. Mediators enable objects to be loosely coupled without explicitly referencing each other, and their interactions can be changed independently.

insert image description here

To put it simply, the intermediary mode is: all objects only know the intermediary, and only interact with the intermediary object.

  • Mediator: The mediator defines an interface to communicate with each colleague (Colleague) object.
  • ConcreteMediator (specific mediator): The specific mediator realizes collaborative behavior by coordinating each colleague object, and understands and maintains his colleagues.
  • Colleague (colleague class): Each colleague class knows its mediator object and only communicates with the mediator object.

Colleagues send and receive requests to the same mediator object. Mediator appropriately forwards requests among various colleagues to achieve collaborative behavior.

After Xiaoshuai applied the intermediary mode, he listed all the codes related to production:
department abstract class:

/**
 * 部门抽象类
 */
public abstract class Department {
    
    

    /**
     * 中介类
     */
    Mediator mediator;

    public Department(Mediator mediator) {
    
    
        this.mediator = mediator;
    }
}

marketing department:

/**
 * 市场部门
 */
public class MarketingDepartment extends Department{
    
    

    public MarketingDepartment(Mediator mediator) {
    
    
        super(mediator);
    }

    /**
     * 通知生产
     */
    public void notifyProduction() {
    
    
        super.mediator.notify(this, "开始生产");
    }
}

Production department:

/**
 * 生产部门
 */
public class ProductionDepartment extends Department{
    
    

    public ProductionDepartment(Mediator mediator) {
    
    
        super(mediator);
    }

    /**
     * 生产产品
     */
    public void production() {
    
    
        System.out.println("生产产品");
    }

    /**
     * 通知采购
     */
    public void notificationPurchase() {
    
    
        super.mediator.notify(this, "通知采购");
    }

    /**
     * 通知入库
     */
    public void notificationStorage() {
    
    
        super.mediator.notify(this, "通知入库");
    }

}

Purchasing department:

/**
 * 采购部门
 */
public class PurchasingDepartment extends Department{
    
    

    public PurchasingDepartment(Mediator mediator) {
    
    
        super(mediator);
    }

    /**
     * 采购原材料
     */
    public void purchaseRawMaterials() {
    
    
        System.out.println("采购原材料");
    }

    /**
     * 通知入库
     */
    public void notificationStorage() {
    
    
        super.mediator.notify(this, "通知入库");
    }
}

storehouse:

/**
 * 仓库
 */
public class Warehouse extends Department{
    
    

    public Warehouse(Mediator mediator) {
    
    
        super(mediator);
    }

    /**
     * 原材料入库
     */
    public void rawMaterialStorage() {
    
    
        System.out.println("原材料入库");
    }

    /**
     * 产成品入库
     */
    public void finishedProductStorage() {
    
    
        System.out.println("产成品入库");
    }

    /**
     * 通知提料
     */
    public void notificationExtractRowMaterials() {
    
    
        super.mediator.notify(this, "通知提料");
    }
}

Mediator interface:

/**
 * 中介者接口
 */
public interface Mediator {
    
    
    /**
     * 通知方法
     * @param department
     * @param event
     */
    void notify(Department department, String event);
}

Project Management Department:

/**
 * 项目管理部
 */
public class ProjectManagementMediator implements Mediator{
    
    

    private MarketingDepartment marketingDepartment;
    private ProductionDepartment productionDepartment;
    private PurchasingDepartment purchasingDepartment;
    private Warehouse warehouse;

    public ProjectManagementMediator() {
    
    
        this.marketingDepartment = new MarketingDepartment(this);
        this.productionDepartment = new ProductionDepartment(this);
        this.purchasingDepartment = new PurchasingDepartment(this);
        this.warehouse = new Warehouse(this);
    }

    @Override
    public void notify(Department department, String event) {
    
    
        // 市场部的通知
        if(department instanceof MarketingDepartment) {
    
    
            this.productionDepartment.production();
        }
        // 生产部的通知
        else if(department instanceof ProductionDepartment) {
    
    
            if("通知采购".equals(event)) {
    
    
                this.purchasingDepartment.purchaseRawMaterials();
            } else if("通知入库".equals(event)) {
    
    
                this.warehouse.finishedProductStorage();
            }
        }
        // 采购部的通知
        else if(department instanceof PurchasingDepartment) {
    
    
            if("通知入库".equals(event)) {
    
    
                this.warehouse.rawMaterialStorage();
            }
        }
        // 仓库的通知
        else if(department instanceof Warehouse) {
    
    
            if("通知提料".equals(event)) {
    
    
                this.productionDepartment.production();
            }
        }
    }
}

client:

/**
 * 客户端
 */
public class Client {
    
    

    public static void main(String[] args) {
    
    
        // 项目管理部
        Mediator mediator = new ProjectManagementMediator();
        // 市场部
        MarketingDepartment marketingDepartment = new MarketingDepartment(mediator);
        // 通知生产
        marketingDepartment.notifyProduction();
        // 生产部
        ProductionDepartment productionDepartment = new ProductionDepartment(mediator);
        // 通知采购
        productionDepartment.notificationPurchase();
        // 采购部
        PurchasingDepartment purchasingDepartment = new PurchasingDepartment(mediator);
        // 通知入库
        purchasingDepartment.notificationStorage();
        // 仓库
        Warehouse warehouse = new Warehouse(mediator);
        // 通知提料
        warehouse.notificationExtractRowMaterials();
        // 生产部通知入库
        productionDepartment.notificationStorage();
    }
}

output:

生产产品
采购原材料
原材料入库
生产产品
产成品入库

In this way, all departments only need to submit their needs to the project management department, and let the project management department act as an intermediary to communicate with other departments for unified management and overall planning, which can greatly improve production efficiency.

Summarize

Difference Between Mediator and Observer Patterns

The main goal of a mediator is to remove interdependencies between a series of objects that would depend on the same mediator object.

The goal of the observer is to establish a dynamic one-way connection between objects. The interaction relationship between them is often one-way. A participant is either an observer or an observed person, and will not have both identities .

The most important thing is that their intentions are different. The intermediary is mainly to eliminate the dependencies between objects; and the observer mode is a subscription mechanism, which is mainly to send notifications to subscribed objects.

We have to distinguish different design patterns, it is best to start from their intentions, different design patterns may be very similar, but the problems they have to solve are different.

Finally, let's take a look at the advantages and disadvantages of the mediator model:

advantage

  • The generation of subclasses is reduced, and Mediator organizes the behavior of Colleague objects together. If you want to change the behavior mode of Colleague objects, you only need to add a subclass of Mediator, and each Colleague class can be reused.
  • Decoupling each Colleague object, all Colleague objects are not related, we can change and reuse each Mediator class and Colleague class independently.
  • The relationship between objects is simplified, and the original many-to-many relationship is changed into a one-to-many relationship between Mediator and Colleague objects.

shortcoming

  • The mediator pattern transforms the complexity of the interaction into the complexity of the mediator, and the mediator object may become more and more complex and difficult to maintain.

Guess you like

Origin blog.csdn.net/zhanyd/article/details/119673741