//真对排序算法的变动应该抽象父类
classClassSort<E>{
privateTreeSet<E>set=null;
privatestaticClassSortsort=newClassSort();
privateClassSort(){}
publicstaticClassSortgetInstence(){
returnsort;
}//endmethod
publicTreeSet<E>getSet(Comparator<?superE>compartor){
if(set==null)
set=newTreeSet<E>(compartor);
returnset;
}//endmethod
}//endclassSortClass
classWriteNative<T>{
privateBufferedWriterbw=null;
privateTreeSet<T>set=null;
publicWriteNative(TreeSet<T>set){
this.set=set;
}
publicvoidWrite()throwsIOException{
Filepath=newFile("D:\\MyTestWrite3.txt");
if(!path.exists())
path.createNewFile();
Write(path);
}
publicvoidWrite(Filepath)throwsIOException{
if(path==null)
return;
if(bw==null)
bw=newBufferedWriter(newFileWriter(path,true));
if(set.size()!=0){
for(Tt:set)
bw.write(t+"\r\n");
}
bw.close();
}//endmethod
}//endclassWriteNative
interfaceIClientProgram{
voidwriteIn(Stringinfo);
voidwriteNative();
}
//这个类应该依赖于抽象而不应该依赖于具体实现算法的变动会给这个类带来毁灭性的灾难鸡肋代码就在这里让我们去重构它吧
//怎么重构才会改善这种高耦合性的代码带来的灾难呢这就是问题所在!
classClientimplementsIClientProgram{
//在构造函数中组合具体子类现在还没有抽象出父类所以先依赖具体实现吧!等待重构
privateWriteNativewriteNative=null;
privateClassSortsort=null;
String[]strs=newString[3];
TreeSet<Student>set=null;
publicvoidwriteIn(Stringinfo){
split(info);//分解
sort=ClassSort.getInstence();
set=sort.getSet(newComparator<Student>(){
publicintcompare(Studentstu1,Studentstu2){
longfirstCompartor=stu1.getSum()-stu2.getSum();
if(firstCompartor==0)
returnstu1.getName().compareTo(stu2.getName());
return(int)firstCompartor;
}
});//endanonymousinnerclass
longenglish=Long.parseLong(strs[1]);
longmath=Long.parseLong(strs[2]);
set.add(newStudent(strs[0],(english+math)));
}//endmethodwritenIn
publicvoidwriteNative(){
if(set==null)return;
writeNative=newWriteNative(set);
try{
writeNative.Write();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
privatevoidsplit(Stringinfo){
if(info!=null){
for(inti=0;i<3;i++){
strs[i]=info.split("")[i];
}//endfor
}//endif
}//endmethodsplit
}//endclassClient
classStudent{
privateStringname;
privatelongsum;
publicStudent(Stringname,longsum){
this.name=name;
this.sum=sum;
}//endconstructor
publicStringgetName(){
returnthis.name;
}
publiclonggetSum(){
returnthis.sum;
}
publicStringtoString(){
returnname+":"+sum;
}//endmethod
}//endinnerclass
上面的代码 已经实现了题目中要求完成的功能 同时代码中也提出了一些需要重构的地方 考虑一下如果需求真的变动了 要求用自然顺序去写入文件这段代码要改动的地方是不是毁灭性的改动 那么我们如何去避免能够预测的改动带来的风险呢?
下面是这段代码的main函数调用
代码
publicstaticvoidmain(String[]args){
System.out.println("请输入信息按照:姓名(空格)英语成绩(空格)数学成绩(空格) 换行输入bye结束输入");
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
IClientProgramcli=newClient();
Stringinfo;
booleanb=true;
while(b){
try{
info=br.readLine();
if("bye".equals(info))
break;
cli.writeIn(info);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}//endcatch
}//endwhile
cli.writeNative();
}//endmethodmain
客户端做到了与逻辑相分离,只是调用实现
接下来要做的就是总结问题 重构代码了 继续
优质内容筛选与推荐>>
1、从“Liskov替换原则”和“Refused Bequest”看“正方形为什么不能继承长方形”2、oracle创建表空间 扩展表空间文件 修改表空间自动增长3、velocity模板引擎学习(4)-在standalone的java application中使用velocity及velocity-tools4、Excel Services OverView系列--2使用Excel Web Access技术在线浏览Excel工作薄 5、duilib入门简明教程 -- 前言(1) (转)
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
分享想法到看一看
确定
最多200字,当前共字