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

AI Hentai Generator
Generate AI Hentai for free.

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

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive
