首页 > 解决方案 > 我的问题的最佳 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()方法变得非常大。它是一种方法,每秒钟都会被超类循环一次。

更多详情:

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

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

标签: javaoopdesign-patterns

解决方案


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设置它。setRobStateRobEvent

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();
}

我希望它对你有帮助!如果不是这次,也许将来!
玩你的游戏:]


推荐阅读