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.