• 面向对象三大特征是封装、继承、多态,多态是面向对象的灵魂
  • 实例方法默认是多态的,在运行时根据this的类型来决定调用哪个方法(和代码在哪个类无关)
  • 静态方法没有多态
  • 参数静态绑定(方法参数重载是静态选择的,不发生在参数选择上),接收者动态绑定(多态仅对接收对象的类型生效)

    策略模式

  • 分离策略逻辑与业务逻辑,每次增加策略可通过添加类来实现,而不是复杂化原先的业务逻辑

  • 下面为使用策略模式进行代码重构的案例,重构指的是在不改变原先代码功能的前提下,将原先的代码变得更精炼、可维护性更强 ```java // 测试类 import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test;

public class PriceCalculatorTest { @Test public void test() { Assertions.assertEquals(95, PriceCalculator.calculatePrice(new Discount95Strategy(), 100, User.dios(“屌丝”))); Assertions.assertEquals(100,PriceCalculator.calculatePrice(new OnlyVipDiscountStrategy(), 100, User.dios(“屌丝”))); Assertions.assertEquals(95, PriceCalculator.calculatePrice(new OnlyVipDiscountStrategy(), 100, User.vip(“土豪”))); Assertions.assertEquals(100, PriceCalculator.calculatePrice(new NoDiscountStrategy(), 100, User.vip(“土豪”))); } }

  1. ```java
  2. // 工具类User
  3. public class User {
  4. private String name;
  5. private boolean vip;
  6. private User(String name, boolean vip) {
  7. this.name = name;
  8. this.vip = vip;
  9. }
  10. public static User vip(String name) {
  11. return new User(name, true);
  12. }
  13. public static User dios(String name) {
  14. return new User(name, false);
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public boolean isVip() {
  20. return vip;
  21. }
  22. }
  1. public class PriceCalculator {
  2. // 使用策略模式重构这个方法,实现三个策略:
  3. // NoDiscountStrategy 不打折
  4. // Discount95Strategy 全场95折
  5. // OnlyVipDiscountStrategy 只有VIP打95折,其他人保持原价
  6. public static int calculatePrice(String discountStrategy, int price, User user) {
  7. switch (discountStrategy) {
  8. case "NoDiscount":
  9. return price;
  10. case "Discount95":
  11. return (int) (price * 0.95);
  12. case "OnlyVip":
  13. {
  14. if (user.isVip()) {
  15. return (int) (price * 0.95);
  16. } else {
  17. return price;
  18. }
  19. }
  20. default:
  21. throw new IllegalStateException("Should not be here!");
  22. }
  23. }
  24. // ---------------------------重构后 -----------------------------
  25. public static int calculatePrice(DiscountStrategy strategy, int price, User user) {
  26. return strategy.discount(price,user);
  27. }
  28. }
  1. public class DiscountStrategy {
  2. public int discount(int price, User user) {
  3. throw new UnsupportedOperationException();
  4. }
  5. }
  1. public class NoDiscountStrategy extends DiscountStrategy {
  2. @Override
  3. public int discount(int price, User user) {
  4. return price;
  5. }
  6. }
  1. public class Discount95Strategy extends DiscountStrategy {
  2. @Override
  3. public int discount(int price, User user) {
  4. return (int) (price * 0.95);
  5. }
  6. }
  1. public class OnlyVipDiscountStrategy extends DiscountStrategy {
  2. @Override
  3. public int discount(int price, User user) {
  4. if (user.isVip()) {
  5. return (int) (price * 0.95);
  6. } else {
  7. return price;
  8. }
  9. }
  10. }

线程池TheadPoolExecutor

  • 线程池是策略模式在JDK中的使用,线程池代码逻辑与策略逻辑完全分离,实现代码解耦