1类图
2java示例
{ main([] args) { Context context Context(); context.setSource(); Translator comma CommaTranslator(); Translator period PeriodTranslator(); SentenctTranstor sentence SentenctTranstor(); sentence.addTranslator(period); sentence.addTranslator(comma); sentence.translate(context); System.out.println(context.getSource()); } }
{ source; getSource() { source; } setSource( source) { .source source; } }
{ Map, vocabulary HashMap,(); translate(Context context); }
Translator { CommaTranslator(){ .vocabulary.put(, ); } translate(Context context) { sourcecontext.getSource(); [] sourceArysource.split(); sb (); Set keys.vocabulary.keySet(); ( i;isourceAry.length;i){ (keys.contains(sourceAry[i])){ sb.append(.vocabulary.get(sourceAry[i])); sb.append(); }{ sb.append(sourceAry[i]); sb.append(); } } context.setSource(sb.toString()); } }
Translator { PeriodTranslator(){ .vocabulary.put(, ); } translate(Context context) { sourcecontext.getSource(); [] sourceArysource.split(); sb (); Set keys.vocabulary.keySet(); ( i;isourceAry.length;i){ (keys.contains(sourceAry[i])){ sb.append(.vocabulary.get(sourceAry[i])); sb.append(); }{ sb.append(sourceAry[i]); sb.append(); } } context.setSource(sb.toString()); } }
Translator { ListTranslator ts ArrayListTranslator(); SentenctTranstor(){ .vocabulary.put(, ); .vocabulary.put(, ); .vocabulary.put(, ); } addTranslator(Translator t){ .ts.add(t); } translate(Context context) { (Translator t:ts){ t.translate(context); } sourcecontext.getSource(); [] sourceArysource.split(); sb (); Set keys.vocabulary.keySet(); ( i;isourceAry.length;i){ (keys.contains(sourceAry[i])){ sb.append(.vocabulary.get(sourceAry[i])); sb.append(); }{ sb.append(sourceAry[i]); sb.append(); } } context.setSource(sb.toString()); } }