我有一堆不同的 Feature
类来计算图像特征。
我必须从这些类中提取“关键特征”,这些特征将被打包在一起作为搜索键。
我还将存储部分 Feature
类。我无法存储整个要素类,因为那样效率非常低。
现在,我想到的是编写一个 Stored_features
类,将“关键功能”组合在一起。
我的布局是:
Facial_features
| | |
| V |
| .---Feature1 V <|-- Abstract_feature
| | .---Feature2 <|---'
V V V
Stored_features
我的问题是,这样的Stored_features
类会有很多getter和setter,据我所知,getters和setter表明设计不好。是否有一种易于维护的方法来避免这里出现过多的 getter 和 setter?
重点是我看到我的代码与此布局紧密结合:(
编辑:
我的代码按要求摘录。
#include <opencv2/core/core.hpp>
class Abstract_feature{
public:
virtual void calculate()=0;
virtual void draw(cv::Mat& canvas)=0;
/// to put values into Stored_features
virtual void registrate_key_values(Stored_features&) const=0;
};
class Facial_features : Abstract_feature{
public:
virtual void calculate()
{
es.calculate; sc.calculate;
/*etc but iterating over a list of Abstract_feature's*/
}
Stored_features get_stored_features() const
{
return sf.clone();
}
private:
Stored_features sf;
Head_size es;
Skin_color sc;
};
class Head_size : public Abstract_feature{
//you can guess the impl.
};
class Stored_features{
public:
typedef enum{SKIN_COLOR=0, HEAD_SIZE_WIDTH,HEAD_SIZE_HEIGHT} Name;
public:
void set_key_feature(Name, double value);
cv::Mat get_feature_vector() const {return key_values;}
private:
cv::Mat key_values;
// and here would come other features eg.
cv::Rect head_roi; // I don't search based on these. (So I should not use getters/setters?)
};
添加了opencv,因为无论如何它都是一个基于opencv的项目。
最佳答案
许多 getter 和 setter 本身并不表示设计不好。然而,它们可以暗示你的类(class)可以分解为更小的类(class)。以“客户”为例,如果您将所有名称、地址、送货地址字段放在一个类中,那么这将是一个糟糕的设计,您应该使用“地址”类对其进行拆分。
我不确定你的设计是关于什么的,重点是你是否需要 100 个 setter/getter ,因为你有 100 个不相关的字段使用 100 个 setter/getter
关于c++ - 提取特征。一个类中有很多 getter/setter 是否正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13349633/