异曲同工的类
Two classes have similar functionality but different interfaces.
When two classes do the same thing but have different method names, this smell occurs. It increases learning costs and maintenance difficulty, and violates the principle of consistency.
These two classes do the same thing but with different interfaces. The interfaces should be unified or the classes merged. This reduces learning costs and improves code consistency.
import java.io.*;import java.nio.file.*;class FileReader {public String readFile(String filename) throws IOException {return new String(Files.readAllBytes(Paths.get(filename)));}public boolean fileExists(String filename) {return Files.exists(Paths.get(filename));}public long getFileSize(String filename) throws IOException {return Files.size(Paths.get(filename));}}class DocumentLoader {// Same functionality but different method namespublic String loadDocument(String path) throws IOException {return new String(Files.readAllBytes(Paths.get(path)));}public boolean documentExists(String path) {return Files.exists(Paths.get(path));}// Different parameter order and return typepublic int getDocumentSize(String path) throws IOException {return (int) Files.size(Paths.get(path));}}