A "Strategy model" of Head first design mode (the optimization course of duck design)

Source: Internet
Author: User

 Public Abstract classDuck {Flybehavior flybehavior; Quackbehavior Quackbehavior; PublicDuck () {} Public Abstract voiddisplay (); Public voidPerformfly () {flybehavior.fly ();} Public voidPerformquack () {quackbehavior.quack ();} Public voidswim () {System.out.println ("All Ducksfloat, even decoys!");}
 Public void  = fb;}  Public void  = QB;}
}
 Public InterfaceFlybehavior { Public voidfly ();} Public classFlywithwingsImplementsFlybehavior { Public voidFly () {System.out.println ("I ' m flying!!");}} Public classFlynowayImplementsFlybehavior { Public voidFly () {System.out.println ("I can ' t Fly");}}
 Public class Implements Flybehavior {publicvoid  Fly () {System.out.println ("I ' m flying with a rocket!< c12> ");}}

 Public InterfaceQuackbehavior { Public voidquack ();} Public classQuackImplementsQuackbehavior { Public voidQuack () {System.out.println ("quack");}} Public classMutequackImplementsQuackbehavior { Public voidQuack () {System.out.println ("<< Silence >>");}} Public classSqueakImplementsQuackbehavior { Public voidQuack () {System.out.println ("Squeak");}}


 Public class extends Duck {public newnew  Quack ();}  Public void display () {System.out.println ("I ' m A model Duck");}}

 Public class miniducksimulator {publicstaticvoidnew  Mallardduck (); Mallard.performquack (); Mallard.performfly ();
New Modelduck (); Model.performfly (); Model.setflybehavior (new  flyrocketpowered ()); Model.performfly ();

}}

Policy mode:

The algorithm family is defined, packaged separately, so that they can be replaced by each other, and this pattern allows the algorithm to change independently of the client using the algorithm.

[strategy mode of]head first design mode] (Duck design optimization process)

Related Article

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.