FRC2706 / 2019-2706-Robot-Code

The main robot code for the FIRST 2019 challenge: Deep Space
MIT License
2 stars 0 forks source link

Problems with current instantiation pattern of subsystems #78

Closed KyleRAnderson closed 5 years ago

KyleRAnderson commented 5 years ago

As we have it currently, the way all subsystems are instantiated is with the following two functions:

public class Subsystem {
   private static Subsystem currentInstance;

    public static void init() {
        if (currentInstance == null) {
            currentInstance = new Subsystem();
        }
    }

    public static Subsystem getInstance() {
        init();
        return currentInstance;
     }
}

The problem with this design is from a testing perspective, you would want to re-initialize subsystems for each test, which would never happen because they won't re-initialize if there is already an instance.

There are two solutions I see to this problem:

  1. Make it so that init() re-initializes a subsystem even if it already exists. The getInstance() method would not initialize the subsystem at all, it would just return currentInstance.
    • This is easy but I don't know if we want anyone to have the power to re-initialize a subsystem.
  2. Create a helper method in tests which resets *everything` before a test.
    • This could be better actually because the way we could do it is by instantiating another robot, which is more accurate.
    • We would need to use reflection extensively however.

Which of these solutions do you prefer @eandr127?

I have already done #1 in the subsystem-factory branch if you want to see the full extent of what would change.