gpt4 book ai didi

java - 我的问题的最佳 OOP 解决方案是什么?

转载 作者:行者123 更新时间:2023-12-02 10:19:39 26 4
gpt4 key购买 nike

现在很长一段时间,我都在尝试更好地构建我的代码,并专注于在我的代码中越来越多地使用 OOP 概念。

我有一个简单的问题,关于以下场景以及如何从设计模式的角度最好地解决它。

我有一个游戏,玩家可以抢劫商店。该商店由 Shop 代表对象和抢劫由 RobEvent 表示。我有一个Spawner对象,当有人试图抢劫商店时,它会处理证券的生成。

我的核心问题是,我有一种感觉,RobEvent对象具有太多的信息和功能,如下所述,我可以将信息和功能拆分为更多对象,为此我需要您的帮助!

public class Main 
{

public static void main(String[] args)
{

}

public class Shop {

private String name;
private Location location;
private boolean isBeingRobbed = false;

/*
* Here one of the problems, should the shop have the
* data of the npcs, which will appear
* in the shop when the player attempts to rob it.
* Should it have methods to place "spawn" positions of
* the securities, the type, the amount etc.
* and store it here?
*
*/

public Shop(String name, Location location){
this.name = name;
this.location = location;
}

public boolean isBeingRobbed(){
return isBeingRobbed;
}

protected void setBeingRobbed(boolean bool){
this.isBeingRobbed = bool;
}
}

public class RobEvent extends Looper {

private Shop shop;

RobEvent(Shop shop){
this.shop = shop;
}

public void start(){
shop.setBeingRobbed(true);
}

@Override
protected void logic(){
/*
* Big chunk of game logic
* Spawning the securities, checking if they
* all appeared(they got a delay of few seconds each),
* check if he ran away before everything
* spawned, check if he killed all, check if all appeared
*/
}

private void completed(){
shop.setBeingRobbed(false);
}
}

public class Spawner {


/* not important things
*
* just has a method to
* handle the spawn of a security
*/

public void spawn(NPC npc){
/*
* spawn the npc aka security
*/
}

}
}

我最大的问题是 logic()方法变得非常大。它是一个方法,每秒由父类(super class)循环一次。

更多细节:

  • RobEvent必须知道它当前是否正在产生证券并执行特定的操作,
  • 它必须检查是否全部生成并执行特定的操作,
  • 它必须在完成之前检查玩家是否逃跑并执行特定的操作,
  • 它必须检查玩家是否杀死了所有人并执行特定的操作等。

最烦人的是跟踪生成的证券,因为如果他逃跑,它们都必须消失。我感觉这个对象中的信息太多了,例如,我可以将生成的证券的跟踪拆分到一个单独的对象中,然后执行类似 tracker.despawn 的操作。当 RaidEvent具有特定的状态。

编辑:代码只是实际代码的真正简化。

最佳答案

The RobEvent has to know if it is currently spawning securities and do specific things, - it has to check if all got spawned and do specific things, - it has to check if the player ran away before it was completed and do specific things, - it has to check if the player killed all and do specific things etc.

这听起来像 State-Pattern它将逻辑与状态解耦。

快速实现

RobEvent 需要跟踪 RobState。您有多个 RobState,例如 StartSpanSecuritiesStateAllSecuritiesSpanedStatePlayerRunAwayState 等。

interface RobState {
void handle();
}

class RobEvent extends Looper implements RobState {

// add new member
private RobState robState;
private Shop shop;

RobEvent(Shop shop) {
this.shop = shop;
this.robState = new StartSpanSecuritiesState();
}

// add new setter - gets normally called only by a `State`
void setRobState(RobState robState) {
this.robState = robState;
}

// ..

}

每个RobState都知道下一个RobState,并通过RobEventsetRobState设置它。

class StartSpanSecuritiesState implements RobState {

private RobEvent robEvent;

public StartSpanSecuritiesState(RobEvent robEvent) {
this.robEvent = robEvent;
}

@Override
public void handle() {
// span your securities
//...

// go to the next state
robEvent.setRobState(new AllSecuritiesSpanedState(robEvent));
}
}

完成所有修改后,logic 方法可能如下所示:

@Override
protected void logic(){
robState.handle();
}
<小时/>

希望对你有帮助!如果这次不是,也许将来也会!
祝您游戏愉快:]

关于java - 我的问题的最佳 OOP 解决方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54431919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com