Skip to content

Commit 3538a51

Browse files
committed
swim and fly and one object both can fly and Swim ...how to implement
1 parent 182f10b commit 3538a51

8 files changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.design.strategy.flyrun;
2+
3+
public interface MotionType {
4+
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
 (0)