- 浏览: 76763 次
文章分类
- 全部博客 (89)
- web service (9)
- subversion (1)
- JBOSS (3)
- interview (23)
- jQery (2)
- ExtJs (0)
- Axis (0)
- Design pattern (3)
- Agile (2)
- mutithread (0)
- Core Java (24)
- programming methods (1)
- SSH (7)
- jee design (1)
- OO (4)
- books (8)
- other (1)
- JSF (7)
- seam (2)
- Weblogic (4)
- JPA (1)
- ADF (1)
- Spring (5)
- Tomcat (1)
- DWR (2)
- JEE (3)
- Servlet (1)
- EJB (1)
- JDBC (3)
最新评论
-
iloveflower:
呵呵。好好学习。。。。。。。。。。。。
java 读书 -
Eric.Yan:
看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
java 读书
http://www.javapractices.com/topic/TopicAction.do;jsessionid=6E104A048475638493BA555238EC17F4?Id=34
Lazy initialization is a performance optimization. It's used when data is deemed to be 'expensive' for some reason. For example: ■if the hashCode value for an object might not actually be needed by its caller, always calculating the hashCode for all instances of the object may be felt to be unnecessary. ■since accessing a file system or network is relatively slow, such operations should be put off until they are absolutely required. Lazy initialization has two objectives : ■delay an expensive operation until it's absolutely necessary ■store the result of that expensive operation, such that you won't need to repeat it again As usual, the size of any performance gain, if any, is highly dependent on the problem, and in many cases may not be significant. As with any optimization, this technique should be used only if there is a clear and significant benefit. To avoid a NullPointerException, a class must self-encapsulate fields that have lazy initialization. That is, a class cannot refer directly to such fields, but must access them through a method. The hashCode method of an immutable Model Object is a common candidate for lazy initialization. Example 1 In this example, there are two fields with lazy initialization - fHashCode and fAwards. import java.util.*; public final class Athlete { public Athlete(int aId){ //a toy implementation: fId = aId; fName = "Roger Bannister"; //fAwards is not set here! } //..elided /** Lazy initialization is used here; this assumes that awards may not always be of interest to the caller, and that for some reason it is particularly expensive to fetch the List of Awards. */ public List getAwards(){ if ( fAwards == null ) { //the fAwards field has not yet been populated //Here is a toy implementation List<String> awards = new ArrayList<String>(); awards.add( "Gold Medal 2006" ); awards.add( "Bronze Medal 1998" ); fAwards = awards; } return fAwards; } /** This style applies only if the object is immutable. Another alternative is to calculate the hashCode once, when the object is initially constructed (again, applies only when object is immutable). */ @Override public int hashCode(){ if ( fHashCode == 0 ) { fHashCode = HashCodeUtil.SEED; fHashCode = HashCodeUtil.hash(fHashCode, fId); fHashCode = HashCodeUtil.hash(fHashCode, fName); //self-encapusulated: fAwards is not referenced directly, //since it may be null: fHashCode = HashCodeUtil.hash(fHashCode, getAwards()); } return fHashCode; } // PRIVATE // private int fId; private String fName; private List<String> fAwards; private int fHashCode; } Example 2 Here, the look up of the printers available to a desktop PC is treated as an expensive operation. import java.util.Arrays; import java.util.List; import javax.print.DocFlavor; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.OrientationRequested; import javax.print.attribute.standard.Sides; /** Printing services available to a desktop client. */ public final class Printers { /** Print some plain text (perhaps internally converted to PDF). */ void printSomething(String aText, PrintService aPrinter) { //...elided } /** Return the list of printers that can print PDFs (double-sided, portrait).*/ List<PrintService> listAvailablePrinters(){ if(fAvailablePrinters == null){ //double-sided, portrait, for PDF files. PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); attrs.add(Sides.DUPLEX); attrs.add(OrientationRequested.PORTRAIT); //Expensive operation! This can take several seconds in some environments: fAvailablePrinters = Arrays.asList( PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PDF, attrs) ); } return fAvailablePrinters; } // PRIVATE /** Looked up once, the first time it's needed, and then stored using a static reference. If it was a non-static reference, then the list of available printers would not be looked up just once, but perhaps many times (once per 'Printers' object, and not once per loaded 'Printers' class). Self-encapsulate : If this class's implementation needs to reference this item, it must do so indirectly, by calling listAvailablePrinters(). */ private static List<PrintService> fAvailablePrinters; } Example 3 Lazy initialization is particularly useful for GUIs which take a long time to construct. There are several policies for GUI construction which a design may follow: ■ always build - construct the window many times, whenever it is demanded, and do not cache the result. ■ first-request build - construct the window once, when first requested. Cache the result for any further requests, should they occur. ■ background build - construct the window once, in a low priority worker thread, when the system is initialized. Cache the result for any requests, should they occur. Here is an example of the first-request style, in which the fEditor field has lazy initialization (see the actionPerformed method). package hirondelle.stocks.preferences; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.util.logging.*; import hirondelle.stocks.util.Args; import hirondelle.stocks.util.ui.StandardEditor; import hirondelle.stocks.util.ui.UiUtil; import hirondelle.stocks.preferences.PreferencesEditor; import hirondelle.stocks.util.Util; /** * Present dialog to allow update of user preferences. * * <P>Related preferences are grouped together and placed in * a single pane of a <tt>JTabbedPane</tt>, which corresponds to an * implementation of {@link PreferencesEditor}. Values are pre-populated with * current values for preferences. * *<P>Most preferences have default values. If so, a * <tt>Restore Defaults</tt> button is provided for that set of related * preferences. * *<P>Preferences are not changed until the <tt>OK</tt> button is pressed. * Exception: the logging preferences take effect immediately, without the need * for hitting <tt>OK</tt>. */ public final class EditUserPreferencesAction extends AbstractAction { /** * Constructor. * * @param aFrame parent window to which this dialog is attached. * @param aPrefEditors contains implementations of {@link PreferencesEditor}, * each of which is placed in a pane of a <tt>JTabbedPane</tt>. */ public EditUserPreferencesAction (JFrame aFrame, List<PreferencesEditor> aPrefEditors) { super("Preferences...", UiUtil.getEmptyIcon()); Args.checkForNull(aFrame); Args.checkForNull(aPrefEditors); fFrame = aFrame; putValue(SHORT_DESCRIPTION, "Update user preferences"); putValue(LONG_DESCRIPTION, "Allows user input of preferences."); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_P) ); fPrefEditors = aPrefEditors; } /** Display the user preferences dialog. */ public void actionPerformed(ActionEvent event) { fLogger.info("Showing user preferences dialog."); //lazy construction: fEditor is created only once, when this action //is explicitly invoked if ( fEditor == null ) { fEditor = new Editor("Edit Preferences", fFrame); } fEditor.showDialog(); } // PRIVATE // private JFrame fFrame; private java.util.List<PreferencesEditor> fPrefEditors; private static final Logger fLogger = Util.getLogger(EditUserPreferencesAction.class); /** * Specifying this as a field allows for "lazy" creation and use of the GUI, which is * of particular importance for a preferences dialog, since they are usually heavyweight, * and have a large number of components. */ private Editor fEditor; /** * Return GUI for editing all preferences, pre-populated with current * values. */ private JComponent getPrefEditors(){ JTabbedPane content = new JTabbedPane(); content.setTabPlacement(JTabbedPane.LEFT); int idx = 0; for(PreferencesEditor prefEditor: fPrefEditors) { JComponent editorGui = prefEditor.getUI(); editorGui.setBorder(UiUtil.getStandardBorder()); content.addTab(prefEditor.getTitle() , editorGui); content.setMnemonicAt(idx, prefEditor.getMnemonic()); ++idx; } return content; } /** Called only when the user hits the OK button. */ private void saveSettings(){ fLogger.fine("User selected OK. Updating table preferences."); for(PreferencesEditor prefEditor: fPrefEditors) { prefEditor.savePreferences(); } } /** * An example of a nested class which is nested because it is attached only * to the enclosing class, and it cannot act as superclass since multiple * inheritance of implementation is not possible. * * The implementation of this nested class is kept short by calling methods * of the enclosing class. */ private final class Editor extends StandardEditor { Editor(String aTitle, JFrame aParent){ super(aTitle, aParent, StandardEditor.CloseAction.HIDE); } public JComponent getEditorUI () { JPanel content = new JPanel(); content.setLayout( new BoxLayout(content, BoxLayout.Y_AXIS) ); content.add( getPrefEditors() ); //content.setMinimumSize(new Dimension(300,300) ); return content; } public void okAction() { saveSettings(); dispose(); } } }
发表评论
-
Java Collection summary
2012-06-16 02:40 534Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 748I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 654How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 666Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 785Below is the syntax highlight ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 888A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 663各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 4751、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 786import java.util.*; public c ... -
String array to arraylist
2012-06-13 00:07 538There are some important thing ... -
core java interview summary
2012-06-12 04:11 344http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 522Question: 1) Write code to che ... -
OO Design books -good website
2012-06-07 03:13 658I’m always on the search on goo ... -
Write a String Reverser (and use Recursion!)
2012-06-07 03:03 489Java Interview questions: Write ... -
Java高手必会的要点
2012-05-29 03:28 579http://developer.51cto.com/art/ ... -
How to use getClass().getClassLoader().getResource()
2012-05-29 03:13 1710This is the simplest wat to get ... -
How to override equals method in Java
2012-05-12 02:57 1509Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 874Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 812ArrayList in Java is most frequ ... -
How to use Comparator and Comparable in Java? With example
2012-05-12 02:21 736Read more: http://javarevisited ...
相关推荐
延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 <idbag> 14 双向关联(Bidirectional Associations) 14 三重关联(Ternary Associations) 15 异类关联(Heterogeneous ...
Puzzle 85: Lazy Initialization Chapter 10. Advanced Puzzlers Puzzle 86: Poison-Paren Litter Puzzle 87: Strained Relations Puzzle 88: Raw Deal Puzzle 89: Generic Drugs Puzzle 90: It's Absurd, It's a ...
Item 83: Use lazy initialization judiciously Item 84: Don’t depend on the thread scheduler 12 Serialization Item 85: Prefer alternatives to Java serialization Item 86: Implement Serializable with ...
Puzzle 85: Lazy Initialization Chapter 10. Advanced Puzzlers Puzzle 86: Poison-Paren Litter Puzzle 87: Strained Relations Puzzle 88: Raw Deal Puzzle 89: Generic Drugs Puzzle 90: It's ...
主要是介绍各种格式流行的软件设计模式,对于程序员的进一步提升起推进作用,有时间可以随便翻翻~~ 23种设计模式汇集 如果你还不了解设计模式是什么的话? 那就先看设计模式引言 ! 学习 GoF 设计模式的重要性 ...
Chapter 21: Lazy Initialization Pattern ................................................................. 403 Chapter 22: Factory Pattern ..............................................................
延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用<idbag><br>6.8. 双向关联(Bidirectional Associations) 6.9. 三重关联(Ternary Associations) 6.10....
延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用<idbag><br>6.8. 双向关联(Bidirectional Associations) 6.9. 三重关联(Ternary Associations) 6.10....
一个 Vaadin 插件,用于在不使用推送的情况下对 UI ... Lazy . schedule(() - > { // access db // update UI with results }); 如果您只有一个 init 块并且不介意 UI 在整个延迟初始化操作期间被锁定(显示正常
修复自动配置 lazy-initialization 无属性提示 修复 mysql on duplicate key update 字段名判断为表名问题 修复 lambda 条件 npe 异常 重构 lambda 信息提取方法 获取 lambda 信息不在序列化 合并 gitee pulls/ 141 ...
If you need a lazy View initialization you would want to implement your own ViewFlow.ViewLazyInitializeListener and pass it to the setOnViewLazyInitializeListener() method. viewFlow....
konteiner-用于node.js应用程序的简单零依赖DI容器 本模块为您提供以下方法: ...通过在构造函数/父函数提供的Konteiner实例上通过getter获取它们,从而在模块中使用已初始化的依赖关系 使模块以惰性方式初始化,即在...
You are visitor as of October 17, 1996. The Art of Assembly Language Programming <br>Forward Why Would Anyone Learn This Stuff? 1 What's Wrong With Assembly Language 2 What's Right With ...
Not Using Commons Logging ..........................Lazy-initialized beans ....................................................................................... 47 Autowiring collaborators ...............
Not Using Commons Logging ..........................Lazy-initialized beans ....................................................................................... 47 Autowiring collaborators ...............
go程序设计语言 Contents Preface................................................................................................................................. xix PART 1—WHY LEARN GO—GETTING ...
The Way to Go,: A Thorough Introduction to the Go Programming Language 英文书籍,已Cross the wall,从Google获得书中源代码,分享一下。喜欢请购买正版。 目录如下: Contents Preface......................