学以致用,但多此一举?# Java - 爪哇娇娃
r*r
1 楼
原来的 code:
for(FileDefinition file : FileDefinitions){
if( filePath.contains("Generic")){
HashSet entries = file.loadGenericLexicon();
}else if( filePath.contains("NNP")){
HashSet entries = file.loadNNPLexicon();
}else if( filePath.contains("TPO")){
HashSet entries = file.loadTPOLexicon();
}
}
class FileDefinition{
...
public HashSet loadGenericLexicon(){
HashSet features = new HashSet();
// reach each line from a file, extract information and store
...
return features
}
public HashSet loadNNPLexicon(){
// similar as above
}
public HashSet loadTPOLexicon(){
// similar as above
}
}
Refactoring Code (是不是很有点多此一举的感觉?):
for(FileDefinition file : FileDefinitions){
if( filePath.contains("Generic")){
HashSet entries = file.load(StrategyType.GenericLexicon);
}else if( filePath.contains("NNP")){
HashSet entries = file.load(StrategyType.NNPLexicon);;
}else if( filePath.contains("TPO")){
HashSet entries = file.load(StrategyType.TPOLexicon);
}
}
class FileDefinition{
...
private LoadContext loadContext_;
HashSet load(StrategyType type ){
switch( type ){
case GenericLexicon:
loadContext_ = new LoadContext(new GenericLexiconLoad(resource,
language));
break;
case NNPLexicon:
loadContext_ = new LoadContext(new NNPLexiconLoad(resource,
language));
break;
case TPOLexicon:
loadContext_ = new LoadContext(new TPOLexiconLoad(resource,
language));
break;
default:
throw new RuntimeException("Invalid strategy type: " + type);
}
return loadContext_.executeLoad();
}
interface LoadStrategy{
HashSet load();
public static enum StrategyType { GenericLexicon, NNPLexicon, TPOLexicon
}
public static final String LABEL_MARKER = "**";
}
abstract class LexiconLoad implements LoadStrategy{
protected String resource_;
protected Language language_;
public LexiconLoad(){
resource_ = null;
language_ = null;
}
public LexiconLoad(String resource, Language language){
resource_ = resource;
language_ = language;
}
public abstract HashSet load();
}
class NNPLexiconLoad extends LexiconLoad{
public NNPLexiconLoad(String resource, Language language){
super(resource, language);
}
public HashSet load(){
HashSet features = new HashSet();
...
return features;
}
}
class TPOLexiconLoad extends LexiconLoad{
public TPOLexiconLoad(String resource, Language language){
super(resource, language);
}
public HashSet load(){
HashSet features = new HashSet();
...
return features;
}
}
class GenericLexiconLoad extends LexiconLoad{
public GenericLexiconLoad(String resource, Language language){
super(resource, language);
}
public HashSet load(){
HashSet features = new HashSet();
...
return features;
}
}
class LoadContext{
private LoadStrategy loadStrategy_;
public LoadContext(LoadStrategy strategy){
loadStrategy_ = strategy;
}
public HashSet executeLoad(){
return this.loadStrategy_.load();
}
}
for(FileDefinition file : FileDefinitions){
if( filePath.contains("Generic")){
HashSet
}else if( filePath.contains("NNP")){
HashSet
}else if( filePath.contains("TPO")){
HashSet
}
}
class FileDefinition{
...
public HashSet
HashSet
// reach each line from a file, extract information and store
...
return features
}
public HashSet
// similar as above
}
public HashSet
// similar as above
}
}
Refactoring Code (是不是很有点多此一举的感觉?):
for(FileDefinition file : FileDefinitions){
if( filePath.contains("Generic")){
HashSet
}else if( filePath.contains("NNP")){
HashSet
}else if( filePath.contains("TPO")){
HashSet
}
}
class FileDefinition{
...
private LoadContext loadContext_;
HashSet
switch( type ){
case GenericLexicon:
loadContext_ = new LoadContext(new GenericLexiconLoad(resource,
language));
break;
case NNPLexicon:
loadContext_ = new LoadContext(new NNPLexiconLoad(resource,
language));
break;
case TPOLexicon:
loadContext_ = new LoadContext(new TPOLexiconLoad(resource,
language));
break;
default:
throw new RuntimeException("Invalid strategy type: " + type);
}
return loadContext_.executeLoad();
}
interface LoadStrategy{
HashSet
public static enum StrategyType { GenericLexicon, NNPLexicon, TPOLexicon
}
public static final String LABEL_MARKER = "**";
}
abstract class LexiconLoad implements LoadStrategy{
protected String resource_;
protected Language language_;
public LexiconLoad(){
resource_ = null;
language_ = null;
}
public LexiconLoad(String resource, Language language){
resource_ = resource;
language_ = language;
}
public abstract HashSet
}
class NNPLexiconLoad extends LexiconLoad{
public NNPLexiconLoad(String resource, Language language){
super(resource, language);
}
public HashSet
HashSet
...
return features;
}
}
class TPOLexiconLoad extends LexiconLoad{
public TPOLexiconLoad(String resource, Language language){
super(resource, language);
}
public HashSet
HashSet
...
return features;
}
}
class GenericLexiconLoad extends LexiconLoad{
public GenericLexiconLoad(String resource, Language language){
super(resource, language);
}
public HashSet
HashSet
...
return features;
}
}
class LoadContext{
private LoadStrategy loadStrategy_;
public LoadContext(LoadStrategy strategy){
loadStrategy_ = strategy;
}
public HashSet
return this.loadStrategy_.load();
}
}