Skip to content

Instantly share code, notes, and snippets.

@5idu
Created June 28, 2018 09:05
Show Gist options
  • Save 5idu/6940483c9fd382fe066ce159d60ac33e to your computer and use it in GitHub Desktop.
Save 5idu/6940483c9fd382fe066ce159d60ac33e to your computer and use it in GitHub Desktop.
code style design
### DRY原则(Don't Repeat Yourself)
* 最初级的DRY:语法级别
* before
```java
System.out.println(1);
System.out.println(2);
……
System.out.println(10);
```
* after
```java
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
```
* 进阶的DRY原则:方法级别
* before
```java
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
```
* after
```java
private static void threadSleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
threadSleep();
```
* 继续进阶的DRY原则:类型级别
* before
```java
public class Person {
private String name;
private int age;
// Setter & Getter ...
}
Person person = new Person();
person.setName("jack");
person.setAge(18);
Person person2 = new Person();
person2.setName("rose");
person2.setAge(17);
.....
System.out.printf("Name: %s, Age:%d\n",person.getName(), person.getAge());
System.out.printf("Name: %s, Age:%d\n",person2.getName(), person2.getAge());
.....
```
* after
```java
public Person(String name, int age) {
this.name = name;
this.age = age;
public String toString() {
return String.format("Name: %s, Age: %d", name, age);
}
}
Person person1 = new Person("jack", 18);
Person person2 = new Person("rose", 17);
System.out.println(person1.toString());
System.out.println(person2.toString());
```
* 继续继续进阶的DRY原则:多个类组合级别
```java
List<Person> list = new ArrayList<>();
list.add(new Person("jack", 18));
list.add(new Person("rose", 17));
list.forEach(p -> System.out.println(p));
```
* 设计模式(最佳实践)
* before
```java
public static boolean updatePassword(String username, String password, String newpassword) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
boolean success = false;
try {
conn = beginTransaction();
stmt = conn.prepareStatement("select id, password from user where username = ?");
stmt.setString(1, username);
rs = stmt.executeQuery();
if (rs.next()) {
if (rs.getString("password").equals(password)) {
PreparedStatement stmt2 = null;
try {
stmt2 = conn.prepareStatement("update user set password = ? where id = ?");
stmt2.setString(1, newpassword);
stmt2.setLong(2, rs.getLong("id"));
success = stmt2.executeUpdate() > 0;
} finally {
safeClose(stmt2);
}
}
}
commitTransaction(conn);
return success;
} catch (SQLException e) {
rollbackTransaction(conn);
throw new RuntimeException(e);
} finally {
safeClose(rs);
safeClose(stmt);
safeClose(conn);
}
}
```
* after
```java
public static boolean updatePassword(String username, String password, String newpassword) {
return connection(conn -> statement(conn, "select id, password from user where username = ?", stmt -> {
stmt.setString(1, username);
return resultSet(stmt, rs -> {
if (rs.next()) {
if (rs.getString("password").equals(password)) {
long id = rs.getLong("id");
return statement(conn, "update user set password = ? where id = ?", stmt2 -> {
stmt2.setString(1, newpassword);
stmt2.setLong(2, id);
return stmt2.executeUpdate() == 1;
});
}
}
return false;
});
}));
}
public interface ConnectionCallback<T> {
T doConnection(Connection conn) throws SQLException;
}
public interface StatementCallback<T> {
T doStatement(PreparedStatement stmt) throws SQLException;
}
public interface ResultSetCallback<T> {
T doResultSet(ResultSet rs) throws SQLException;
}
public static <T> T connection(ConnectionCallback<T> callback) {
Connection conn = null;
T result = null;
try {
conn = beginTransaction();
result = callback.doConnection(conn);
commitTransaction(conn);
} catch (SQLException e) {
rollbackTransaction(conn);
throw new RuntimeException(e);
} finally {
safeClose(conn);
}
return result;
}
public static <T> T statement(Connection conn, String sql, StatementCallback<T> callback) throws SQLException {
PreparedStatement stmt = null;
T result = null;
try {
stmt = conn.prepareStatement(sql);
result = callback.doStatement(stmt);
} finally {
safeClose(stmt);
}
return result;
}
public static <T> T resultSet(PreparedStatement stmt, ResultSetCallback<T> callback) throws SQLException {
ResultSet rs = null;
T result = null;
try {
rs = stmt.executeQuery();
result = callback.doResultSet(rs);
} finally {
safeClose(rs);
}
return result;
}
```
* 框架
```java
@Transactional
public boolean updatePassword(String username, String password, String newpassword) {
User user = (User) session().createQuery("from User where username = :username")
.setString("username", username)
.uniqueResult();
if (user != null && user.getPassword().equals(password)) {
user.setPassword(newpassword);
return true;
}
return false;
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment