Tag: Customer ext void replaces OWA class Mini with Ace
Policy mode
Simulated Duck Game Simuduck
Design principles: Find out what needs to change in your application, separate them, and don't need to mix with the code that changes
Design principles: Programming for interfaces, not for implementation
Design principle: Multi-use combination, less inheritance
The policy pattern definition: defines the algorithm family, encapsulates separately, allows them to replace each other, this pattern lets the algorithm change independently from the customer using the algorithm
1 Public classMiniducksimulator2 {3 Public Static voidMain (string[] args)4 {5 //Green-headed Duck6Duck Mallard =NewMallardduck ();7 Mallard.performquack ();8 mallard.performfly ();9 TenSystem.out.println ("----------------"); One A - //Model Duck -Duck model =NewModelduck (); the model.performfly (); -Model.setflybehavior (Newflyrocketpowered ()); - model.performfly (); - } + } - //Duck Abstract class + Abstract classduck{ A Flybehavior Flybehavior; at Quackbehavior Quackbehavior; - PublicDuck () { - - - } - in Public Abstract voiddisplay (); - //there's more . to + Public voidPerformfly () { - flybehavior.fly (); the } * $ Public voidPerformquack () {Panax Notoginseng Quackbehavior.quack (); - } the + Public voidswim () { ASystem.out.println ("All ducks float, even decoys!"); the } + - Public voidSetflybehavior (Flybehavior fb) { $Flybehavior =FB; $ } - - Public voidSetquackbehavior (Quackbehavior QB) { theQuackbehavior =QB; - } Wuyi } the //Flight Behavior - InterfaceFlybehavior { Wu Public voidfly (); - } About //fly with your wings $ classFlywithwingsImplementsFlybehavior { - Public voidFly () { -System.out.println ("I ' m flying!!"); - } A } + //can't fly the classFlynowayImplementsFlybehavior { - Public voidFly () { $System.out.println ("I can ' t fly.")); the } the } the //Rocket-powered the classFlyrocketpoweredImplementsFlybehavior { - Public voidFly () { inSystem.out.println ("I ' m flying with a rocket!"); the } the } About the //Call Behavior the InterfaceQuackbehavior { the Public voidQuack (); + } - // the classQuackImplementsQuackbehavior {Bayi Public voidQuack () { theSYSTEM.OUT.PRINTLN ("Quack"); the } - } - //won't call the classMutequackImplementsQuackbehavior { the Public voidQuack () { theSystem.out.println ("<< Silence >>"); the } - } the // the classSqueakImplementsQuackbehavior { the Public voidQuack () {94System.out.println ("Squeak"); the } the } the //Green-headed Duck98 classMallardduckextendsDuck { About PublicMallardduck () { -Quackbehavior =NewQuack ();101Flybehavior =Newflywithwings ();102 }103 104 Public voiddisplay () { theSystem.out.println ("I ' m a real mallad duck");106 }107 }108 109 //Model Duck the classModelduckextendsDuck {111 PublicModelduck () { theFlybehavior =NewFlynoway ();113Quackbehavior =NewQuack (); the } the the Public voiddisplay () {117System.out.println ("I ' m a model duck");118 }119}
Similar: Each character can use only one weapon at a time, but it is possible to change weapons during the game.
The strategy mode of Java design pattern