第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將域?qū)ο笈c業(yè)務(wù)邏輯耦合

將域?qū)ο笈c業(yè)務(wù)邏輯耦合

犯罪嫌疑人X 2022-07-20 10:50:08
假設(shè)我們有一個領(lǐng)域?qū)ο蠛瓦@個對象的評估類。例如,在單獨的類中的促銷及其評估邏輯:class BuyXGetYFreePromotion extends AbstractPromotion{   String x;   String y;}class BuyXGetYFreePromotionEvaluation {   public void evaluate(Cart cart){       for(String product : cart.getProducts()){           if(product.equal(BuyXGetYFreePromotion.x)){              //some code to discount y price           }       }   }}另一個例子:class FixedPricePromotion extends AbstractPromotion{    String product;    Double price;}class FixedPricePromotionEvaluation {    public void evaluate(Cart cart){        for(String product : cart.getProducts()){           if(product.equal(FixedPricePromotion.product)){              //some code to discount y price           }        }    }}我們有很多這樣的對子。我們不能在域?qū)ο笾凶⑷朐u估,但我們可以將它們關(guān)聯(lián)到評估類或其他類中。第一個選項是將它們與 instanceof 語句相關(guān)聯(lián)。例如:class PromotionService {    void evaluation(Cart cart){        for(AbstractPromotion p : getPromotions){            if(p instanceof BuyXGetYFreePromotion)               BuyXGetYFreePromotionEvaluation.evaluate(cart);            else if(p instanceof FixedPricePromotion)               FixedPricePromotionEvaluation.evaluate(cart);        }    }}但是這個例子違反了開閉原則。我的問題是,我應(yīng)該如何通過考慮 SOLID 原則來耦合這些對。
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

1)模型領(lǐng)域驅(qū)動設(shè)計方式


通過使域?qū)ο缶哂行袨?,您可以使事情變得更簡單?/p>

例如,您可以將促銷和促銷評估合并到一個類中。

貧血的對象不一定是最好的東西。

例如 :


class FixedPricePromotion extends AbstractPromotion{

    String product;

    Double price;

    public void evaluate(Cart cart){

        for(String product : cart.getProducts()){

           if(product.equal(product)){

              //some code to discount y price

           }

        }

    }    

}

現(xiàn)在可以通過這種方式更改 PromotionService :


class PromotionService {

    void evaluation(Cart cart){

        for(AbstractPromotion p : getPromotions){                

               p.evaluate(cart);

        }

    }

}

2)模型域和邏輯分離的方式


如果您不想合并它們,則可以將它們橋接起來,這要感謝從一個到另一個的字段依賴關(guān)系。


PromotionEvaluation可以是一個接口,它定義了在子類中定義的邏輯和抽象isMatch()和applyPromotion()方法的模板:


public interface PromotionEvaluation{


       boolean isMatch(String product);

       void applyPromotion(String product);

       default void evaluate(Cart cart){

            for(String product : cart.getProducts()){

               if(isMatch(product)){

                  applyPromotion(product);

               }

            }

        }       

}

子類可以是:


class FixedPricePromotionEvaluation implements PromotionEvaluation{

    FixedPricePromotion promotion;


    public FixedPricePromotionEvaluation(FixedPricePromotion promotion){

      this.promotion = promotion;

    }


    public boolean isMatch(String product){

       return product.equal(promotion.product)

    }


    public void applyPromotion(String product){

      // do your logic

    }



}

現(xiàn)在您可以以這種方式迭代評估:


class PromotionService {

    void evaluation(Cart cart){

        for(PromotionEvaluation evaluation : getEvaluations()){

            e.evaluate(cart);

        }

    }

}


查看完整回答
反對 回復 2022-07-20
  • 1 回答
  • 0 關(guān)注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號