当前位置:网站首页>Intermediary model -- collaboration among departments

Intermediary model -- collaboration among departments

2022-06-24 20:35:00 zhanyd

Introduction

Xiaoshuai works in a manufacturing enterprise , His production department is responsible for the production of products . Because their products are customized , They are all produced according to the orders of the sales department , In the process of production, it is also necessary to check whether the raw materials in the warehouse are sufficient .

If the raw materials are not enough, the purchasing department shall be informed to purchase , The purchasing department shall notify the warehouse to stock in after purchasing , The warehouse will inform the production department to pick up materials for production .

After final production , The production department shall also notify the warehouse to put the finished products into storage .

The whole production process involves the cooperation of multiple departments , The relationships are complex , The relationship between the departments is as follows :
 Insert picture description here
Xiaoshuai found strong coupling between departments , The objects of each department must reference the objects of many other departments , It is difficult to reuse various department classes . So Xiaoshuai suggested to his boss to set up a project management department , Responsible for communicating with all departments , Each department only needs to communicate with the project management department , In this way, the relationship between departments is much clearer :
 Insert picture description here
The boss thinks Xiaoshuai's proposal is very good , I adopted Xiao Shuai's suggestion , A project management department has been established to take charge of all links related to production , Promote Xiaoshuai as the manager of the project management department , Let him get away with it .

Xiaoshuai was secretly pleased , This model is not my original , I just applied the mediator pattern .

Intermediary model

Intermediary model : Encapsulate a series of object interactions with a mediation object . Mediators make objects reference to each other without any need to show , So that the coupling is loose , And they can change their interaction independently .

 Insert picture description here

Simply put, the mediator model is : All objects know only mediators , Only interact with mediator objects .

  • Mediator( Intermediary ): The mediator defines an interface with various colleagues (Colleague) Object communication .
  • ConcreteMediator( Specific intermediary ): The concrete intermediary realizes the cooperative behavior by coordinating the colleague objects , Understand and maintain his colleagues .
  • Colleague( Colleagues ): Every colleague class knows its mediator object , And only communicate with the mediator object .

Colleagues send and receive requests to the same mediator object . Mediators appropriately forward requests among colleagues to achieve collaborative behavior .

After Xiaoshuai applied the intermediary model , Lists all production related codes :
Department abstract class :

/** *  Department abstract class  */
public abstract class Department {
    

    /** *  Intermediate class  */
    Mediator mediator;

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

Marketing Department :

/** *  Marketing Department  */
public class MarketingDepartment extends Department{
    

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

    /** *  Inform production  */
    public void notifyProduction() {
    
        super.mediator.notify(this, " Started to produce ");
    }
}

Production department :

/** *  Production department  */
public class ProductionDepartment extends Department{
    

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

    /** *  Production of products  */
    public void production() {
    
        System.out.println(" Production of products ");
    }

    /** *  Notification procurement  */
    public void notificationPurchase() {
    
        super.mediator.notify(this, " Notification procurement ");
    }

    /** *  Notify warehousing  */
    public void notificationStorage() {
    
        super.mediator.notify(this, " Notify warehousing ");
    }

}

Purchasing Department :

/** *  Purchasing Department  */
public class PurchasingDepartment extends Department{
    

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

    /** *  Purchasing raw materials  */
    public void purchaseRawMaterials() {
    
        System.out.println(" Purchasing raw materials ");
    }

    /** *  Notify warehousing  */
    public void notificationStorage() {
    
        super.mediator.notify(this, " Notify warehousing ");
    }
}

Warehouse :

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

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

    /** *  Warehousing of raw materials  */
    public void rawMaterialStorage() {
    
        System.out.println(" Warehousing of raw materials ");
    }

    /** *  Finished goods warehousing  */
    public void finishedProductStorage() {
    
        System.out.println(" Finished goods warehousing ");
    }

    /** *  Notify to pick up materials  */
    public void notificationExtractRowMaterials() {
    
        super.mediator.notify(this, " Notify to pick up materials ");
    }
}

Intermediary interface :

/** *  Intermediary interface  */
public interface Mediator {
    
    /** *  Notification method  * @param department * @param event */
    void notify(Department department, String event);
}

Project management department :

/** *  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) {
    
        //  Notice from the marketing department 
        if(department instanceof MarketingDepartment) {
    
            this.productionDepartment.production();
        }
        //  Notice from the production department 
        else if(department instanceof ProductionDepartment) {
    
            if(" Notification procurement ".equals(event)) {
    
                this.purchasingDepartment.purchaseRawMaterials();
            } else if(" Notify warehousing ".equals(event)) {
    
                this.warehouse.finishedProductStorage();
            }
        }
        //  Notice from the purchasing department 
        else if(department instanceof PurchasingDepartment) {
    
            if(" Notify warehousing ".equals(event)) {
    
                this.warehouse.rawMaterialStorage();
            }
        }
        //  Warehouse notification 
        else if(department instanceof Warehouse) {
    
            if(" Notify to pick up materials ".equals(event)) {
    
                this.productionDepartment.production();
            }
        }
    }
}

client :

/** *  client  */
public class Client {
    

    public static void main(String[] args) {
    
        //  Project management department 
        Mediator mediator = new ProjectManagementMediator();
        //  The Marketing Department 
        MarketingDepartment marketingDepartment = new MarketingDepartment(mediator);
        //  Inform production 
        marketingDepartment.notifyProduction();
        //  Production department 
        ProductionDepartment productionDepartment = new ProductionDepartment(mediator);
        //  Notification procurement 
        productionDepartment.notificationPurchase();
        //  Purchasing Department 
        PurchasingDepartment purchasingDepartment = new PurchasingDepartment(mediator);
        //  Notify warehousing 
        purchasingDepartment.notificationStorage();
        //  Warehouse 
        Warehouse warehouse = new Warehouse(mediator);
        //  Notify to pick up materials 
        warehouse.notificationExtractRowMaterials();
        //  The production department shall notify the warehouse keeper 
        productionDepartment.notificationStorage();
    }
}

Output :

 Production of products 
 Purchasing raw materials 
 Warehousing of raw materials 
 Production of products 
 Finished goods warehousing 

such , All departments only need to submit their own needs to the project management department , Let the project management department act as an intermediary to communicate with other departments , Unified management , As a whole , Can greatly improve production efficiency .

summary

The difference between the mediator and observer patterns

The primary goal of a mediator is to eliminate the interdependencies between a set of objects , These objects will depend on the same mediator object .

The goal of the observer is to establish a dynamic one-way connection between objects , The interaction between them is often one-way , A participant is either an observer , Or the observer , Not both identities .

The most important thing is that their intentions are different , The main purpose of mediators is to eliminate dependencies between objects ; The observer model is a subscription mechanism , It is mainly used to send notifications to subscribers .

We need to distinguish between different design patterns , It's better to start from their intentions , Different design patterns may be very similar , But they have different problems to solve .

Last , Let's look at the pros and cons of the mediator model :

advantage

  • Reduce the generation of subclasses ,Mediator take Colleague The behavior of objects is organized together , If you want to change Colleague The behavior pattern of the object only needs to be added Mediator Just subclasses of , each Colleague Classes can be reused .
  • Will all Colleague Object decoupling , be-all Colleague There is no association between objects , We can change and reuse each... Independently Mediator Classes and Colleague class .
  • Simplifies the relationship between objects , Change the original many to many relationship into Mediator And Colleague One to many relationships between objects .

shortcoming

  • The mediator pattern transforms the complexity of interactions into the complexity of mediators , Mediator objects can become more and more complex , Difficult to maintain .
原网站

版权声明
本文为[zhanyd]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211327028533.html