Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设计模式——中介者模式 #29

Open
hoperyy opened this issue Jun 25, 2017 · 0 comments
Open

设计模式——中介者模式 #29

hoperyy opened this issue Jun 25, 2017 · 0 comments

Comments

@hoperyy
Copy link
Owner

hoperyy commented Jun 25, 2017

中介者模式是迎合迪米特法则的一种实现。

迪米特法则也叫最少知识原则,是指一个对象应该尽可能少地了解另外的对象(类似不和陌生人说话)。如果对象之间的耦合度太高,一个对象发生改变后,难免会影响到其他的对象,跟“城门失火,殃及池鱼”的道理是一样的。

中介者模式使得各个对象之间得以解耦,以中介者和对象之间的一对多关系取代了对象之间的网状多对多的关系。

而中介者的缺点:新增了一个中介者对象。因为对象之间交互的复杂性,转移成了中介者对象的复杂性,使得中介者对象经常是巨大的,中介者对象自身就是一个难以维护的对象。

中介者模式可以非常方便地对模块或对象进行解耦,但对象之间并非一定需要解耦。在实际项目中,模块或对象之间有一些依赖关系是很正常的,关键就在于如何去衡量对象之间的耦合程度和项目效率的关系。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant