我应该使用什么面向对象结构来描述动物的行为?
我有一项 Java 作业,其中我的教授要求我使用 LeJOS NXT 来制作一个模拟某种动物行为的机器人。我选择开发一条龙。到目前为止,我提出的所有可能的行为是:
- 如果距离障碍物太近,则转身。
- 电池电量低时进入睡眠状态。
- 如果接触到物体则推动。
- 如果太亮,就找一个暗点。
- 我现在很困惑,因为我不知道是否要在一个类中顺序开发它,或者将所有
龙的行为分成不同的类。请看我下面的解释。
而不是像这样在一个类中编写所有内容:
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
}
但是,我想让它看起来更加面向对象,因为该课程旨在帮助我们获得更多 OOP 技能。所以我的第二个选择是创建扩展 Dragon 的行为抽象类的操作类(大致):
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
}
}
上面的代码大致是一个草稿,不要关注它。最后,我想问一下我对上面的OO结构的想法是否合理,否则在一个类中开发整个东西会容易得多,但它与OOP无关。我还有几个小组成员来完成代码,所以我认为如果我们共享类以 OOP 方式进行开发可能会更好。
在这种情况下我该走哪条路呢?
我感谢所有评论(:
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 (:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您选择将不同的操作提取到具有公共超类的类中是恕我直言合理的。但是,我会让
Dragon
类仅了解DragonBehavior
抽象类,不子类。通过这种方式,您可以添加和删除龙的行为,而无需实际更改它。如何?查看责任链模式 - 每个行为在链中都有其位置。如果行为决定激活自身(即执行某些操作),它可能会或可能不允许触发进一步的行为。此外,您可以删除行为(即使在运行时!)并重新排列它们以更改优先级(推动障碍比睡觉更重要还是更不重要?)。
Your choice of extracting different actions into classes with common super class is IMHO reasonable. However I would make
Dragon
class only aware of theDragonBehavior
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?).