public static LogUtil getInstance() { if (sLogUtil == null) { synchronized (LogUtil.class) { if (sLogUtil == null) { sLogUtil = new LogUtil(); } } } return sLogUtil; }
扩展
A. 无论synchronized关键字加在方法上还是对象上,如果它作用的对象是非静态的,则它取得的锁是对象;如果synchronized作用的对象是一个静态方法或一个类,则它取得的锁是对类,该类所有的对象同一把锁。 B. 每个对象只有一个锁(lock)与之相关联,谁拿到这个锁谁就可以运行它所控制的那段代码。 C. 实现同步是要很大的系统开销作为代价的,甚至可能造成死锁,所以尽量避免无谓的同步控制。
public class AllStatistics implements Statistics {
private List<Statistics> statistics = new ArrayList<Statistics>();
public AllStatistics() { statistics.add(new NovelStatistics()); statistics.add(new TechnicalStatistics()); }
@Override public int getBrowseCount() { int browseCount = 0; for (Statistics s : statistics) { browseCount += s.getBrowseCount(); } return browseCount; }
@Override public int getSalesCount() { int saleCount = 0; for (Statistics s : statistics) { saleCount += s.getBrowseCount(); } return saleCount; }
@Override public String getSQL(String[] usernames) { StringBuilder sql = new StringBuilder("select * from user_info where "); for (String user : usernames) { sql.append("username = '"); sql.append(user); sql.append("' or "); } sql.delete(sql.length() - " or ".length(), sql.length()); return sql.toString(); }
}
public class Strategy2 implements Strategy {
@Override public String getSQL(String[] usernames) { StringBuilder sql = new StringBuilder("select * from user_info where "); boolean needOr = false; for (String user : usernames) { if (needOr) { sql.append(" or "); } sql.append("username = '"); sql.append(user); sql.append("'"); needOr = true; } return sql.toString(); }
@Override protected String formating(Book book) { String result = ""; result += "<book_name>" + book.getBookName() + "</book_name>\n"; result += "<pages>" + book.getPages() + "</pages>\n"; result += "<price>" + book.getPrice() + "</price>\n"; result += "<author>" + book.getAuthor() + "</author>\n"; result += "<isbn>" + book.getIsbn() + "</isbn>\n"; return result; }
}
public class JSONFormatter extends Formatter {
@Override protected String formating(Book book) { String result = ""; result += "{\n"; result += "\"book_name\" : \"" + book.getBookName() + "\",\n"; result += "\"pages\" : \"" + book.getPages() + "\",\n"; result += "\"price\" : \"" + book.getPrice() + "\",\n"; result += "\"author\" : \"" + book.getAuthor() + "\",\n"; result += "\"isbn\" : \"" + book.getIsbn() + "\",\n"; result += "}"; return result; }
}
public class YAMLFormatter extends Formatter {
@Override protected String formating(Book book) { String result = ""; result += "book_name: " + book.getBookName() + "\n"; result += "pages: " + book.getPages() + "\n"; result += "price: " + book.getPrice() + "\n"; result += "author: " + book.getAuthor() + "\n"; result += "isbn: " + book.getIsbn() + "\n"; return result; }
}
public class Test {
public static void main(String[] args) throws Exception { Book book = new Book(); book.setBookName("Thinking in Java"); book.setPages(880); book.setPrice(68); book.setAuthor("Bruce Eckel"); book.setIsbn("9787111213826"); XMLFormatter xmlFormatter = new XMLFormatter(); String result = xmlFormatter.formatBook(book); System.out.println(result);
JSONFormatter jsonFormatter = new JSONFormatter(); result = jsonFormatter.formatBook(book); System.out.println(result);
YAMLFormatter yamlFormatter = new YAMLFormatter(); String result = yamlFormatter.formatBook(book); System.out.println(result); }