Java設(shè)計(jì)模式之Strategy模式
基于有了OO的基礎(chǔ)后,開始認(rèn)真學(xué)習(xí)設(shè)計(jì)模式!設(shè)計(jì)模式是java設(shè)計(jì)中必不可少的!
Apple.java
package strategy; /** * * @author Andy * */ public class Apple implements Discountable { //重量 private double weight; //單價(jià) 實(shí)際開發(fā)中 設(shè)計(jì)金錢等精確計(jì)算都是BigDecimal; private double price; //按購買量打折 // private Discountor d = new AppleWeightDiscountor(); //按購買總價(jià)打折 private Discountor d = new ApplePriceDiscountor(); public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Apple (double weight,double price ){ super(); this.weight=weight; this.price=price; } @Override public void discountSell() { d.discount(this); } }
Banana.java
package strategy; /** * * @author Andy * */ public class Banana implements Discountable { //重量 private double weight; ////單價(jià) 實(shí)際開發(fā)中 涉及金錢等精確計(jì)算都是用BigDecimal private double price; public Banana(double weight, double price) { super(); this.weight = weight; this.price = price; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public void discountSell() { //打折算法 if(weight < 5) { System.out.println("Banana未打折價(jià)錢: " + weight * price); }else if(weight >= 5 && weight < 10) { System.out.println("Banana打八八折價(jià)錢: " + weight * price * 0.88 ); }else if(weight >= 10) { System.out.println("Banana打五折價(jià)錢: " + weight * price * 0.5 ); } } }
Market.java
package strategy; /** * * @author Andy * */ public class Market { /** * 對(duì)可打折的一類事物進(jìn)行打折 * @param apple */ public static void discountSell(Discountable d) { d.discountSell(); } }
Discountable.java
package strategy; /** * * @author Andy * */ public interface Discountable { public void discountSell(); }
Test.java
package strategy; /** * * @author Andy * */ public class Test { /** * * @param args */ public static void main(String[] args) { // 只能對(duì)蘋果打折 還不能對(duì)通用的一類事物打折 而且都是要賣什么就寫什么打折算法 // 其實(shí)每類事物打折算法又是不一致的 Discountable d = new Apple(10.3, 3.6); Discountable d1= new Banana(5.4,1.1); Market.discountSell(d); Market.discountSell(d1); } }
相關(guān)文章
Java實(shí)現(xiàn)重定向過程中添加請(qǐng)求頭信息
在Java中,我們經(jīng)常需要使用網(wǎng)絡(luò)請(qǐng)求來與服務(wù)器進(jìn)行通信,在進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí),有時(shí)我們需要在重定向過程中添加請(qǐng)求頭信息,本文將介紹如何使用Java在重定向過程中添加請(qǐng)求頭,并提供相應(yīng)的代碼示例,2023-10-10Java Web Listener實(shí)現(xiàn)事件監(jiān)聽與處理
Java Web開發(fā)中的Listener是一種事件機(jī)制,通過監(jiān)聽Web應(yīng)用程序的事件,實(shí)現(xiàn)對(duì)事件的處理,從而實(shí)現(xiàn)更加靈活和高效的應(yīng)用程序開發(fā)。Listener能夠監(jiān)聽的事件包括應(yīng)用程序啟動(dòng)和關(guān)閉、Session創(chuàng)建和銷毀、請(qǐng)求和響應(yīng)對(duì)象的創(chuàng)建和銷毀等2023-04-04Spring 事務(wù)事件監(jiān)控及實(shí)現(xiàn)原理解析
本文首先會(huì)使用實(shí)例進(jìn)行講解Spring事務(wù)事件是如何使用的,然后會(huì)講解這種使用方式的實(shí)現(xiàn)原理。感興趣的朋友跟隨小編一起看看吧2018-09-09SpringBoot開發(fā)存儲(chǔ)服務(wù)器實(shí)現(xiàn)過程詳解
這篇文章主要為大家介紹了SpringBoot開發(fā)存儲(chǔ)服務(wù)器實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Java8新日期時(shí)間API的20個(gè)使用示例
這篇文章主要介紹了Java8新日期時(shí)間API的20個(gè)使用示例,為了學(xué)習(xí)Java 8的這個(gè)新庫,這里我創(chuàng)建了20個(gè)以任務(wù)為導(dǎo)向的例子,需要的朋友可以參考下2015-03-03startActivityForResult和setResult案例詳解
這篇文章主要介紹了startActivityForResult和setResult案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08關(guān)于JpaRepository的關(guān)聯(lián)查詢和@Query查詢
這篇文章主要介紹了JpaRepository的關(guān)聯(lián)查詢和@Query查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java基于Session登錄驗(yàn)證的實(shí)現(xiàn)示例
基于Session的登錄驗(yàn)證方式是最簡單的一種登錄校驗(yàn)方式,本文主要介紹了Java基于Session登錄驗(yàn)證的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02IntelliJ?IDEA?2023.2最新版激活方法及驗(yàn)證ja-netfilter配置是否成功
隨著2023.2版本的發(fā)布,用戶們渴望了解如何激活這個(gè)最新版的IDE,本文將介紹三種可行的激活方案,包括許可證服務(wù)器、許可證代碼和idea?vmoptions配置,幫助讀者成功激活并充分利用IDEA的功能,感興趣的朋友參考下吧2023-08-08