针对热词分析进行系统质量的可修改性的体现
将数据库模块放于一个java类中,其他模块重复调用:
package hotWords.util; import java.sql.*; public class DBUtil { public static Connection getConnection() throws ClassNotFoundException, SQLException { String JDBC_DRIVER = "com.mysql.jdbc.Driver"; String DB_URL = "jdbc:mysql://localhost:3306/hotwords?useUnicode=true&characterEncoding=UTF-8&useSSL=false"; String USER = "root"; String PASS = "042669"; @SuppressWarnings("unused") Connection conn = null; Class.forName(JDBC_DRIVER); System.out.println("连接数据库..."); return conn = DriverManager.getConnection(DB_URL,USER,PASS); } public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement ) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet resultSet ) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
一、局部化修改——目标是减少由某个变更直接影响的模块的数量:
1、预期期望的变更(expected changes ):确保canVote() 方法返回true或者false, 同时你也能写一个测试用来验证这个方法抛出的IllegalArgumentException异常。Guava类库中提供了一个作参数检查的工具类--Preconditions类,也许这种方法能够更好的检查这样的参数,不过这个例子也能够检查
2、维持语义一致性(semantic coherence ):
3、泛化模块(Generalize the module ):
1)
public List<Explain> loadhot() throws ClassNotFoundException, SQLException { Connection connection=DBUtil.getConnection(); String sql="select * from keywords"; PreparedStatement preparedStatement=null; ResultSet resultSet=null; Explain explain=null; List<Explain> explains=new ArrayList<>(); int flag=1; try { preparedStatement=connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { if(resultSet.getInt("num")<300) continue; explain=new Explain(); explain.setWords(resultSet.getString("word")); explain.setNum(resultSet.getInt("num")); explain.setExp(resultSet.getString("exp")); explain.setId(flag); explains.add(explain); flag++; } }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return explains; }
2)
public List<Explain> load() throws ClassNotFoundException, SQLException { Connection connection=DBUtil.getConnection(); String sql="select * from keywords"; PreparedStatement preparedStatement=null; ResultSet resultSet=null; Explain explain=null; List<Explain> explains=new ArrayList<>(); int flag=1; try { preparedStatement=connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { explain=new Explain(); explain.setWords(resultSet.getString("word")); explain.setNum(resultSet.getInt("num")); explain.setExp(resultSet.getString("exp")); explain.setId(flag); explains.add(explain); flag++; } }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return explains; }
3)
public Explain loadmore(String wordname) throws ClassNotFoundException, SQLException { Connection connection=DBUtil.getConnection(); String sql="select * from keywords where word = "+"'"+wordname+"'"; PreparedStatement preparedStatement=null; ResultSet resultSet=null; Explain explain=null; try { preparedStatement=connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { explain=new Explain(); explain.setWords(resultSet.getString("word")); explain.setExp(resultSet.getString("exp")); } }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return explain; }
4、限制选择参数(Limit possible options ):
二、防止连锁反应——目标是限制对局部化的模块的修改,以防止对某个模块的修改间接地影响到其他模块;
尽量维持现有接口或类的名字等不变,把要改动的模块尽量降到最低。推迟绑定时间在我的系统中还没有体现出来。
维持语义的一致性是保证模块中不同责任之间可以协同工作,不要太多的依赖于其他的模块。
1、信息隐藏(Hide information ):
2、维持现有接口(Maintain existing interfaces ):
3、限制通信路径(Restrict communication paths ):
4、使用仲裁者(Use an intermediary ):
三、延迟绑定时间——目标是控制部署时间并允许非开发人员进行修改。
1、运行时注册(Runtime registration):
2、配置文件(Configuration files):
3、多态(Polymorphism):
4、组件更换(Component replacement ) :
5、遵守已定义的协议(Adherence to defined protocols) :
延迟绑定时间:
延时0.5秒后发送一个网络请求,首先想到了handler,结果出现这么一个错误,解决方案很简单,就是在线程里调用Looper.prepare(),然后调用Looper.loop()就可以了
private void sendMessageToClient(final StringBuilder s){ new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } sendToClient.sendDataToClient(s,clientSocketAddress);//网络请求必须在子线程 } }).start(); }