What is a good data structure to represent a more complex field than an enumeration, but not quite a class?

I am writing a program related to rowing and I am creating an immutable class called "BoatType" to represent the various "types" of boats that are. This becomes complicated because there are many types that require a unique description.

A BoatType must have the following:

  • Either type SCULL or SWEEP
  • A number that matches the number of people who fit into the boat
  • Type PORT or STARBOARD, but only if the first type is SWEEP
  • Boolean representing COXED or UNCOXED, usually only for SWEEP

This is further complicated by the fact that only certain combinations of these fields exist in the rowing world. I want my program to have easy access to these standard types without the need to create new BoatType objects. This, in full:

Scully 8
SCULL 4
SCULL 2
SCULL 1
SWEEP 8 COXED PORT
SWEEP 8 COXED STARBOARD
SWEEP 4 COXED PORT
SWEEP 4 UNCOXED PORT
SWEEP 4 COXED STARBOARD
SWEEP 4 STARBOARD UNCOXED
SWEEP 2 COXED PORT
SWEEP 2 UNCOXED PORT
SWEEP 2 COXED STARBOARD
SWEEP 2 STARBOARD UNCOXED

This is currently a class that I wrote in Java:

public class BoatType
{
public enum RiggerType{SCULL, SWEEP_PORT, SWEEP_STARBOARD}

private int numSeats;

private RiggerType riggerType;

public boolean coxswain = true;

public BoatType(RiggerType type, int seats, boolean coxed)
{
    numSeats = seats;
    riggerType = type;
    coxswain = coxed;
}
}

listing standard types elsewhere:

    public static final BoatType 
    SCULL_OCTUPLE = new BoatType(RiggerType.SCULL, 8, false),
    SCULL_QUAD = new BoatType(RiggerType.SCULL, 4, false),
    SCULL_DOUBLE = new BoatType(RiggerType.SCULL, 2, false),
    SCULL_SINGLE = new BoatType(RiggerType.SCULL, 1, false),
    SWEEP_PORT_EIGHT_COXED  = new BoatType(RiggerType.SWEEP_PORT, 8, true),
    SWEEP_STARBOARD_EIGHT_COXED = new BoatType(RiggerType.SWEEP_STARBOARD, 8, true),
    SWEEP_PORT_FOUR_COXED = new BoatType(RiggerType.SWEEP_PORT, 4, true),
    SWEEP_PORT_FOUR_UNCOXED = new BoatType(RiggerType.SWEEP_PORT, 4, false),
    SWEEP_STARBOARD_FOUR_COXED = new BoatType(RiggerType.SWEEP_STARBOARD, 4, true),
    SWEEP_STARBOARD_FOUR_UNCOXED = new BoatType(RiggerType.SWEEP_STARBOARD, 4, false),
    SWEEP_PORT_PAIR_COXED = new BoatType(RiggerType.SWEEP_PORT, 2, true),
    SWEEP_PORT_PAIR_UNCOXED = new BoatType(RiggerType.SWEEP_PORT, 2, false),
    SWEEP_STARBOARD_PAIR_COXED = new BoatType(RiggerType.SWEEP_STARBOARD, 2, true),
    SWEEP_STARBOARD_PAIR_UNCOXED = new BoatType(RiggerType.SWEEP_STARBOARD, 2, false);

, , - , . , , , .

+3
4

BoatType BoatType, ScullBoatType SweepBoatType. "isPort" "isCoxed" SweepBoatType ( ScullBoatType). :

abstract class BoatType {
    protected int numSeats;

    public BoatType(int numSeats) {
            this.numSeats = numSeats;
    }
}

final class ScullBoatType extends BoatType {
    public ScullBoatType(int numSeats) {
        super(numSeats);
    }
}

final class SweepBoatType extends BoatType {
    private boolean isPort;
    private boolean isCoxed;

    public SweepBoatType(int numSeats, boolean isPort, boolean isCoxed) {
        super(numSeats);
        this.isPort = isPort;
        this.isCoxed = isCoxed;
    }
}

:

BoatType SCULL_OCTUPLE = new ScullBoatType(8);
BoatType SWEEP_PORT_EIGHT_COXED = new SweepBoatType(8, true, true);
// and so on...
+3

, , , :

  • factory, .

  • , .

  • (toString, hashCode, equals, getters).

  • private (coxswain is not) final.

, , , Java. IDE, Eclipse, , .

+4

I don’t see how cumbersome it all is. This is what it is, and I don’t think you will find a much cleaner way.

In one note, you can have constant visibility for members BoatTypeand make all of these members final.

+1
source

Source: https://habr.com/ru/post/1791800/


All Articles