当前位置:网站首页>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 .
边栏推荐
- 网络安全审查办公室对知网启动网络安全审查,称其“掌握大量重要数据及敏感信息”
- 天天鉴宝暴雷背后:拖欠数千万、APP停摆,创始人预谋跑路?
- Leetcode (455) - distribute cookies
- 16个优秀业务流程管理工具
- 红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定
- Nodered has no return value after successfully inserting into the database (the request cannot be ended)
- The latest simulated question bank and answers of the eight members (Electrical constructors) of Sichuan architecture in 2022
- 《梦华录》“超点”,鹅被骂冤吗?
- [普通物理] 光栅衍射
- "Super point" in "Meng Hua Lu", is the goose wronged?
猜你喜欢

Apple doesn't need money, but it has no confidence in its content

微信小程序中使用vant组件

二叉树的基本性质与遍历

字节、腾讯也下场,这门「月赚3000万」的生意有多香?

Cooking business experience of young people: bloggers are busy selling classes and bringing goods, and the organization earns millions a month

Sequence stack version 1.0

基于QT+MySQL的相机租赁管理系统

Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!

云计算发展的 4 个阶段,终于有人讲明白了

Byte and Tencent have also come to an end. How fragrant is this business of "making 30million yuan a month"?
随机推荐
Map跟object 的区别
[cloud resident co creation] ModelBox draws your own painting across the air
两位湖南老乡,联手干出一个百亿IPO
等保备案是等保测评吗?两者是什么关系?
Otaku can't save yuan universe
Error in Android connection database query statement
Grating diffraction
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Difference between map and object
The latest simulated question bank and answers of the eight members (Electrical constructors) of Sichuan architecture in 2022
视频平台如何将旧数据库导入到新数据库?
微信小程序中使用vant组件
Anti epidemic through science and technology: white paper on network insight and practice of operators | cloud sharing library No.20 recommendation
物聯網?快來看 Arduino 上雲啦
“拯救”直播带货,一个董宇辉还不够
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
Mapstacks: data normalization and layered color layer loading
Accurate calculation of task progress bar of lol mobile game
[performance tuning basics] performance tuning standards
【CANN文档速递04期】揭秘昇腾CANN算子开发