File tree Expand file tree Collapse file tree
DesignPatternByExample/src/com/design/strategy/flyrun Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .design .strategy .flyrun ;
2+
3+ public abstract class Animal {
4+
5+ protected String name ;
6+ protected Fly flyType ;
7+ protected Run runType ;
8+
9+ public String getName () {
10+ return name ;
11+ }
12+
13+ public void setName (String name ) {
14+ this .name = name ;
15+ }
16+
17+ public Fly getFlyType () {
18+ return flyType ;
19+ }
20+
21+ public void setFlyType (Fly flyType ) {
22+ this .flyType = flyType ;
23+ }
24+
25+ public Run getRunType () {
26+ return runType ;
27+ }
28+
29+ public void setRunType (Run runType ) {
30+ this .runType = runType ;
31+ }
32+
33+ public abstract void motionType ();
34+ }
Original file line number Diff line number Diff line change 1+ package com .design .strategy .flyrun ;
2+
3+ public class Bird extends Animal {
4+
5+ public Bird (String birdName ) {
6+ super ();
7+ this .setName (birdName );
8+ this .setFlyType (new CanFly ());
9+ this .setRunType (new CanNotRun ());
10+ // TODO Auto-generated constructor stub
11+ }
12+
13+ @ Override
14+ public void motionType () {
15+ // TODO Auto-generated method stub"
16+ System .out .println ("I AM " + this .getName () +"-------" );
17+ this .getFlyType ().fly ();
18+ this .getRunType ().run ();
19+ }
20+
21+ }
Original file line number Diff line number Diff line change 1+ package com .design .strategy .flyrun ;
2+
3+ public class CircusManger {
4+
5+ public static void main (String [] arg ){
6+ Animal [] animalOnStage = new Animal [3 ];
7+ animalOnStage [0 ]= new Dog ("Luckky---the Dog----" );
8+ animalOnStage [1 ]= new Bird ("Twidder--Sweet Singer" );
9+ animalOnStage [2 ]= new Penguine ("Penguine of Maddaguskar" );
10+
11+ for (Animal ani : animalOnStage ){
12+ ani .motionType ();
13+ }
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .design .strategy .flyrun ;
2+
3+ public class Dog extends Animal {
4+
5+ public Dog (String dogName ) {
6+ super ();
7+ this .setName (dogName );
8+ this .setFlyType (new CanNotFly ());
9+ this .setRunType (new CanRun ());
10+ // TODO Auto-generated constructor stub
11+ }
12+
13+ @ Override
14+ public void motionType () {
15+ // TODO Auto-generated method stub
16+ System .out .println ("I AM " + this .getName () +"-------" );
17+
18+ this .getFlyType ().fly ();
19+ this .getRunType ().run ();
20+ }
21+
22+ }
Original file line number Diff line number Diff line change 1+ package com .design .strategy .flyrun ;
2+
3+ public interface Fly {
4+
5+ public void fly ();
6+
7+ }
8+
9+ class CanFly implements Fly {
10+
11+ @ Override
12+ public void fly () {
13+ // TODO Auto-generated method stub
14+ System .out .println ("Flying High........." );
15+ }
16+
17+ }
18+
19+ class CanNotFly implements Fly {
20+
21+ @ Override
22+ public void fly () {
23+ // TODO Auto-generated method stub
24+ System .out .println ("Not Able to Fly........." );
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1+ package com .design .strategy .flyrun ;
2+
3+ public interface MotionType {
4+
5+ }
Original file line number Diff line number Diff line change 1+ package com .design .strategy .flyrun ;
2+
3+ public class Penguine extends Animal {
4+
5+ public Penguine (String PenguineName ) {
6+ super ();
7+ this .setName (PenguineName );
8+ this .setFlyType (new CanFly ());
9+ this .setRunType (new CanRun ());
10+ // TODO Auto-generated constructor stub
11+ }
12+
13+ @ Override
14+ public void motionType () {
15+ // TODO Auto-generated method stub"
16+ System .out .println ("I AM " + this .getName () +"-------" );
17+ this .getFlyType ().fly ();
18+ this .getRunType ().run ();
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .design .strategy .flyrun ;
2+
3+ public interface Run {
4+
5+ public void run ();
6+ }
7+
8+ class CanRun implements Run {
9+
10+ @ Override
11+ public void run () {
12+ // TODO Auto-generated method stub
13+ System .out .println ("Running in Supper Speed...." );
14+ }
15+
16+ }
17+
18+ class CanNotRun implements Run {
19+
20+ @ Override
21+ public void run () {
22+ // TODO Auto-generated method stub
23+ System .out .println ("Can't Run like others" );
24+ }
25+
26+ }
You can’t perform that action at this time.
0 commit comments