The strategy mode of Java design pattern

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.