Java Strategy Pattern and strategypattern

Source: Internet
Author: User

Java Strategy Pattern and strategypattern

I recently read Head First design patterns, a good book. Easy to understand.

Rule Mode

Defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This pattern makes the algorithm changes independent of the customers who use the algorithm.

Code:

Package gx. customer; import gx. behavior. flyBehavior; import gx. behavior. quackBehavior;/***** the Duck class is the super class of all ducks, but the flight behavior and cronning behavior have been extracted (changed behavior) * @ author always **/public abstract class Duck {protected FlyBehavior flyBehavior; protected QuackBehavior quackBehavior; public void swim () {System. out. println ("It's nothing for ducks to swim back.");} public void synchronized mfly () {if (flyBehavior! = Null) {flyBehavior. fly () ;}else {System. out. println ("no flight behavior") ;}} public void extends mquack () {if (quackBehavior! = Null) {quackBehavior. quack ();} else {System. out. println ("No behavior");} public void setFlyBehavior (FlyBehavior flyBehavior) {this. flyBehavior = flyBehavior;} public void setQuackBehavior (QuackBehavior quackBehavior) {this. quackBehavior = quackBehavior;} public abstract void display ();}
Package gx. behavior;/*** flight behavior interface * @ author always **/public interface FlyBehavior {void fly ();}
Package gx. behavior;/*** interface for calling behavior ** @ author always **/public interface QuackBehavior {void quack ();}

Behavior implementation: algorithm family

Package gx. behavior. impl; import gx. behavior. flyBehavior; public class FlyNoWay implements FlyBehavior {public void fly () {System. out. println ("I haven't learned it yet. Great difficulty ");}}
Package gx. behavior. impl; import gx. behavior. flyBehavior; public class FlyWithWings implements FlyBehavior {public void fly () {System. out. println ("fly with wings, hard ");}}
Package gx. behavior. impl; import gx. behavior. QuackBehavior; public class MuteQuack implements QuackBehavior {public void quack () {System. out. println ("not called ");}}
Package gx. behavior. impl; import gx. behavior. QuackBehavior; public class Quack implements QuackBehavior {public void quack () {System. out. println (" ");}}
Package gx. behavior. impl; import gx. behavior. QuackBehavior; public class Squack implements QuackBehavior {public void quack () {System. out. println (" ");}}
Package gx. customer; import gx. behavior. impl. flyWithWings; import gx. behavior. impl. quack; public class MallardDuck extends Duck {public MallardDuck () {flyBehavior = new FlyWithWings (); quackBehavior = new Quack ();} public void display () {System. out. println ("looks like a green duck ");}}

The customer uses the encapsulated flight and crouch algorithm family.

Package gx. customer; import gx. behavior. impl. flyNoWay; import gx. behavior. impl. muteQuack; public class RedheadDuck extends Duck {public RedheadDuck () {flyBehavior = new FlyNoWay (); quackBehavior = new MuteQuack () ;}@ Override public void display () {System. out. println ("braised chicken ");}}

Test class:

Package gx. test; import org. junit. test; import gx. behavior. impl. flyWithWings; import gx. customer. duck; import gx. customer. mallardDuck; import gx. customer. redheadDuck; public class TestStrategy {private Duck duck; @ Test public void testMallardDuck () {duck = new MallardDuck (); duck. display (); duck. optional mfly (); duck. explain mquack ();/* Result: It looks like a green-headed duck with wings. The fly is awesome */} @ Test public void testRedheadDuck () {duck = n Ew RedheadDuck (); duck. display (); duck. optional mfly (); duck. optional mquack (); duck. setFlyBehavior (new FlyWithWings (); // dynamically call the new "algorithm" duck. performFly ();/* result: the red-headed chicken has started learning. Difficult to use wings, Fly hard */}}

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.