面试题答案
一键面试设计模式组合
- 策略模式:
- 对于不同编码格式的文本文件处理,可以使用策略模式。策略模式定义一系列算法,将每个算法封装起来,使它们可以相互替换。这样,针对不同编码格式(如UTF - 8、GBK等),可以创建不同的策略类来处理文本文件的读取和解析。
- 对于二进制文件加密处理,也可以采用策略模式。不同的加密算法(如AES、DES等)可以封装成不同的策略类,根据实际需求选择相应的加密策略。
- 工厂模式:
- 结合工厂模式来创建上述策略对象。工厂模式负责创建对象,它可以根据配置或运行时条件,返回合适的策略对象。例如,创建一个
EncodingStrategyFactory
来创建不同编码格式处理策略对象,创建一个EncryptionStrategyFactory
来创建不同加密策略对象。
- 结合工厂模式来创建上述策略对象。工厂模式负责创建对象,它可以根据配置或运行时条件,返回合适的策略对象。例如,创建一个
具体代码修改方向
- 定义策略接口和实现类:
- 文本文件编码处理策略:
public interface TextEncodingStrategy { String readTextFile(String filePath); } public class UTF8EncodingStrategy implements TextEncodingStrategy { @Override public String readTextFile(String filePath) { // 使用UTF - 8编码读取文件的逻辑 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"))) { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line).append("\n"); } return content.toString(); } catch (IOException e) { e.printStackTrace(); return null; } } } public class GBKEncodingStrategy implements TextEncodingStrategy { @Override public String readTextFile(String filePath) { // 使用GBK编码读取文件的逻辑 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "GBK"))) { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line).append("\n"); } return content.toString(); } catch (IOException e) { e.printStackTrace(); return null; } } }
- 二进制文件加密策略:
public interface BinaryEncryptionStrategy { byte[] encryptBinaryFile(byte[] fileContent); } public class AESBinaryEncryptionStrategy implements BinaryEncryptionStrategy { @Override public byte[] encryptBinaryFile(byte[] fileContent) { // 使用AES算法加密二进制文件内容的逻辑 // 此处仅为示例,实际需实现AES加密算法 return fileContent; } } public class DESBinaryEncryptionStrategy implements BinaryEncryptionStrategy { @Override public byte[] encryptBinaryFile(byte[] fileContent) { // 使用DES算法加密二进制文件内容的逻辑 // 此处仅为示例,实际需实现DES加密算法 return fileContent; } }
- 文本文件编码处理策略:
- 创建工厂类:
- 文本文件编码策略工厂:
public class EncodingStrategyFactory { public static TextEncodingStrategy getEncodingStrategy(String encodingType) { if ("UTF-8".equalsIgnoreCase(encodingType)) { return new UTF8EncodingStrategy(); } else if ("GBK".equalsIgnoreCase(encodingType)) { return new GBKEncodingStrategy(); } return null; } }
- 二进制文件加密策略工厂:
public class EncryptionStrategyFactory { public static BinaryEncryptionStrategy getEncryptionStrategy(String encryptionType) { if ("AES".equalsIgnoreCase(encryptionType)) { return new AESBinaryEncryptionStrategy(); } else if ("DES".equalsIgnoreCase(encryptionType)) { return new DESBinaryEncryptionStrategy(); } return null; } }
- 文本文件编码策略工厂:
- 修改模板方法模式:
- 在模板方法模式的抽象类中,添加获取策略对象的方法,并在具体的模板方法中调用策略对象的方法。
public abstract class FileProcessor { // 获取文本文件编码策略 protected abstract TextEncodingStrategy getTextEncodingStrategy(); // 获取二进制文件加密策略 protected abstract BinaryEncryptionStrategy getBinaryEncryptionStrategy(); public void processFile(String filePath, String fileType) { if ("text".equalsIgnoreCase(fileType)) { TextEncodingStrategy strategy = getTextEncodingStrategy(); if (strategy != null) { String content = strategy.readTextFile(filePath); // 后续对文本内容的处理逻辑 } } else if ("binary".equalsIgnoreCase(fileType)) { BinaryEncryptionStrategy strategy = getBinaryEncryptionStrategy(); if (strategy != null) { try (FileInputStream fis = new FileInputStream(filePath)) { byte[] fileContent = fis.readAllBytes(); byte[] encryptedContent = strategy.encryptBinaryFile(fileContent); // 后续对加密后二进制内容的处理逻辑 } catch (IOException e) { e.printStackTrace(); } } } } }
- 创建具体的实现类,实现获取策略对象的方法:
public class SpecificFileProcessor extends FileProcessor { private String textEncodingType; private String binaryEncryptionType; public SpecificFileProcessor(String textEncodingType, String binaryEncryptionType) { this.textEncodingType = textEncodingType; this.binaryEncryptionType = binaryEncryptionType; } @Override protected TextEncodingStrategy getTextEncodingStrategy() { return EncodingStrategyFactory.getEncodingStrategy(textEncodingType); } @Override protected BinaryEncryptionStrategy getBinaryEncryptionStrategy() { return EncryptionStrategyFactory.getEncryptionStrategy(binaryEncryptionType); } }
- 使用示例:
public class Main { public static void main(String[] args) { FileProcessor processor = new SpecificFileProcessor("UTF - 8", "AES"); processor.processFile("test.txt", "text"); processor.processFile("test.bin", "binary"); } }
通过上述策略模式和工厂模式与模板方法模式的组合,可以有效地优化代码,使其适应处理不同编码格式的文本文件和对二进制文件进行加密处理的需求。