java - 我的问题的最佳 OOP 解决方案是什么?
问题描述
很长一段时间以来,我一直在尝试更好地构建我的代码,并越来越多地专注于在我的代码中使用 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()
方法变得非常大。它是一种方法,每秒钟都会被超类循环一次。
更多详情:
RobEvent
必须知道它当前是否正在产生证券并做特定的事情,- 它必须检查是否所有都生成并做特定的事情,
- 它必须检查玩家是否在完成之前逃跑并做特定的事情,
- 它必须检查玩家是否杀死了所有人并做特定的事情等。
最烦人的是跟踪生成的证券,因为如果他逃跑,它们都必须被消灭。我觉得这个对象中的信息太多了,例如,我可以将生成的证券的跟踪拆分到一个单独的对象中,并在具有特定状态tracker.despawn
时执行类似操作。RaidEvent
编辑:代码只是实际代码的真正简化。
解决方案
RobEvent 必须知道它当前是否正在生成证券并做特定的事情,- 它必须检查是否所有都生成并做特定的事情, - 它必须检查玩家是否在完成之前逃跑并做特定的事情,-它必须检查玩家是否杀死了所有人并做特定的事情等。
这听起来像是将逻辑与状态分离的状态模式。
快速实施
RobEvent
需要跟踪一个RobState
. 你有多个RobState
喜欢StartSpanSecuritiesState
,,AllSecuritiesSpanedState
等等PlayerRunAwayState
..
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
知道下一个并通过ofRobState
设置它。setRobState
RobEvent
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();
}
我希望它对你有帮助!如果不是这次,也许将来!
玩你的游戏:]
推荐阅读
- javascript - 将带偏移的纹理从 Three.js 场景导出到 GLTF
- css - 无法在 CSS 中添加 bgimage
- ios - 在 uitableview 重新加载时显示活动指示器
- node.js - 从 Firebase Cloud Functions 访问 Auth
- c++ - 有没有办法让 dlopen 故意失败
- javascript - IDE 中的 VSCode eslint 错误,但 CLI 中没有
- javascript - 将 .js 文件中的参数传递给控制器
- java - 如何将对象从 thymeleaf 表单发送到 MVC spring boot 控制器
- javascript - 如何在 Python 中通过 Beautifulsoup 获取选定的文本
- jquery - 通过ajax成功下载文件