PDA

View Full Version : OO programming


mickey
24th August 2008, 18:53
Hello,
I have this class:

class Value {
public:
double _value;
bool _missed;
};
class Feature {
double _mean;
vector<Value> _featVector;
void computeMean() {
for (int i=0; i < _featVector.size(); ++i) {
sum += _featVec[i]._value;
}
_mean = sum / _featVec.size();
}
void normalize() {
for (int i=0; i < _featVector.size(); ++i) {
_featVec[i]._value = /*normalization equation */
}
}

class Datas {
vector<Feature> _fet;
public:

void computeMean() {
for(int i=0; i < _feat.size(); ++i)
_feat[i].computeMean();
}
void normalize() {
for(int i=0; i < _feat.size(); ++i)
_feat[i].normalize();
}
//main.cpp
Datas data;
data.computeMean();
data.normalize();

Beyond that utility of some things, the point is: Feature is able to compute the mean and normalization on itself BUT to compute it It's necessary call it from one other class....; is it good?
Furthermore: Will it better: class Feature : public vector<Value> { .....}

I hope of many opinions,

wysota
24th August 2008, 19:04
I'm not sure what you mean but if you want the mean to be computed "by itself" simply compute it the first time someone requests to read the mean.

mickey
24th August 2008, 19:09
I'm not sure what you mean but if you want the mean to be computed "by itself" simply compute it the first time someone requests to read the mean.
see the post #1 I edited it (now it's a bit more complex);
I thought that when Data is build, computeMean() must be called; when some changes in the datas occur, the mean should be computate again; to retrieve the mean there is a getMean() { return _mean; } method (there isn't in post #1 at moment).
"itself" mean capability of Feature of iterates on a its vector......

wysota
24th August 2008, 21:16
I still don't understand, but regarding calculating the mean upon data change - simply invalidate the mean so that it gets calculated next time someone queries for it.

....::append(double value){
vector.append(value);
mean = -1;
}

...::getMean(){
if(mean<0) calculateMean();
return mean;
}

mickey
24th August 2008, 21:57
hello,

my program must be start with creation of a object Data();

//Data.h
Data::Data() {
compouteMean();
normalize();
}
//main.cpp
Data d;

This mechanism when someome call getMean(), the _mean is set to some value.....(bad?)
My question was: is this OO design good? Data::computeMean() calls Feature::computeMean() ?

wysota
24th August 2008, 22:04
Well... this is tightly coupling those two objects, so you could certainly try to find a better solution.

mickey
24th August 2008, 22:38
i have no idea how improve it........anyway is good that Feature as capability to compute its mean? Or maybe is better leave this completly to Data class

wysota
24th August 2008, 22:43
I admit I can't understand your language. Could you try to rephrase your post?

mickey
24th August 2008, 23:09
Probabily I'm asking something obvious;

1.I understand that this design isn't good because there isn't object decoupling: how improve it? Any pattern to follow?

2. class Feature contains a vector plus some methods (and here one my previous question if it was better write "class Feature : public std::vector<Value>"....). Feature contains the value of mean (double _mean); I'm wondering is ok that Feature has a method that computes the Mean on a "ITS" vector (the vector vector<Value> _values ) or if it was better write "Data::computeMean()" in a way that it can compute the Feature::_mean "without" call Feature::computeMean() (and then the method Feature::computeMean() will be unuseful). I.e. (or similar)

Data::computeMean() {
for (int i=0; i < _feat.size(); ++i) {
double sum =0;
for (int j=0; j < _feat[i]._featVector[j].size; ++j)
sum += _feat[i]._featVector[j]._value;
_feat[i]._featVector[j].setMean( sum / _feat[i]._featVector[j].size() ) ;
}
}


Maybe if you suggest a good decoupling, my doubts will disappear..

Thanks,

jacek
25th August 2008, 00:07
You don't have to define these functions as methods of any class. computeMean() and normalize() are very good candidates for template functions that can be applied to any collection. If you don't want them lying around in a void, you can put them in a namespace.

mickey
25th August 2008, 20:11
or I could put it in a class "Statistics".
Anyway:
- could anyone get me an idea of how decouple these two class?
- putting it around, I don't know how template it:


template<class T>
T Max(T& t) {
return (*std::max_element( t.begin(), t.end(), Feature::less_than ));
//this will return a Value object; but how template the value of return to be a double???
}

//main.cpp
vector<Value> _values;
double maxValue = Max( _values);

wysota
25th August 2008, 23:13
- could anyone get me an idea of how decouple these two class?
Jacek already gave you two good hints.

I'd like to note one thing, Mickey. I have noticed some time ago that you desperately try to use existing STL calls or "STL-ize" your code when there is no need of doing it or when the STL call does something similar (as opposed to "the same") to what you want. Placing STL calls everywhere can easily make your code unreadable or can even break it if you happen to use a faulty STL implementation (remember that only the API is the standard, not the internal implementation of the library). Simple solutions are usually the best and there is no benefit from spending hours trying to wrap the functionality you want to obtain into an STL (or place another of ones favourite library name here, Qt included) call.

mickey
26th August 2008, 09:19
Jacek already gave you two good hints.

I'd like to note one thing, Mickey. I have noticed some time ago that you desperately try to use existing STL calls or "STL-ize" your code when there is no need of doing it or when the STL call does something similar (as opposed to "the same") to what you want. Placing STL calls everywhere can easily make your code unreadable or can even break it if you happen to use a faulty STL implementation (remember that only the API is the standard, not the internal implementation of the library). Simple solutions are usually the best and there is no benefit from spending hours trying to wrap the functionality you want to obtain into an STL (or place another of ones favourite library name here, Qt included) call.

It's not clear;
Are you saying to don't use std:max_element? (and write it by myself)
I can simply put into a namespace a function double computeMax() { }, but for my application the datas are in vector<Value> and I think that I'll have to pass this vector to computeMax()
so I'll have computeMax(vector<Value>& vec) { }, BUT in this way I have a function that is coupled with that vector.......
Probabibly I don't understand....

wysota
26th August 2008, 10:36
I say not to try to find a matching STL call where a single line of simple hand written code would be sufficient. This was a general remark, not a remark to this particular situation, at least not only.

mickey
26th August 2008, 20:03
or I could put it in a class "Statistics".
Anyway:
- could anyone get me an idea of how decouple these two class?
- putting it around, I don't know how template it:


template<class T>
T Max(T& t) {
return (*std::max_element( t.begin(), t.end(), Feature::less_than ));
//this will return a Value object; but how template the value of return to be a double???
}
//main.cpp
vector<Value> _values;
double maxValue = Max( _values);

How can I template this above, please???