1 Packagecom.tn. Strategy mode;2 3 Public classClient {4 Privatestrategy strategy;5 Public voidSetstrategy (Strategy strategy) {6 This. strategy=strategy;7 }8 Public voidExealgorithm () {9 strategy.exealgorithm ();Ten } One A Public Static voidMain (String[]args) { -Strategy strategy1=NewConcreteStrategy1 (); -Strategy strategy2=NewConcreteStrategy2 (); theStrategy strategy3=NewConcreteStrategy3 (); - -Client c=NewClient (); - + C.setstrategy (strategy1); - c.exealgorithm (); + A C.setstrategy (strategy2); at c.exealgorithm (); - - C.setstrategy (strategy3); - c.exealgorithm (); - } - } in Interfacestrategy{ - voidexealgorithm (); to } + classConcreteStrategy1Implementsstrategy{ - @Override the Public voidExealgorithm () { *SYSTEM.OUT.PRINTLN ("Execute algorithm 1 ..."); $ }Panax Notoginseng } - classConcreteStrategy2Implementsstrategy{ the @Override + Public voidExealgorithm () { ASYSTEM.OUT.PRINTLN ("Execute algorithm 2 ..."); the } + } - classConcreteStrategy3Implementsstrategy{ $ @Override $ Public voidExealgorithm () { -SYSTEM.OUT.PRINTLN ("Execute algorithm 3 ..."); - } the}
"Java design pattern" "behavior pattern Behavioral pattern" strategy pattern