Optimization
if (type.equal ( "simple") ) {
simple mode SimplepolymorphismService
} the else IF (type.equal ( "Middle")) {
Medium MiddlepolymorphismService
} the else IF (type.equal ( "Hard")) {
complex HardpolymorphismService
}
structures .
1. Use of polymorphic
main codes
SimplePolymorphismService simple = new SimplePolymorphismService(); MiddlePolymorphismService middle = new MiddlePolymorphismService(); HardPolymorphismService hard = new HardPolymorphismService(); map.put("simple", restart); map.put("middle", middle); map.put("hard", hard); PolymorphismService poly = (PolymorphismService) map.get("simple"); try { poly.run(); } catch (Exception e) { e.printStackTrace (); }
Parent code
public interface PolymorphismService { void run() throws Exception; }
Subclasses
class MiddlePolymorphismService implements PolymorphismService { public void run() throws Exception { System.out.println("middlePolymorphismService"); } } class HardPolymorphismService implements PolymorphismService { public void run() throws Exception { System.out.println("hardPolymorphismService"); } }
This method map requires manual placement, add a new policy in use, it may be ignored placement.
2. Enumeration
Performing the method defined in the enumeration class
Enum class code
public enum EnumBetterBetter {
FAST { void run () { System.out.println("FAST enum"); } }, simple { void run () { System.out.println("simple nume"); } } ; abstract void run();
}
main code execution
public static void main(String[] args) { EnumBetterBetter enumBetter = EnumBetterBetter.valueOf("FAST"); enumBetter.run(); EnumBetterBetter enumBetter1 = EnumBetterBetter.valueOf("simple"); enumBetter1.run(); EnumBetterBetter enumBetter3 = EnumBetterBetter.FAST; enumBetter3.run(); EnumBetterBetter enumBetter4 = EnumBetterBetter.simple; enumBetter4.run(); }
Results of the
FAST enum simple name FAST enum simple name
3. Optional
Optional mainly used to determine non-empty, because it is jdk8 new features, so it is not particularly used, but with it is really cool.
Mainly to solve
if(user == null) {
}else {
}
This situation.
Specific code execution
Optional<String> userOptional = Optional.ofNullable(null); userOptional.map(action1).orElse(action2); or use a lambda expression Optional<String> userOptional = Optional.ofNullable(null); userOptional.map(list -> { return null; }).orElse("there have nothing");
4. Array Tips
From google explained, which is a programming model, table-driven method is called, is essentially query information from the table in place of logic statements.
For example, to get the number of days of the month by month.
Code issues
int getDay (int month) { if (month == 1) return 31; if (month == 2) return 29; if (month == 3) return 31; if (month == 4) return 30; if (month == 5) return 31; if (month == 6) return 30; if (month == 7) return 31; if (month == 8) return 31; if (month == 9) return 30; if (month == 10) return 31; if (month == 11) return 30; if (month == 12) return 31; }
Solutions
int monthDay[] = {31,29,31,30,31,30,31,31,30,31,30,31}; int getDays(int month) { return monthDay[--month]; }