代码重构挑战:Java简化复杂逻辑实例

原创 待我称王封你为后i 2024-12-07 07:27 91阅读 0赞

在Java编程中,经常需要处理复杂的业务逻辑。以下是一个简化复杂逻辑的实例,我们假设有一个库存管理系统:

原始代码(可能存在的问题和冗余):

  1. public class InventorySystem {
  2. private List<Product> products;
  3. // constructor, getters and setters
  4. public void processOrder(Order order) {
  5. for (Product product : order.getProducts()) {
  6. if (!products.contains(product)) {
  7. System.out.println("Adding product to inventory: " + product);
  8. products.add(product);
  9. } else {
  10. System.out.println("Updating product in inventory: " + product);
  11. }
  12. }
  13. // 计算订单总价并更新库存
  14. double total = order.calculateTotal();
  15. if (total > 0) {
  16. System.out.println("Order processed. Total: " + total);
  17. // 根据订单数量减少库存
  18. for (Product product : order.getProducts()) {
  19. products.remove(product);
  20. if (product.getQuantity() > 0) {
  21. int remaining = product.getQuantity() - order.getProducts().get(index).getQuantity();
  22. if (remaining > 0) {
  23. System.out.println("Reducing inventory for " + product + ": " + remaining);
  24. products.add(product.withQuantity(remaining)));
  25. } else {
  26. System.out.println("No inventory to reduce for " + product);
  27. }
  28. }
  29. }
  30. } else {
  31. System.out.println("Order cannot be processed due to negative total.");
  32. }
  33. }
  34. }

重构实例:

  1. processOrder方法拆分为多个方法,例如addProductToInventoryupdateProductInInventory等。

  2. 创建一个InventoryService类来管理库存逻辑,将processOrder调用移到服务中。

  3. 优化代码结构和命名规范,提高可读性和维护性。

重构后的代码示例:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class InventorySystem {
  4. private List<Product> products = new ArrayList<>();
  5. // constructor, getters and setters
  6. public void addProductToInventory(Product product) {
  7. if (!products.contains(product)) {
  8. System.out.println("Adding product to inventory: " + product);
  9. products.add(product);
  10. } else {
  11. System.out.println("Updating product in inventory: " + product);
  12. }
  13. }
  14. // ...其他方法
  15. public void processOrder(Order order) {
  16. for (Product product : order.getProducts()) {
  17. InventoryService service = new InventoryService(products);
  18. String operationResult;
  19. if (!products.contains(product)) {
  20. operationResult = "Adding product to inventory: " + product;
  21. } else {
  22. operationResult = "Updating product in inventory: " + product;
  23. }
  24. System.out.println(operationResult);
  25. // 根据逻辑进行库存操作
  26. service.productInInventoryAction(product, order));
  27. // ...其他逻辑处理
  28. }
  29. }
  30. }
  31. class InventoryService {
  32. private List<Product> products;
  33. public InventoryService(List<Product> products) {
  34. this.products = products;
  35. }
  36. // ...产品库存操作方法
  37. }
  38. class Product {
  39. // ...属性和构造器
  40. // ...产品库存操作方法(用于InventoryService)
  41. }

这个重构后的代码示例更清晰地展示了如何将复杂的逻辑拆分为多个可管理的方法。

文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

表情:
评论列表 (有 0 条评论,91人围观)

还没有评论,来说两句吧...

相关阅读