当前位置:网站首页>Intermediary model -- collaboration among departments
Intermediary model -- collaboration among departments
2022-06-24 20:35:00 【zhanyd】
List of articles
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 :
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 :
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 .

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 .
边栏推荐
- The four stages of cloud computing development have finally been clarified
- OpenVINO2022 Dev Tools安装与使用
- Leetcode (455) - distribute cookies
- 用自身细胞作为原料,首例3D打印耳朵移植成功!未来可打印更复杂器官
- Vant component used in wechat applet
- Some ideas about chaos Engineering
- 苹果不差钱,但做内容“没底气”
- 托管服务与SASE,纵享网络与安全融合 | 一期一会回顾
- Difference between map and object
- 等保备案是等保测评吗?两者是什么关系?
猜你喜欢

宅男救不了元宇宙

Basic concepts and definitions of Graphs

Map跟object 的区别

Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable

The Network Security Review Office launched a network security review on HowNet, saying that it "has a large amount of important data and sensitive information"

Camera rental management system based on qt+mysql

16 excellent business process management tools

两位湖南老乡,联手干出一个百亿IPO

Bean lifecycle flowchart

Basic properties and ergodicity of binary tree
随机推荐
gateway
The AI for emotion recognition was "harbouring evil intentions", and Microsoft decided to block it!
Leetcode (146) - LRU cache
史上最全DPU厂商大盘点(上)
基于SSM的物料管理系统(源码+文档+数据库)
RF_ DC system clock setting gen1/gen2
Fuzzy background of unity (take you to appreciate the hazy beauty of women)
开放可编程基础设施(OPI)项目,重新定义DPU/IPU
苹果不差钱,但做内容“没底气”
Leetcode (455) - distribute cookies
Bean lifecycle flowchart
图的基本概念以及相关定义
lol手游之任务进度条精准计算
Openvino2022 dev tools installation and use
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Docker deploy mysql5.7
Q1: error in JMeter filename must not be null or empty
Basic concepts and definitions of Graphs
CVPR 2022缅怀孙剑!同济、阿里获最佳学生论文奖,何恺明入围
The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars