I'm working on a library that can represent LED strips virtually. My main class, LEDStrip
, is abstract and has a couple abstract methods, like getLength()
and setColor(int index, Color c)
. This lets whoever implements the class focus on just a few simple methods and let the parent class do all of the processing (such as applying patterns or "joining" two strips virtually).
My question is how it would be best to create these "synthetic" LEDStrip objects. There are a few ways that I can think of, but I'm not sure which is considered to be best practice:
Method 1: Separate class and constructor
public class JoinedLEDStrip extends LEDStrip {
private LEDStrip strip1;
private LEDStrip strip2;
public JoinedLEDStrip(LEDStrip s1, LEDStrip s2) {
strip1 = s1;
strip2 = s2;
}
@Override
protected void setLED(int LEDNum, Color c) {
//Select which LEDStrip to set using length of first and second strips and apply offset
}
@Override
public int getNumLEDs() {
return strip1.getNumLEDs() + strip2.getNumLEDs();
}
}
This would simply have a separate class that implements the abstract methods of the LEDStrip class. It's pretty simple but it seems like it might be a bit wordy to use in practice.
LEDStrip joined = new JoinedLEDStrip(firstStrip, secondStrip);
Method 2: "Instance" factory
//LEDStrip.java
public final LEDStrip join(LEDStrip strip) {
return new LEDStrip() {
private LEDStrip strip1 = LEDStrip.this;
private LEDStrip strip2 = strip;
@Override
protected void setLED(int LEDNum, Color c) {
//Select which LEDStrip to set using length of first and second strips and apply offset
}
@Override
public int getNumLEDs() {
return strip1.getNumLEDs() + strip2.getNumLEDs();
}
};
}
This method seems pretty simple, though the LEDStrip class might get a bit crowded, especially if there are more than a few different synthetic versions. It also won't be especially clear to a user of the library how they can create new transformations.
LEDStrip joined = firstStrip.join(secondStrip);
Method 3: Something else?
Maybe there is another method that I haven't thought of. A static factory seems like it wouldn't be especially helpful, but I'm sure Java has some other methods that would work better.
Which way is considered to be the most accepted practice?
LEDStrip
s?LEDStrip final = firstStrip.join(secondStrip).join(thirdStrip)
. This is just an example that I made in 10 minutes, the actual version may accept an array or list of LEDStrips and decide based on that.new JoinedLEDStrip(strip1,new JoinedLEDStrip(strip2, strip3))
. I'm also planning on having invert, split, etc.