侃侃重构(refactor)一
话说距离上次发文章已经是时隔将近一个半月了,期间忙着项目所以对于文章或者说知识的总结就给忽略了!
这次我们来说说重构,说之前要感谢带领我入门的那些大哥(我曾经的同事,我喊他锋哥,还有经理 包大哥),那个时候虽然大家都在讨论但是我作为一个菜鸟级的人物,听得不太懂不过现在做android 这一块我发现代码真是乱,所以就想到了这个东西,于是乎就做了一把refactor!
个人理解重构的概念
重构:就是在不改变现有软件的质量以及运行的情况下,对代码的整理以及对逻辑的从新梳理,已达到逻辑清晰,代码可读性好,整洁这样一个效果!
以上是我个人的理解,有误之处请大家指出来啊!无喷!
那么好了接下来说例子,因为我做的android 项目现在负责的就是xml解析数据 以及返回给 activity 数据 先来了解一下结构类图如下:
这是 原来没有重构之前的类图,需要说明的是service 和handler的子类有很多个!
下面说一下service 和 handler 里面的方法。。。
在每个service 和 handler 里面都包含有这样一个方法getXXXtBean():Object 返回的都是 具体的类型。。。(我这到这样做不好)
下面我们重点说一下 service 里面的方法。。
代码如下:
public static CheckOutBean getCheckOutBean(Context context,String playlistId,String pID) throws ParserConfigurationException, SAXException, IOException{String urlPath = URLRequest.CHECKOUT.getUrl()+"playlistid="+ playlistId + "&pid="+ pID; //这个地方每个sevice 的url 不同InputStream inputStream = HttpClient.getInputByHttpConn(urlPath,context);if(null != inputStream){SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser parser = factory.newSAXParser();ParserCheckOutXMLHandler handler = new ParserCheckOutXMLHandler();//这个地方调用的 handler 不同parser.parse(inputStream, handler);return handler.getCheckOutBean(); //返回的对象不同}return null;}
public static CheckUserPlayingBean getCheckUserPlayingBean(Context context,String playlistId) throws ParserConfigurationException, SAXException, IOException{String url = URLRequest.CHECKUSERPLAYING.getUrl()+ playlistId;InputStream inputStream = HttpClient.getInputByHttpConn(url,context);if(null != inputStream){SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser saxParser = factory.newSAXParser();ParserCheckUserPlayingXMLHandler handler = new ParserCheckUserPlayingXMLHandler();saxParser.parse(inputStream, handler);return handler.getCheckUserPlayingBean();}return null;}
public abstract class BaseService {int retryTimes = 5;protected Context context;protected String url;protected InputStream inputStream;protected SAXParserFactory factory;protected SAXParser parser;protected MyDefaultHandler handler;protected Object object;protected final void template(){getInputStream();if( null != inputStream ){factory = SAXParserFactory.newInstance();try {parser = factory.newSAXParser();handler = getHandler();parser.parse(inputStream, handler);} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}this.object = handler.getBean();}}private void getInputStream(){//这个方法 是因为某些 service 处理的逻辑需要反复的 去判断网络是否正常//所以我使用了一个callBack方法。。让子类决定是否调用这里面的逻辑if(callBack()){while(retryTimes>=1){inputStream = HttpClient.getInputByHttpConn(loadUrl(), context);if(null != inputStream){break;}try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}retryTimes --;}}inputStream = HttpClient.getInputByHttpConn(loadUrl(), context);}protected abstract String loadUrl();protected abstract MyDefaultHandler getHandler();protected boolean callBack(){return false;}}
public abstract class MyDefaultHandler extends DefaultHandler {public abstract Object getBean();}
private String playlistId;private String pID;public ParserCheckOutService(){}public ParserCheckOutService( Context context,String playlistId,String pID){this.context = context;this.playlistId = playlistId;this.pID = pID;this.template();//这个地方要注意了。。。。记得调用啊。。}@Overrideprotected String loadUrl() {return URLRequest.CHECKOUT.getUrl()+"playlistid="+ playlistId + "&pid="+ pID;}@Overrideprotected MyDefaultHandler getHandler() {return new ParserCheckOutXMLHandler();}public CheckOutBean getCheckOutBean() {return (CheckOutBean)object;}
if(callBack()){while(retryTimes>=1){inputStream = HttpClient.getInputByHttpConn(loadUrl(), context);if(null != inputStream){break;}try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}retryTimes --;}}
@Overrideprotected boolean callBack() {return true;}