当前位置:网站首页>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 .
边栏推荐
- Ribbon源码分析之@LoadBalanced与LoadBalancerClient
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- 《梦华录》“超点”,鹅被骂冤吗?
- 【CANN文档速递04期】揭秘昇腾CANN算子开发
- Byte and Tencent have also come to an end. How fragrant is this business of "making 30million yuan a month"?
- Nodered has no return value after successfully inserting into the database (the request cannot be ended)
- 首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
- [cann document express issue 06] first knowledge of tbe DSL operator development
- [suggested collection] time series prediction application and paper summary
- Ribbon source code analysis @loadbalanced and loadbalancerclient
猜你喜欢

The four stages of cloud computing development have finally been clarified

Nodered has no return value after successfully inserting into the database (the request cannot be ended)

物聯網?快來看 Arduino 上雲啦

"Ningwang" was sold and bought at the same time, and Hillhouse capital has cashed in billions by "selling high and absorbing low"

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

主数据建设的背景

Stackoverflow annual report 2022: what are developers' favorite databases?

虚拟化是什么意思?包含哪些技术?与私有云有什么区别?

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

物联网?快来看 Arduino 上云啦
随机推荐
基于QT+MySQL的相机租赁管理系统
Internet of things? Come and see Arduino on the cloud
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
建立自己的网站(14)
With its own cells as raw materials, the first 3D printing ear transplantation was successful! More complex organs can be printed in the future
Ribbon source code analysis @loadbalanced and loadbalancerclient
天天鉴宝暴雷背后:拖欠数千万、APP停摆,创始人预谋跑路?
Dongyuhui is not enough to bring goods to "rescue" live broadcast
Unit actual combat lol skill release range
Map跟object 的区别
顺序栈遍历二叉树
You can capture fingerprints with a mobile camera?! Accuracy comparable to signature and monogram, expert: you are aggravating discrimination
JVM tuning
伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
Otaku can't save yuan universe
When querying the database with Gorm, reflect: reflect flag. mustBeAssignable using unaddressable value
大一女生废话编程爆火!懂不懂编程的看完都拴Q了
VMware virtual machine setting static IP
Nodered has no return value after successfully inserting into the database (the request cannot be ended)
Jd.com: how does redis implement inventory deduction? How to prevent oversold?