Smell Detail

Large Class

过大的类

Bloaters

A class contains too many fields, methods, or lines of code, violating the Single Responsibility Principle.

Define

Introduction

When a class tries to do too many things, it becomes excessively large. Large classes are hard to understand and maintain and usually signal a violation of the Single Responsibility Principle (SRP).

Key Identifiers

  • The class has an unusually high line count
  • Too many fields and methods
  • Multiple unrelated responsibilities are mixed together
  • Hard to articulate the class's single, clear purpose

Refactoring Suggestions

  1. Extract Class: Move cohesive groups of fields and methods into new classes
  2. Use Delegation / Composition: Prefer composition over inheritance for separating concerns
  3. Enforce Single Responsibility: Ensure each class has only one reason to change

Explanation

This class handles game management, rendering, audio, networking, file system access, and more. It should be split into multiple specialized classes, each owning a specific domain concern.

Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.util.*;
class GameManager {
// Player management
private List<Player> players;
private Player currentPlayer;
// Game state
private GameState state;
private int currentLevel;
private int score;
private boolean isPaused;
private long gameStartTime;
// Rendering related
private Renderer renderer;
private Camera camera;
private List<Sprite> sprites;
private List<Texture> textures;
private RenderSettings renderSettings;
// Audio related
private AudioEngine audioEngine;
private List<Sound> sounds;
private float masterVolume;
private boolean isMuted;
// Network related
private NetworkManager network;
private List<NetworkMessage> messages;
private String serverAddress;
private boolean isConnected;
// File system
private FileManager fileManager;
private SaveData saveData;
private String saveFilePath;
// Input handling
private InputManager inputManager;
private Map<String, Boolean> keyStates;
// Physics
private PhysicsEngine physicsEngine;
private List<RigidBody> rigidBodies;
public void updateGame() {
updatePhysics();
updatePlayers();
updateNetworking();
checkCollisions();
}
public void renderGame() {
renderer.clear();
renderSprites();
renderUI();
renderer.present();
}
public void handleInput() {
inputManager.update();
processKeyboard();
processMouse();
}
public void playSound(int soundId) {
if (!isMuted && soundId < sounds.size()) {
audioEngine.play(sounds.get(soundId));
}
}
public void saveGame() {
saveData.setLevel(currentLevel);
saveData.setScore(score);
fileManager.save(saveData, saveFilePath);
}
public void loadGame() {
saveData = fileManager.load(saveFilePath);
currentLevel = saveData.getLevel();
score = saveData.getScore();
}
public void sendNetworkMessage(String message) {
if (isConnected) {
network.send(new NetworkMessage(message));
}
}
public void receiveNetworkMessage() {
messages.addAll(network.receive());
}
public void addPlayer(Player player) {
players.add(player);
}
public void removePlayer(int playerId) {
players.removeIf(p -> p.getId() == playerId);
}
public void updateCamera() {
if (currentPlayer != null) {
camera.setPosition(currentPlayer.getPosition());
}
}
public void loadTextures() {
textures.clear();
textures.addAll(fileManager.loadTextures());
}
// Physics methods
private void updatePhysics() {
physicsEngine.update();
}
private void checkCollisions() {
for (RigidBody body : rigidBodies) {
physicsEngine.checkCollisions(body);
}
}
// Rendering methods
private void renderSprites() {
for (Sprite sprite : sprites) {
renderer.draw(sprite);
}
}
private void renderUI() {
renderer.drawText("Score: " + score, 10, 10);
renderer.drawText("Level: " + currentLevel, 10, 30);
}
// Input methods
private void processKeyboard() {
if (keyStates.get("SPACE")) {
currentPlayer.jump();
}
if (keyStates.get("LEFT")) {
currentPlayer.moveLeft();
}
if (keyStates.get("RIGHT")) {
currentPlayer.moveRight();
}
}
private void processMouse() {
// Mouse input processing logic
}
// Player methods
private void updatePlayers() {
for (Player player : players) {
player.update();
}
}
// Network methods
private void updateNetworking() {
receiveNetworkMessage();
processNetworkMessages();
}
private void processNetworkMessages() {
for (NetworkMessage msg : messages) {
handleNetworkMessage(msg);
}
messages.clear();
}
private void handleNetworkMessage(NetworkMessage message) {
// Process individual network messages
}
// ... many more methods would continue here
}
// Supporting classes for demonstration
class Player {
public int getId() { return 0; }
public Vector getPosition() { return new Vector(); }
public void jump() {}
public void moveLeft() {}
public void moveRight() {}
public void update() {}
}
class Vector {
public double x, y;
}
enum GameState { MENU, PLAYING, PAUSED, GAME_OVER }
class Renderer {
public void clear() {}
public void present() {}
public void draw(Sprite sprite) {}
public void drawText(String text, int x, int y) {}
}
class Camera {
public void setPosition(Vector position) {}
}
class Sprite {}
class Texture {}
class RenderSettings {}
class AudioEngine {
public void play(Sound sound) {}
}
class Sound {}
class NetworkManager {
public void send(NetworkMessage message) {}
public List<NetworkMessage> receive() { return new ArrayList<>(); }
}
class NetworkMessage {
public NetworkMessage(String message) {}
}
class FileManager {
public void save(SaveData data, String path) {}
public SaveData load(String path) { return new SaveData(); }
public List<Texture> loadTextures() { return new ArrayList<>(); }
}
class SaveData {
public void setLevel(int level) {}
public void setScore(int score) {}
public int getLevel() { return 0; }
public int getScore() { return 0; }
}
class InputManager {
public void update() {}
}
class PhysicsEngine {
public void update() {}
public void checkCollisions(RigidBody body) {}
}
class RigidBody {}