3

I have an OOP design question.

Let's assume that I have a class that contains several numerical scalar properties like maximum, minimum, frequency etc. Since data are flowing in continuously I eventually end up with a list of such class instances. To obtain, say, the global minimum I loop over all classes in the list to find it.

Alternatively, I could instantiate one class (possibly a singleton) that contains lists instead of scalars for each property, and function members that loop over the lists. This approach however seems to generate code that looks more like procedural than object oriented programming.

The question is: What criterions define which approach to choose? If efficiency is important, should I choose one class that contains lists for each properties? If readability is key, should I choose a list of classes?

Thanks for suggestions.

  • depends what you're trying to design..please post some sudo code and add more info. what are the classes? what shall they contain? how do you see the classes interacting, etc.. – Dory Zidon May 26 '13 at 20:56
  • As @Dory Zidon writes: it depends on what you are trying to accomplish. If you have some high performance requirements, breaking up the data into lists may be necessary, but if clarity and ease of maintenance is a priority, doing the opposite may be necessary. – mzedeler May 26 '13 at 20:58
  • Please the the [FAQ as to why this is a poor question](http://stackoverflow.com/faq#dontask) to ask here. – msw May 26 '13 at 20:59
  • @msw true..that's why I was trying to help him out a little :) – Dory Zidon May 26 '13 at 21:02
  • Thanks for your answers. I agree that my question is general. As such it might be classified as poor. However I believe the question is legitimate and of interest to many programmers. In my specific case performance will be important, but my primary interest is in the criterions that lead to the one or the other approach. In my next application clarity might be more important. – user2423086 May 30 '13 at 20:13
  • Since I'm not that experienced I have finally decided to write small skeleton prototypes for both variants and test them. – user2423086 May 30 '13 at 20:23

5 Answers5

1

Basically you're askyng if it's more preferable to have an "Array of Structures (AoS)" or "Structure of Arrays (SoA)"

The answer depends on what you need to do with this data. If you want to write a more readable code than go for an Array of Structures, if you want to use SSE or CUDA to optimize your computation-heavy code then go for a Structure of Arrays.

If you search in literature the terms "Array of Structures (AoS)" and "Structure of Arrays (SoA)" you will find many in depth dissertations on this topic, i link just some discussions here:

Community
  • 1
  • 1
Nicola Pezzotti
  • 2,317
  • 1
  • 16
  • 26
  • Thanks for this answer. I already started to read up. I believe your answer solves my problem as good as possible as it points to the fact that this issue is broadly discussed in the literature and that there is no binary yes/no solution. – user2423086 May 30 '13 at 20:16
1

You were asking for decision criteria. Let me recommend one:

You should think about what constitutes a data point in your application. Let's assume you are measuring values, and one data point consists of several numerical properties. Then you would certainly want a list of classes, where the class represents all of the properties that go together (what I called 'data point' for lack of a better term).

If you must perform some aggregation of these 'data points', such as finding a global minimum over a longer time period, I would suggest designing an extra component for this. So you'd end up with a data gathering component which consists mainly of a 'list of classes', and an aggregation component which may utilize different data structures, but processes parts of your 'list of classes' (say, the part over which the global minimum is to be found).

barfuin
  • 16,865
  • 10
  • 85
  • 132
0

Basically, OOP is not a solution to every question in programming, sometimes you have to see beyond this,below this. The thing is you have to concentrate on the problem. Efficiency should be more preferable. But if your code is taking too much time to load or you can say its time complexity is too much high then again you'll have trouble. You have to keep to keep both ends in hands. What i'll prefer is the list of classes not the class of list. But different people have different point of views and so we should respect them. Why i am chosing list of classes because,i'll have each object with the respected data, say , i have one object with higher frequency,one with lower,one with a medium,it will be easier to manage all that,plus not much time will be taken. i think in both cases it will be O(n) where n is the number of elements or classes in my case.

Rana Waleed
  • 55
  • 10
0

You could also store the values and the statistics together in one class and do the calculations on the fly when adding a new value (example in Java):

public class YourClass {
    private List<Integer> values = new ArrayList<Integer>();

    private long sum = 0;
    private int minimum = Integer.MAX_VALUE;
    private int maximum = Integer.MIN_VALUE;
    // add more stuff you need

    public synchronized void add(Integer value) {
        values.add(value);
        sum += value;

        if (value < minimum) {
            minimum = value;
        }

        if (value > maximum) {
            maximum = value;
        }
    }

    public List<Integer> getValues() {
        return Collections.unmodifiableList(values);
    }

    public long getSum() {
        return sum;
    }

    public long getAvg() {
        return values.isEmpty() ? 0 : sum / values.size();
    }

    public int getMaximum() {
        return maximum;
    }

    public int getMinimum() {
        return minimum;
    }
}
Marco Forberg
  • 2,634
  • 5
  • 22
  • 33
  • Thanks. True, the properties mentioned in the question (and actually quite some more) have the mathematical property that they can be updated on the fly with every new data point. There are however more involved properties that do require all data points explicitly. I would like to keep the code such as to include such properties later on. – user2423086 May 30 '13 at 20:21
0

In your case, list of data

struct Statistic{
   int max;
   int min;
   std::vector<int> points;
   int average;
};

int main(){
   std::vector<Statistic> stats;
   return 0;
}
dchhetri
  • 6,926
  • 4
  • 43
  • 56