代码之家  ›  专栏  ›  技术社区  ›  j panton

使用几乎完全重复的代码重构函数,但对其他方法的调用不同

  •  1
  • j panton  · 技术社区  · 7 年前

    我正在学习Java,正在学习在线课程等,我正在做一个编码练习,发现我的两种方法之间有很多重复,如下所示:

    private static void addCustomerTransaction() {
        System.out.println("Enter the branch name:");
        String branchName = scanner.nextLine();
    
        System.out.println("Enter the customer name:");
        String customerName = scanner.nextLine();
    
        System.out.println("Enter the transaction");
        while (!scanner.hasNextDouble()) {
            scanner.next();
        }
        double transaction = scanner.nextDouble();
    
        bank.addCustomerTransaction(branchName,customerName,transaction);
    
    }
    
    private static void addCustomer() {
        System.out.println("Enter the branch name:");
        String branchName = scanner.nextLine();
    
        System.out.println("Enter the customer name:");
        String customerName = scanner.nextLine();
    
        System.out.println("Enter the transaction");
        while (!scanner.hasNextDouble()) {
            scanner.next();
        }
        double transaction = scanner.nextDouble();
    
        bank.addCustomer(branchName,customerName,transaction);
    }
    

    我想知道如何重构这些方法以减少重复。我可以做到:

    private static void addCustomerTransaction() {
        customerInput();
    
    }
    
    private static void addCustomer() {
        customerInput();
    }
    
    private static void customerInput() {
        System.out.println("Enter the branch name:");
        String branchName = scanner.nextLine();
    
        System.out.println("Enter the customer name:");
        String customerName = scanner.nextLine();
    
        System.out.println("Enter the transaction");
        while (!scanner.hasNextDouble()) {
            scanner.next();
        }
        double transaction = scanner.nextDouble();
    
        bank.addCustomerTransaction(branchName,customerName,transaction);
    }
    

    但我不知道如何使代码能够更改方法调用(当前 bank.addCustomerTransaction(branchName,customerName,transaction); customerInput 函数调用 .

    有人能就下一步提出建议吗?

    4 回复  |  直到 7 年前
        1
  •  1
  •   assylias    7 年前

    这里有一个选择。

    为最后一个方法创建接口:

    @FunctionalInterface public interface CustomerOperation {
      void apply(Bank bank, String branch, String customer, String transaction);
    }
    

    private static void customerInput(CustomerOperation operation) {
      //common code here
      operation.apply(bank, branchName, customerName, transaction);
    }
    

    你这样称呼它:

    private static void addCustomerTransaction() {
      customerInput((bank, branchName, customerName, transaction) ->
          bank.addCustomerTransaction(branchName, customerName, transaction));
    }
    
    private static void addCustomer() {
      customerInput((bank, branchName, customerName, transaction) ->
          bank.addCustomer(branchName, customerName, transaction));
    }
    

    或者使用方法引用:

    private static void addCustomerTransaction() {
      customerInput(Bank::addCustomerTransaction);
    }
    
    private static void addCustomer() {
      customerInput(Bank::addCustomer);
    }
    
        2
  •  1
  •   Yannik    7 年前

    public class TransactionInfo {
    
      private String branchName;
      private String customerName;
      private Double transaction;
    
      public TransactionInfo(String branchName, String customerName, Double transaction) {
        this.branchName = branchName;
        this.customerName = customerName;
        this.transaction = transaction;
      }
    
      ...
    }
    
    private static TransactionInfo customerInput() {
      System.out.println("Enter the branch name:");
      String branchName = scanner.nextLine();
    
      System.out.println("Enter the customer name:");
      String customerName = scanner.nextLine();
    
      System.out.println("Enter the transaction");
      while (!scanner.hasNextDouble()) {
        scanner.next();
      }
      double transaction = scanner.nextDouble();
    
      return new TransactionInfo(branchName, customerName, transaction);
    }
    
    private static void addCustomerTransaction() {
      TransactionInfo transactionInfo = customerInput();
      bank.addCustomerTransaction(transactionInfo.getBranchName(), transactionInfo.getCustomerName(), transactionInfo.getTransaction());
    }
    
    private static void addCustomer() {
      TransactionInfo transactionInfo = customerInput();
      bank.addCustomer(transactionInfo.getBranchName(), transactionInfo.getCustomerName(), transactionInfo.getTransaction());
    }
    

    如果你能控制 bank ,你可以考虑 addCustomer addCustomerTransaction 方法接受 TransactionInfo 作为参数。

    为了那个班。但我希望你能明白。

    你的想法是正确的,把公共代码提取到一个方法中。您可以看到,我的解决方案基本上只添加了一个类,供客户输入用作 customerInput

        3
  •  1
  •   displayName    7 年前
    private static double readTransaction() {
        System.out.println("Enter the transaction");
        while (!scanner.hasNextDouble()) {
            scanner.next();
        }
        double transaction = scanner.nextDouble();
        return transaction;
    }
    
    private static String readCustomerName() {
        System.out.println("Enter the customer name:");
        String customerName = scanner.nextLine();
        return customerName;
    }
    
    private static String readBranch() {
        System.out.println("Enter the branch name:");
        String branchName = scanner.nextLine();
        return branchName;
    }
    
    private static void addCustomer() {
        String branchName = readBranch();
        String customerName = readCustomerName();
        double transaction = readTransaction();
        bank.addCustomer(branchName,customerName,transaction);
    }
    
    private static void addCustomerTransaction() {
        String branchName = readBranch();
        String customerName = readCustomerName();
        double transaction = readTransaction();
        bank.addCustomerTransaction(branchName,customerName,transaction);
    }
    

    而且,你可以 Preserve Whole Object 通过创建包含分支名称、客户名称和事务的类,并执行以下操作:

    class Receipt {
        private String customerName;
        private String branchName;
        private double transaction;
    
        //Getters and setters...
    
        public void populateReceipt () {
            customerName = readCustomer();
            branchName = readBranch();
            transaction = readTransaction();
        }
    }
    

    然后。。。

    private void addCustomer (Receipt receipt) {
        //Modify the method to take in a receipt, rather than its 3 components
        bank.addCustomer(receipt);
    }
    
    private void addCustomerTransaction (Receipt receipt) {
        //Modify the method to take in a receipt, rather than its 3 components
        bank.addCustomerTransaction(receipt);
    }
    
        4
  •  0
  •   maha    7 年前

    如果要使它们成为addCustomerTransaction或addCustomer,可以使用另一个参数定义函数,如下所示:

    private static void addCustomer(boolean transaction) {
    System.out.println("Enter the branch name:");
    String branchName = scanner.nextLine();
    
    System.out.println("Enter the customer name:");
    String customerName = scanner.nextLine();
    
    System.out.println("Enter the transaction");
    while (!scanner.hasNextDouble()) {
        scanner.next();
    }
    double transaction = scanner.nextDouble();
    
    if( transaction ) {
           bank.addCustomerTransaction(branchName,customerName,transaction);
      } else {
          bank.addCustomer(branchName,customerName,transaction);
      }
    }