当前位置:网站首页>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 AI for emotion recognition was "harbouring evil intentions", and Microsoft decided to block it!
- lol手游之任务进度条精准计算
- Otaku can't save yuan universe
- 顺序表的基本操作
- Internet of things? Come and see Arduino on the cloud
- Apple doesn't need money, but it has no confidence in its content
- 云计算发展的 4 个阶段,终于有人讲明白了
- 建立自己的网站(14)
- 两位湖南老乡,联手干出一个百亿IPO
- RF_ DC system clock setting gen1/gen2
猜你喜欢
顺序栈遍历二叉树
Bytebase加入阿里云PolarDB开源数据库社区
Camera rental management system based on qt+mysql
Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?
Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable
图像PANR
天天鉴宝暴雷背后:拖欠数千万、APP停摆,创始人预谋跑路?
Image panr
2022年最新四川建筑八大员(电气施工员)模拟题库及答案
Basic properties and ergodicity of binary tree
随机推荐
Hosting service and SASE, enjoy the integration of network and security | phase I review
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
Cooking business experience of young people: bloggers are busy selling classes and bringing goods, and the organization earns millions a month
Anti epidemic through science and technology: white paper on network insight and practice of operators | cloud sharing library No.20 recommendation
Wechat applet custom tabbar
Basic operation of sequence table
Wait for the victory of the party! After mining ebb tide, graphics card prices plummeted across the board
1、 Downloading and installing appium
【CANN文档速递04期】揭秘昇腾CANN算子开发
gateway
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"
Bean lifecycle flowchart
Stackoverflow annual report 2022: what are developers' favorite databases?
Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?
Q1: error in JMeter filename must not be null or empty
Some ideas about chaos Engineering
Accurate calculation of task progress bar of lol mobile game
全上链哈希游戏dapp系统定制(方案设计)
Ribbon源码分析之@LoadBalanced与LoadBalancerClient
科技抗疫: 运营商网络洞察和实践白皮书 | 云享书库NO.20推荐