Java Design Pattern 1 -- stratrgy

Source: Internet
Author: User

 Package CN. hnu. WY. strategy; </P> <p> Public interface goalgorithm <br/> {<br/> Public void go (); <br/>}</P> <p> package CN. hnu. WY. strategy; </P> <p> public class gobydrivingalgorithm implements goalgorithm <br/>{< br/> Public void go () <br/>{< br/> system. out. println ("Now I'm driving. "); <br/>}</P> <p> package CN. hnu. WY. strategy; </P> <p> public class gobyflyingalgorithm implements goalgorithm <br/>{< br/> Public void go () {<br/> system. out. println ("Now I'm flying. "); <br/>}</P> <p> package CN. hnu. WY. strategy; </P> <p> public class gobyflyingfastalgorithm implements goalgorithm <br/>{< br/> Public void go () <br/>{< br/> system. out. println ("Now I'm flying fast. "); <br/>}</P> <p> package CN. hnu. WY. strategy; </P> <p> public abstract class vehicle <br/> {<br/> private goalgorithm; </P> <p> public vehicle () <br/>{< br/>}</P> <p> Public void setgoalgorithm (goalgorithm algorithm) <br/>{< br/> goalgorithm = algorithm; <br/>}</P> <p> Public void go () {<br/> goalgorithm. go (); <br/>}</P> <p> package CN. hnu. WY. strategy; </P> <p> public class jet extends vehicle <br/> {<br/> Public jet () <br/>{< br/> setgoalgorithm (New gobyflyingfastalgorithm (); <br/>}</P> <p> package CN. hnu. WY. strategy; </P> <p> public class realjet <br/>{</P> <p> Public static void main (string [] ARGs) <br/>{< br/> jet = new jet (); </P> <p> jet. setgoalgorithm (New gobydrivingalgorithm (); <br/> jet. go (); </P> <p> jet. setgoalgorithm (New gobyflyingfastalgorithm (); <br/> jet. go (); </P> <p> jet. setgoalgorithm (New gobyflyingalgorithm (); <br/> jet. go (); <br/>}</P> <p>

 

Running result:

Now I'm driving.
Now I'm flying fast.
Now I'm flying.

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.