Guest User

Untitled

a guest
Jun 28th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. ### DRY原则(Don't Repeat Yourself)
  2. * 最初级的DRY:语法级别
  3. * before
  4. ```java
  5. System.out.println(1);
  6. System.out.println(2);
  7. ……
  8. System.out.println(10);
  9. ```
  10. * after
  11. ```java
  12. for (int i = 1; i <= 10; i++) {
  13. System.out.println(i);
  14. }
  15. ```
  16. * 进阶的DRY原则:方法级别
  17. * before
  18. ```java
  19. try {
  20. Thread.sleep(1000);
  21. } catch (InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. ```
  25. * after
  26. ```java
  27. private static void threadSleep(int millis) {
  28. try {
  29. Thread.sleep(millis);
  30. } catch (InterruptedException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. threadSleep();
  35. ```
  36. * 继续进阶的DRY原则:类型级别
  37. * before
  38. ```java
  39. public class Person {
  40. private String name;
  41. private int age;
  42. // Setter & Getter ...
  43. }
  44. Person person = new Person();
  45. person.setName("jack");
  46. person.setAge(18);
  47. Person person2 = new Person();
  48. person2.setName("rose");
  49. person2.setAge(17);
  50. .....
  51. System.out.printf("Name: %s, Age:%d\n",person.getName(), person.getAge());
  52. System.out.printf("Name: %s, Age:%d\n",person2.getName(), person2.getAge());
  53. .....
  54. ```
  55. * after
  56. ```java
  57. public Person(String name, int age) {
  58. this.name = name;
  59. this.age = age;
  60. public String toString() {
  61. return String.format("Name: %s, Age: %d", name, age);
  62. }
  63. }
  64.  
  65. Person person1 = new Person("jack", 18);
  66. Person person2 = new Person("rose", 17);
  67. System.out.println(person1.toString());
  68. System.out.println(person2.toString());
  69. ```
  70. * 继续继续进阶的DRY原则:多个类组合级别
  71. ```java
  72. List<Person> list = new ArrayList<>();
  73. list.add(new Person("jack", 18));
  74. list.add(new Person("rose", 17));
  75.  
  76. list.forEach(p -> System.out.println(p));
  77. ```
  78. * 设计模式(最佳实践)
  79. * before
  80. ```java
  81. public static boolean updatePassword(String username, String password, String newpassword) {
  82. Connection conn = null;
  83. PreparedStatement stmt = null;
  84. ResultSet rs = null;
  85. boolean success = false;
  86. try {
  87. conn = beginTransaction();
  88. stmt = conn.prepareStatement("select id, password from user where username = ?");
  89. stmt.setString(1, username);
  90. rs = stmt.executeQuery();
  91. if (rs.next()) {
  92. if (rs.getString("password").equals(password)) {
  93. PreparedStatement stmt2 = null;
  94. try {
  95. stmt2 = conn.prepareStatement("update user set password = ? where id = ?");
  96. stmt2.setString(1, newpassword);
  97. stmt2.setLong(2, rs.getLong("id"));
  98. success = stmt2.executeUpdate() > 0;
  99. } finally {
  100. safeClose(stmt2);
  101. }
  102. }
  103. }
  104. commitTransaction(conn);
  105. return success;
  106. } catch (SQLException e) {
  107. rollbackTransaction(conn);
  108. throw new RuntimeException(e);
  109. } finally {
  110. safeClose(rs);
  111. safeClose(stmt);
  112. safeClose(conn);
  113. }
  114. }
  115. ```
  116. * after
  117. ```java
  118. public static boolean updatePassword(String username, String password, String newpassword) {
  119. return connection(conn -> statement(conn, "select id, password from user where username = ?", stmt -> {
  120. stmt.setString(1, username);
  121. return resultSet(stmt, rs -> {
  122. if (rs.next()) {
  123. if (rs.getString("password").equals(password)) {
  124. long id = rs.getLong("id");
  125. return statement(conn, "update user set password = ? where id = ?", stmt2 -> {
  126. stmt2.setString(1, newpassword);
  127. stmt2.setLong(2, id);
  128. return stmt2.executeUpdate() == 1;
  129. });
  130. }
  131. }
  132. return false;
  133. });
  134. }));
  135. }
  136.  
  137. public interface ConnectionCallback<T> {
  138. T doConnection(Connection conn) throws SQLException;
  139. }
  140. public interface StatementCallback<T> {
  141. T doStatement(PreparedStatement stmt) throws SQLException;
  142. }
  143. public interface ResultSetCallback<T> {
  144. T doResultSet(ResultSet rs) throws SQLException;
  145. }
  146. public static <T> T connection(ConnectionCallback<T> callback) {
  147. Connection conn = null;
  148. T result = null;
  149. try {
  150. conn = beginTransaction();
  151. result = callback.doConnection(conn);
  152. commitTransaction(conn);
  153. } catch (SQLException e) {
  154. rollbackTransaction(conn);
  155. throw new RuntimeException(e);
  156. } finally {
  157. safeClose(conn);
  158. }
  159. return result;
  160. }
  161. public static <T> T statement(Connection conn, String sql, StatementCallback<T> callback) throws SQLException {
  162. PreparedStatement stmt = null;
  163. T result = null;
  164. try {
  165. stmt = conn.prepareStatement(sql);
  166. result = callback.doStatement(stmt);
  167. } finally {
  168. safeClose(stmt);
  169. }
  170. return result;
  171. }
  172. public static <T> T resultSet(PreparedStatement stmt, ResultSetCallback<T> callback) throws SQLException {
  173. ResultSet rs = null;
  174. T result = null;
  175. try {
  176. rs = stmt.executeQuery();
  177. result = callback.doResultSet(rs);
  178. } finally {
  179. safeClose(rs);
  180. }
  181. return result;
  182. }
  183. ```
  184. * 框架
  185. ```java
  186. @Transactional
  187. public boolean updatePassword(String username, String password, String newpassword) {
  188. User user = (User) session().createQuery("from User where username = :username")
  189. .setString("username", username)
  190. .uniqueResult();
  191. if (user != null && user.getPassword().equals(password)) {
  192. user.setPassword(newpassword);
  193. return true;
  194. }
  195. return false;
  196. }
  197. ```
Add Comment
Please, Sign In to add comment