1 PackageCom.shejimoshi.behavioral.Strategy;2 3 4 /**5 * Function: interface to work6 * Time: March 9, 2016 PM 8:53:347 * Cutter_point8 */9 Public InterfacetoworkTen { One /** A * How to work - */ - Public Abstract voidWorkStyle (); the -}
1 PackageCom.shejimoshi.behavioral.Strategy;2 3 4 /**5 * Function: walk to work6 * Time: March 9, 2016 PM 8:55:157 * Cutter_point8 */9 Public classWalkingworkImplementstoworkTen { One A @Override - Public voidWorkStyle () - { theSystem.out.println ("Walk To Work"); - } - -}
1 PackageCom.shejimoshi.behavioral.Strategy;2 3 4 /**5 * Function: Use tool to go to work6 * Time: March 9, 2016 PM 8:57:357 * Cutter_point8 */9 Public classTooltoworkImplementstoworkTen { One A @Override - Public voidWorkStyle () - { theSystem.out.println ("Use tool to go to work"); - } - -}
1 PackageCom.shejimoshi.behavioral.Strategy;2 3 4 /**5 * Function: Selection method6 * Time: March 9, 2016 PM 9:15:527 * Cutter_point8 */9 Public classSelectTen { One Privatetowork tw; A - PublicSelect (String type) - { the Switch(type) - { - CaseWalk: -Walkingwork ww =Newwalkingwork (); +TW =ww; - Break; + Case"Using Tools": ATooltowork TTW =Newtooltowork (); atTW =TTW; - Break; - } - } - - /** in * Implement the appropriate policy - */ to Public voidGetResult () + { - Tw.workstyle (); the } *}
1 PackageCom.shejimoshi.behavioral.Strategy;2 3 4 /**5 * Function: Define a series of algorithms, wrap them up one by one, and make them interchangeable with each other. 6 * Applicable: Many related classes are just differences in behavior7 * need to use different variants of an algorithm8 * Algorithms use data that the customer shouldn't know9 * Time: March 9, 2016 PM 8:49:34Ten * Cutter_point One */ A Public classTest - { - Public Static voidMain (string[] args) the { -Select st =NewSelect ("Walking"); - St.getresult (); -Select St2 =NewSelect ("Use Tools"); + St2.getresult (); - } +}
Walk to work using tools to go to work
"Design mode" 21, Policy mode