如何避免因小改动而更新多个类?

时间:2019-07-16 18:39:36

标签: java

我的应用程序以消息形式获取产品。

class Product{
int id;
int qty;
List<Product> subProducts;
}

class BuyAction{        
         // Existing code here      
        // below is new code added to hold list of buyAction

        List<BuyAction> buyAction = new ArrayList<>();

        public void add(List<BuyAction> buyActions){
                   this.buyAction = buyActions;
        }

        public List<BuyAction> get(){ 
              return buyAction;
        }
}

下面的“创建”函数接受Message类型的参数

Class Receive{
    private ReadAction create(Message m){
        BuyRequest buyRequest = getBuyRequest(message);
        List<BuyRequest> buyRequestList= (message, message.getSubProducts)
        BuyAction buyAction = new BuyAction(buyRequest, null);

        List<BuyAction> buyForSub = createBuyActionForSubProducts(
        buyRequestList,null)

        buyAction.add(buyForSub) //i want to avoid doing this. Beacuse of 
        //this change i have to change BuyAction object to hold buyForSub and get buyForSub
    }
}


//The purpose of createFinalAction below is to create FinalAction for product and its subProduct
Class XYZ{

  private FinalAction createFinalAction(BuyAction buyAction){
       List<BuyAction>        buyActionForSubProducts =      buyAction.get();

     FinalAction finalAction = new FinalAction(buyAction.item);

     List<FinalAction> finalActionsForSubProducts = //code for subProducts

      // "add" function is new piece of code added to FinalAction to hold list of FinalAction for subProducts
    finalAction.add(finalActionsForSubProducts)   
  }

}

此代码的主要目的是以一种干净的方式为Product及其子产品创建FinalAction。

在此代码中,必须为根订单和subProduct创建BuyAction和FinalAction。对于子产品,我必须将BuyAction和FinalAction更改为商店列表。是否有其他方法可以执行此操作而无需更改BuyAction和FinalAction来保存列表

0 个答案:

没有答案