Some class objects may have certain behavior, but others do not

How can I create this specific situation.

The basic outline of my problem.

Problem:

  • if I implement Swim in Bird and add the diveNow () behavior for "fly high", but this is undesirable since all birds cannot fly.

In other words: "Some objects can do this, but some cannot and both belong to the same class.

Can I use a strategy template. How? What if I have a huge set of behavior [Interfaces] having the same problem.

another case (if I redefine flyNow () in Animal but, of course, some animals can fly (SUMMER MOBULAS, SUMMER LEMURA.)

+4
source share
4 answers

, Fly :

class Bird {}

, , :

class FlyingBird extends Bird implements Fly {}

- , . ( , - , ):

class Duck extends Bird implements Fly, Swim {}
class Duck extends FlyingBird implements Swim {}

- , , :

class Penguin extends Bird implements Swim {}

, (AFAIK):

class Ostrich extends Bird {}
+2

, , , , flyNow() diveNow():

Bird divingCapableBird = new Bird() {
    void flyNow() {

    }

    void diveNow() {

    }
};

, , divingCapableBird instanceof Swim, false, divingCapableBird Swim.

, , Swim extends Bird, :

class DivingCapableBird extends Bird implements Swim {
    //methods here
}

- , divingCapableBird Animal.

, Animal Swim Fly.

Animal (abstract), Animal.

, , :

interface Swim {
    void diveNow();
}

interface Fly {
    void flyNow();
}

abstract class Animal {
   //some common animal features go here
}

class Bird implements Fly {
    //implement methods
}

class DivingCapableBird extends Bird implement Swim {
    //implement methods
}
+1

:

interface Animal {}

class Bird implements Animal {}

abstract class AbstractBehavior {
   protected final Animal animal;
   Behavior(Animal animal) {
      this.animal = animal;
   }
}

class SwimImpl extends AbstractBehavior implements Swim {
   SwimImpl(Animal animal) {
      super(animal);
   }
   void swim() { System.out.println(animal + " is swimming."); }
}

class FlyImpl implements Fly {
   FlyImpl(Animal animal) {
      super(animal);
   }
   void fly() { System.out.println(animal + " is flying."); }
}

:

class SwimFly implements Swim, Fly {
   private Swim swim;
   private Fly fly;

   SwimFly(Animal animal) {
      this.swim = new SwimImpl(animal);
      this.fly= new FlyImpl(animal);
   }

   void swim() { swim.swim(); }
   void fly() { fly.fly(); }
}

Bird duck = new Bird();
SwimFly duckBehavior = new SwimFly(duck);
duckBehavior.swim();
duckBehavior.fly();

Bird penguin = new Bird();
Swim penguinBehavior = new SwimImpl(penguin);
penguinBehavior.swim();

, Bridge , .

:

Bridge Design

+1

You can go and use some kind of composition. If you share these behaviors in the respective classes, you can freely switch between them even between members of the same class.

This is also related to your strategy template. You might have something like this:

public interface Behaviour {

    void act();

}

public class FlightBehaviour implements Behaviour {

    public void act() {
      System.out.println("I am flying");
    }

}

public class FlightlessBehaviour implements Behaviour {

    public void act() {
        System.out.println("I don't feel like flying, although I have wings");
    }

}

You can then create as many types of behavior as possible in your hierarchy of Animal classes. It can get quite complicated with animals with multiple behaviors and the like, but if you plan it carefully, I think you can use it.

0
source

All Articles