2012-11-01 41 views
3

我的問題看起來很簡單,但只是一見鍾情(對我來說:P)。
我需要創建一個構建特徵向量的類。這個特徵向量表示一個文本。特徵如下:Avarage word lenght,孔洞文本中句子的數量等Feature Vector Builder - 什麼樣的設計模式可能有用?

某些特徵可以在其他特徵計算期間提取,這就是爲什麼我稍微修改了一個Builder設計模式,它看起來像這樣:

我創建一個生成器對象:

FeatureVectorBuilder fvb = new FeatureVectorBuilder(String text/InputStream <- now it doesn't matter) 

那麼我specifing了訂單,其明確哪些功能我想包括

fvb.setLenghtWord(True) <- for fixed length features 
fvb.setXXXFeature(32) <- for variable length features 

下一個我正在構建此矢量:

fvb.buildFeatureVector() <- this way computations are optimized; 

最後我有一個FeatureVector來獲取。

fvb.getFeatureVector(); 

一切正常,但是......有大約32不同的功能來設置...
這樣,悲觀的情況下,需要32個函數調用,也創造了功能與32個參數看起來愚蠢。

我想知道,如果有人用這樣的問題struggeling,也許沒有比「32種不同的方法」更好的解決辦法:)

+1

創建8個功能,每個都有4個參數? :)有時工作是工作,這是不可避免的。 – irreputable

回答

0

一個生成器模式的要點是避免與大量的方法通過在構建器中用幾種方法替換它們來實現。如果您有32種可能的功能,那麼在構建器中使用32個方法對我來說看起來很正常。

另一種可能性是,設計每個要素爲一類,並添加這些類的實例在你的建設者:

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

一種可能性乾淨封裝的特點是:

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);