質問

I have a Java assignment in which my professor requires me to use a LeJOS NXT to make a robot that simulates a certain animal's behaviors. I chose to develop a dragon. All the possible behaviors that I've come up so far is:

  • Turning around if it's too close to an obstacle.
  • Going to sleep when battery is low.
  • Pushing an object if touches.
  • If it's too bright, find a dark spot.
  • etc.

I'm now quite confused because I don't know whether to develop it sequentially in one class or to split all the dragon's behaviors into different classes. Please have a look at my explanation below.

Instead of writing everything inside one class like this:

Dragon.java

public class Dragon {
   LightSensor ls = new LightSensor
   public static main(String args[]) {
       while (!BUTTON.Escape.IsPressed()) {
           if (this.closeToObject()) {
               this.turnAround();
           }

           // more conditions
       }
   }
   private boolean closeToObject() {
       //TODO
       return false;
   }
   private void turnAround() {
       //TODO
   }

   //... more methods
}

However, I want to make it appears to be more object-oriented as the course is meant to help us gain more OOP skills. So what my second option is to create action classes that extends Dragon's Behavior abstract class like this (roughly):

Dragon.java

public class Dragon {
   Detect detect = new Detect();  // carry all the detection methods: distance, sound, etc.
   TurnAround turnAround = new TurnAround();
   public static main(String args[]) {
       while (!BUTTON.Escape.IsPressed()) {
           if (detect.tooCloseToObject()) {
               turnAround.prepare(); // beep beep alert
               turnAround.setDerection(true); // e.g. turn right
               turnAround.turn();
           }
       }
   }
}

DragonBehaviors.java

abstract class DragonBehavior {
    abstract void prepare();
    public void setDirection(boolean direction) {
        //...
    }
}

TurnAround.java

public class TurnAround extends DragonBehaviors {
    String direction;
    public void TurnAround() {}
    public void prepare() {
        // sound alert
    }
    public void setDirection(boolean direction) {
        if (direction) this.direction = "Right";
        else this.direction = "Left";
    }
    public void turn() {
        // TODO
    }
}

The code above is roughly a draft, don't focus on it. Eventually, I want to ask if my idea about the OO structure above is reasonable, otherwise it's much easier to develop the whole thing in one class, but it has nothing to do with OOP. I also have several group members to make the code finished, so I think it could be better if we share the classes to develop in an OOP way.

Which way should I go in this circumstance?

I appreciate all the comments (:

役に立ちましたか?

解決

Your choice of extracting different actions into classes with common super class is IMHO reasonable. However I would make Dragon class only aware of the DragonBehavior abstract class, not the subclasses. This way you can add and remove behaviours to the dragon without actually changing it.

How? Look at Chain-of-responsibility pattern - each behaviour has its place in the chain. If behaviour decides to activate itself (i.e. perform something) it may or may not allow further behaviours to be triggered. Moreover, you can and remove behaviours (even at runtime!) and rearrange them to change the precedence (is pushing the obstacle more or less important than going to sleep?).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top