Added code comments.
This commit is contained in:
parent
b0b4ca09f8
commit
a016d3355c
@ -1,5 +1,29 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* Resource Acquisition Is Initialization pattern was developed
|
||||
* for exception safe resource management by C++ creator Bjarne
|
||||
* Stroustrup.
|
||||
*
|
||||
* In RAII resource is tied to object lifetime: resource allocation
|
||||
* is done during object creation while resource deallocation is
|
||||
* done during object destruction.
|
||||
*
|
||||
* In Java RAII is achieved with try-with-resources statement and
|
||||
* interfaces Closeable and AutoCloseable. The try-with-resources
|
||||
* statement ensures that each resource is closed at the end of the
|
||||
* statement. Any object that implements java.lang.AutoCloseable, which
|
||||
* includes all objects which implement java.io.Closeable, can be used
|
||||
* as a resource.
|
||||
*
|
||||
* In this example, SlidingDoor implements AutoCloseable and
|
||||
* TreasureChest implements Closeable. Running the example, we can
|
||||
* observe that both resources are automatically closed.
|
||||
*
|
||||
* http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args ) throws Exception {
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* SlidingDoor resource
|
||||
*
|
||||
*/
|
||||
public class SlidingDoor implements AutoCloseable {
|
||||
|
||||
public SlidingDoor() {
|
||||
|
@ -3,6 +3,11 @@ package com.iluwatar;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* TreasureChest resource
|
||||
*
|
||||
*/
|
||||
public class TreasureChest implements Closeable {
|
||||
|
||||
public TreasureChest() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user