全局数据
Global data can be modified from anywhere in the codebase, and there's no mechanism to detect which code made the modifications.
Global data is one of the most pungent code smells. The problem with global data is that it can be modified from any corner of the codebase, and there's no mechanism to detect which code made the modifications. Time and again, global data causes mysterious bugs where the source of the problem is somewhere far away, making it extremely difficult to find the erroneous code.
Global data violates the principle of encapsulation and makes code unpredictable. When data can be modified from anywhere, it becomes impossible to reason about the state of the system. The primary defense is to encapsulate the data with functions, allowing you to see where it's being modified and control access to it. Ideally, move the data into a class or module and allow only that module's code to use it, thus limiting its scope as much as possible.
import java.util.*;// Typical example of global dataclass GlobalConfig {// Global variables - can be accessed and modified from anywherepublic static String DATABASE_URL = "localhost:3306";public static int MAX_CONNECTIONS = 100;public static boolean DEBUG_MODE = false;public static Map<String, String> CACHE = new HashMap<>();public static List<String> ERROR_MESSAGES = new ArrayList<>();}// Singleton pattern can also cause global data problemsclass UserManager {private static UserManager instance;public static List<String> currentUsers = new ArrayList<>(); // Global mutable statepublic static int totalLogins = 0;public static UserManager getInstance() {if (instance == null) {instance = new UserManager();}return instance;}public void addUser(String username) {currentUsers.add(username);totalLogins++; // Global state is modified}}// Classes using global data - the problem is that it can be modified anywhereclass DatabaseService {public void connect() {// Direct access to global variablesString url = GlobalConfig.DATABASE_URL;int maxConn = GlobalConfig.MAX_CONNECTIONS;// Modify global stateGlobalConfig.CACHE.put("last_connection", new Date().toString());if (GlobalConfig.DEBUG_MODE) {System.out.println("Connecting to: " + url);}}public void logError(String error) {// Global error list is modifiedGlobalConfig.ERROR_MESSAGES.add(error);}}class UserService {public void login(String username) {// Modify global dataUserManager.getInstance().addUser(username);// Direct modification of global stateUserManager.totalLogins++;UserManager.currentUsers.add(username + "_session");}public void processUser() {// Depend on global stateif (UserManager.totalLogins > 1000) {GlobalConfig.DEBUG_MODE = true; // Modify global state here}// Modify global cacheGlobalConfig.CACHE.put("last_user_count",String.valueOf(UserManager.currentUsers.size()));}}// Another class also modifying the same global dataclass AdminService {public void resetSystem() {// Clear global dataGlobalConfig.CACHE.clear();GlobalConfig.ERROR_MESSAGES.clear();UserManager.currentUsers.clear();UserManager.totalLogins = 0;// Modify configurationGlobalConfig.MAX_CONNECTIONS = 50;GlobalConfig.DEBUG_MODE = false;}public void updateConfig(String dbUrl) {// Modify global configuration from another placeGlobalConfig.DATABASE_URL = dbUrl;GlobalConfig.CACHE.put("config_updated", "true");}}