Java Design Patterns Strategy Pattern
Based on the foundation of OO, start studying design patterns seriously! Design patterns are essential in java design!
Apple.java
package strategy; /** * * @author Andy * */ public class Apple implements Discountable { //重量 private double weight; //单价 实际开发中 设计金钱等精确计算都是BigDecimal; private double price; //按购买量打折 // private Discountor d = new AppleWeightDiscountor(); //按购买总价打折 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; ////单价 实际开发中 涉及金钱等精确计算都是用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未打折价钱: " + weight * price); }else if(weight >= 5 && weight < 10) { System.out.println("Banana打八八折价钱: " + weight * price * 0.88 ); }else if(weight >= 10) { System.out.println("Banana打五折价钱: " + weight * price * 0.5 ); } } }
Market.java
package strategy; /** * * @author Andy * */ public class Market { /** * 对可打折的一类事物进行打折 * @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) { // 只能对苹果打折 还不能对通用的一类事物打折 而且都是要卖什么就写什么打折算法 // 其实每类事物打折算法又是不一致的 Discountable d = new Apple(10.3, 3.6); Discountable d1= new Banana(5.4,1.1); Market.discountSell(d); Market.discountSell(d1); } }
For more articles related to the Strategy mode of Java design patterns, please pay attention to PHP Chinese net!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
