Xeno Pyax
Xeno Pyax

Reputation: 37

Return multiple classes java

So I'm trying to write an API for an game and i made 4 different classes named ShipScanStage0, ShipScanStage1, ShipScanStage2, ShipScanStage3 which I want to return in this class.

I tried to set the return type of getScanStageInfo to Object but that is not the kinda of solution im looking for I want a clean and simply way for API users to gain access to the scanStage and use the Information of it.

ShipTargetedEvent.java

    package org.xenodev.edj.events;

    import org.xenodev.edj.Event;
    import org.xenodev.edj.events.storage.ShipScanStage0;

    public class ShipTargetedEvent extends Event {

    int scanStage;

    public ShipTargetedEvent(String timestamp, int scanStage) {
        super(timestamp);
        this.scanStage = scanStage;
    }

    public void getScanStageInfo() {
        if(scanStage == 0) {
            return new ShipScanStage0();
        }else if(scanStage == 1) {
            return new ShipScanStage1();
        }else if(scanStage == 2) {
            return new ShipScanStage2();
        }else if(scanStage == 3) {
            return new ShipScanStage3();
        }
     }

    }

I want to achieve that users can access only information from the StageScan level because if the scanStage level is 2 and they access the scanStage level 3 class they are gonna have some uninitialized variables cause the game didnt provide that information.

Upvotes: 0

Views: 68

Answers (2)

PseudoAj
PseudoAj

Reputation: 5967

There are various ways to achieve this, one common way is to use a composite pattern to abstract the implementation and return the root interface. In implementation it would look like:

Define a common interface

public interface ScanStageInfo() {

}

Have different implementations of this based off the stage:

public class ScanStageOneInfo implements ScanStageInfo {

}

public class ScanStageTwoInfo implements ScanStageInfo {

}

public class ScanStageThreeInfo implements ScanStageInfo {

}

public class ScanStageFourInfo implements ScanStageInfo {

}

Return based of the business logic

    public ScanStageInfo getScanStageInfo() {
        if (scanStage == 0) {
            return new ScanStageOneInfo();
        } else if(scanStage == 1) {
            return new ScanStageTwoInfo();
        } else if(scanStage == 2) {
            return new ScanStageThreeInfo();
        } else if(scanStage == 3) {
            return new ScanStageFourInfo();
        }
     }

Upvotes: 1

Makoto
Makoto

Reputation: 106470

If you're saying that each ShipScanStage-type object is similar, then it would have similar-enough functionality.

If this is the case, then each one of those objects should either extend from an abstract class or implement an interface.

...Then, you actually have to return either that abstract type or interface from your method.

Supposing that the abstract class or interface was named ShipScanStage, then the signature of your method would be:

public ShipScanStage getScanStageInfo() {

}

Upvotes: 2

Related Questions