What is happening in the following snippet?
I am not able to understand the following snipet. I mean what is happening exactly. Can any one explain me what is happening?
This is the snippet:
protected NodeService getUnprotectedNodeService()
{
if (this.unprotectedNodeService == null)
{
this.unprotectedNodeService = (NodeService) FacesHelper.getManagedBean(FacesContext.getCurrentInstance(), "nodeService");
}
开发者_如何学JAVAreturn this.unprotectedNodeService;
}
it sets this.unprotectedNodeService
if it not set yet(and also returns it..), and if it is already set, it just returns it.
seems like a caching mechanism to prevent calling heavy methods more then once
- If
this.unprotectedNodeService
is null, a bean is looked up and stored inthis.unprotectedNodeService
. this.unprotectedNodeService
is returned from the function.
This is an example of lazy initialization.
If the unprotectedNodeService
instance variable is not set, it's looking up a ManagedBean
stored in scope under the "nodeService
" key. If it doesn't exist it will get created. It's being casted to a NodeService
Object and returned, as well as being set as the instance variable. This must be from a JSF application. Look in faces-config.xml
or relevant @ManagedBean
annotations to find more info.
This is the lazy initializion pattern.
The field unprotectedNodeService
is initialized when it's first used (and not before).
Note that this code is not thread safe.
精彩评论