Polymorphism,

  • A method takes on many forms
  • Instance methods (which are instantiated without static) are polymorphic \ by default

  • Extended to real life, in real life, the plane flies, the plane starts the engine to fly, tell the bird to fly, the bird will wave its wings to fly
  • In real life, depending on the message, the recipient will decide how to respond to the message based on who they are. Right
  • Static methods have no polymorphism
  • The receiver is dynamically bound and the parameter is statically bound to select the type that needs least conversion
  • In other words, polymorphism is only valid for the receiver of the message; polymorphism selects only the type of the receiver and not the type of the parameter

Polymorphic combat: strategic mode

  • Never use a double for sums, which means that a sum of $1 *100 becomes 100 points, and another option is bigDecimal
  • It was a long list of code
package com.github.hcsp; public class PriceCalculator { public int calculate(String strategy, int price, User user) { switch (strategy) { case "NoDiscount": return price; Case "95": return (int) (price * 0.95); Case "VIP": if (user.isvip ()) {return (int) (price * 0.95); } { return price; } default: throw new IllegalStateException(); }}}Copy the code
  • A strategy pattern is a strategy

    • PriceCalculator.java
package com.github.hcsp; public class PriceCalculator { public int calculate(DiscountStrategy strategy, int price, User user) { return strategy.discount(price, user); } // public int calculate(String strategy, int price, User user) { // switch (strategy) { // case "NoDiscount": // return price; // case "95": // return (int) (price * 0.95); / / case "VIP" : / / if (user) isVip ()) {/ / return (int) (price * 0.95); // } // { // return price; // } // default: // throw new IllegalStateException(); //} //Copy the code
  • DiscountStrategy.java
package com.github.hcsp; public class DiscountStrategy { public int discount(int price, User user) { throw new UnsupportedOperationException(); }}Copy the code
  • NoDiscountStrategy.java
package com.github.hcsp; public class NoDiscountStrategy extends DiscountStrategy{ @Override public int discount(int price, User user) { return price; }}Copy the code
  • The advantage of this design is that the redundant code is broken down and the policy and business are separated through polymorphism, which is easy to scale
package com.github.hcsp; public class PriceCalculator { public static void main(String[] args) { calculate(new NoDiscountStrategy(), 100, User.vip("")); } public static int calculate(DiscountStrategy strategy, int price, User user) { return strategy.discount(price, user); }}Copy the code
  • Summary: For every policy you add, you add a class

Policy schema in the JDK

  • JDK thread pool four public methods, basically interview multithreading must ask, this several parameters what mean, how flexible control thread pool implementation \

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {}
Copy the code
  • RejectedExecutionHandler RejectedExecutionHandler RejectedExecutionHandler RejectedExecutionHandler RejectedExecutionHandler RejectedExecutionHandler RejectedExecutionHandler RejectedExecutionHandler RejectedExecutionHandler

    • There are four strategies through THE JDK source code, the first is to directly abandon, throw the exception, the second is to let the user decide to let the caller decide, the third is to discard the oldest thread, and the fourth is to directly discard the current incoming task

  • Benefits: It can change independently, the thread pool does not need to care about the four policies at all, the original code does not need to change the policy at all, change the thread pool code does not need to change the policy class, achieve independent change, namely decoupling, communication through one interface, through very weak relationship

Polymorphic combat: simplifying and refactoring code using polymorphisms

  • Right-click Diagram to view the inheritance Diagram