内幕交易
Excessive private data exchange between modules increases coupling and should be reduced with transparent communication mechanisms.
Software developers like to build high walls between modules and are extremely averse to extensive data exchange between modules, as this increases coupling. In practice, some data exchange is inevitable, but we must minimize this and make all such exchanges transparent.
When two modules are always "whispering by the coffee machine", it indicates they have inappropriate intimacy that should be addressed. If two modules have common interests, try creating a new module to put shared data in a well-managed place. Inheritance often causes conspiracy because subclasses always know more about their superclass than the latter would prefer. Sometimes it's time to let the child live independently from the inheritance hierarchy.
import java.util.*;// example: classes sharing data through back channelsclass EmailService {private Map<String, String> emailTemplates = new HashMap<>();public String getTemplate(String type) {return emailTemplates.get(type);}public void setTemplate(String type, String template) {emailTemplates.put(type, template);}// Exposing internal map - dangerouspublic Map<String, String> getAllTemplates() {return emailTemplates; // Direct access to internal data}}class NotificationService {private EmailService emailService;public NotificationService(EmailService emailService) {this.emailService = emailService;}public void customizeNotifications() {// Directly manipulating EmailService's internal dataMap<String, String> templates = emailService.getAllTemplates();templates.put("welcome", "Custom welcome message"); // Back-channel modificationtemplates.remove("spam"); // Direct manipulation of another class's data}}