Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 特征向量生成器-什么设计模式有用?_Java_Design Patterns - Fatal编程技术网

Java 特征向量生成器-什么设计模式有用?

Java 特征向量生成器-什么设计模式有用?,java,design-patterns,Java,Design Patterns,我的问题看起来很简单,但只是一见钟情(对我来说:p)。 我需要创建一个类来构建一个特征向量。此特征向量表示文本。特点有:单词长度短,有洞文本中的句子数等 一些特征可以在其他特征计算过程中提取,这就是为什么我稍微修改了一个生成器设计模式,它看起来像这样: 我正在创建一个生成器对象: FeatureVectorBuilder fvb = new FeatureVectorBuilder(String text/InputStream <- now it doesn't matter) 一切

我的问题看起来很简单,但只是一见钟情(对我来说:p)。
我需要创建一个类来构建一个特征向量。此特征向量表示文本。特点有:单词长度短,有洞文本中的句子数等

一些特征可以在其他特征计算过程中提取,这就是为什么我稍微修改了一个生成器设计模式,它看起来像这样:

我正在创建一个生成器对象:

FeatureVectorBuilder fvb = new FeatureVectorBuilder(String text/InputStream <- now it doesn't matter) 
一切看起来都很好,但是。。。大约有32种不同的功能需要设置…
这样,悲观情况需要调用32个函数,创建一个包含32个参数的函数看起来也很愚蠢


我想知道是否有人在纠结这样一个问题,也许有比“32种不同的方法”更好的解决方案:)

构建器模式的要点之一是通过在构建器中用几个方法替换大量参数的方法来避免这些方法。如果您有32个可能的特性,那么在构建器中有32个方法对我来说是正常的

另一种可能性是将每个要素设计为一个类,并在生成器中添加这些类的实例:

builder.addFeature(new LengthWordFeature(true))
       .addFeature(new XxxFeature(32));

一种干净封装功能的可能性是:

abstract class Feature
{
    String name;
    ...
}

class NumericFeature extends Feature
{
    int value;
}

class OtherFeatureType extends Feature
{
    ....
}

Feature[] features = new Feature[] {
    new NumericFeature("xxxFeature", 32),
    new OtherFeature("feature1", ...),
    ...
};

FeatureVectorBuilder fvb = new FeatureVectorBuilder(text, features);

创建8个函数,每个函数有4个参数:)有时工作就是工作,这是不可避免的。
fvb.getFeatureVector();
builder.addFeature(new LengthWordFeature(true))
       .addFeature(new XxxFeature(32));
abstract class Feature
{
    String name;
    ...
}

class NumericFeature extends Feature
{
    int value;
}

class OtherFeatureType extends Feature
{
    ....
}

Feature[] features = new Feature[] {
    new NumericFeature("xxxFeature", 32),
    new OtherFeature("feature1", ...),
    ...
};

FeatureVectorBuilder fvb = new FeatureVectorBuilder(text, features);