vendredi 20 octobre 2017

Creating abstraction from the concept of RGB LED Strips containing individual RGB Leds, which each contain 3 individual LEDs

I'm having some problems abstracting my idea into classes and I'm hoping someone can point me in the right direction. I feel like there's some concept I'm missing and it's preventing me from moving further, since I don't have a clue on what to search for.

My current goal is to have various 'LED' objects, and have their states updated on the screen in a GUI. I have not written the GUI yet because I want to get my basic structure down first. While they represent LEDs, I am not trying to physically control the LEDs at this time.

Right now I have 4 types of 'LEDs' I want to use, they are:

1) LED - a single LED with a brightness between 0 and 255 2) RGB LED - a single 'package' LED with separate R/G/B values. This is composed of 3 LEDs (red, green, blue) each with their own brightness value. 3) RGB Strip - multiple RGB leds connected together. This is composed of RGB LEDs. 4) LED Strip - multiple single-LEDs connected together. This is composed of LEDs.

Here are examples of LED, RGB LED, and RGB Strip. (LED Strip is the same as RGB Strip, except an array of single LEDs) They don't have all the features, but just an idea of what my structure currently looks like.

public class Led {
    private int brightness;
    private boolean lit;

    public Led() {
        brightness = 0;
        lit = false;
    }

    public int getBrightness() {
        return brightness;
    }

    public void setBrightness(int bright) {
        if (bright < 0)
            return;
        brightness = bright;
        return;
    }

    public boolean isLit() {
        return lit;
    }

    public void setLit(boolean litUp) {
        lit = litUp;
    }

}


public class RgbLed {
    private Led red;
    private Led green;
    private Led blue;
    private boolean isLit;

    public RgbLed() {
        red = new Led();
        green = new Led();
        blue = new Led();
    }

    public void setLit(boolean turnLit) {
        red.setLit(turnLit);
        green.setLit(turnLit);
        blue.setLit(turnLit);
        isLit = turnLit;
    }

    public boolean isLit() {
        return isLit;
    }

    public void setColor(int r, int g, int b) {
        red.setBrightness(r);
        green.setBrightness(g);
        blue.setBrightness(b);
        return;
    }

    public int getRed() {
        return red.getBrightness();
    }


    public int getGreen() {
        return green.getBrightness();
    }


    public int getBlue() {
        return blue.getBrightness();
    }

}

public class RgbStrip {
    private int numLeds;
    private ArrayList<RgbLed> storage;

    public RgbStrip(int numRgbLeds) {
        numLeds = numRgbLeds;
        storage = new ArrayList<RgbLed>(numRgbLeds);
        for (int i = 0; i < numLeds; i++) {
            storage.add(new RgbLed());
        }
    }

    public int size() { return numLeds; }

    // accessors & setters will go here

}

I plan on using an Observer/subject pattern to 'notify' the GUI when the LED objects are updated, but have not implemented this due to where I'm stuck.

My problem is that I do not know how to handle the returning of the state. The state may be a single Integer (such as in the case of LED), 3 Integers (such as in the RGB LED), or arrays of Single or Triple integers for LED Strip & RGB Strip respectively and the number of them is determined at runtime. I plan on implementing other types of objects for display in the future, too.

I considered hard-coding this where I need to know what each object type returns, but it felt wrong to me and highly coupled. I could be wrong.

Is there some sort of design pattern I should be considering when I have related objects that ultimately contain the same type of data (integers) but in different amounts?

Aucun commentaire:

Enregistrer un commentaire