File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed
resource-acquisition-is-initialization/src/main/java/com/iluwatar Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Resource Acquisition Is Initialization pattern was developed
6+ * for exception safe resource management by C++ creator Bjarne
7+ * Stroustrup.
8+ *
9+ * In RAII resource is tied to object lifetime: resource allocation
10+ * is done during object creation while resource deallocation is
11+ * done during object destruction.
12+ *
13+ * In Java RAII is achieved with try-with-resources statement and
14+ * interfaces Closeable and AutoCloseable. The try-with-resources
15+ * statement ensures that each resource is closed at the end of the
16+ * statement. Any object that implements java.lang.AutoCloseable, which
17+ * includes all objects which implement java.io.Closeable, can be used
18+ * as a resource.
19+ *
20+ * In this example, SlidingDoor implements AutoCloseable and
21+ * TreasureChest implements Closeable. Running the example, we can
22+ * observe that both resources are automatically closed.
23+ *
24+ * http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html
25+ *
26+ */
327public class App {
428
529 public static void main ( String [] args ) throws Exception {
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * SlidingDoor resource
6+ *
7+ */
38public class SlidingDoor implements AutoCloseable {
49
510 public SlidingDoor () {
Original file line number Diff line number Diff line change 33import java .io .Closeable ;
44import java .io .IOException ;
55
6+ /**
7+ *
8+ * TreasureChest resource
9+ *
10+ */
611public class TreasureChest implements Closeable {
712
813 public TreasureChest () {
You can’t perform that action at this time.
0 commit comments