diff --git a/object-pool/README.md b/object-pool/README.md index 543c42101..623e7999c 100644 --- a/object-pool/README.md +++ b/object-pool/README.md @@ -21,9 +21,9 @@ The Object Pool design pattern manages a pool of reusable objects, optimizing re ## Explanation -Real world example +Real-world example -> In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they are extremely expensive to create. The solution is to create a pool of them, track which ones are in-use, and instead of disposing them re-use the instances. +> Imagine a library with a limited number of study rooms that are frequently in demand. Instead of each student building their own study room whenever they need one, the library manages a pool of available study rooms. When a student needs a study room, they check one out from the pool. After they are done, they return the room back to the pool for others to use. This ensures that the study rooms are efficiently utilized without the need to build new rooms each time, thus saving time and resources, similar to how the Object Pool pattern manages the reuse of expensive objects in software. In plain words @@ -35,32 +35,31 @@ Wikipedia says **Programmatic Example** +In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they are extremely expensive to create. The solution is to create a pool of them, track which ones are in-use, and instead of disposing them re-use the instances. + Here's the basic `Oliphaunt` class. These giants are very expensive to create. ```java public class Oliphaunt { - private static final AtomicInteger counter = new AtomicInteger(0); + private static final AtomicInteger counter = new AtomicInteger(0); - private final int id; + @Getter + private final int id; - public Oliphaunt() { - id = counter.incrementAndGet(); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); + public Oliphaunt() { + id = counter.incrementAndGet(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + LOGGER.error("Error occurred: ", e); + } } - } - public int getId() { - return id; - } - - @Override - public String toString() { - return String.format("Oliphaunt id=%d", id); - } + @Override + public String toString() { + return String.format("Oliphaunt id=%d", id); + } } ``` @@ -107,37 +106,49 @@ public class OliphauntPool extends ObjectPool { Finally, here's how we utilize the pool. ```java +public static void main(String[] args) { var pool = new OliphauntPool(); + LOGGER.info(pool.toString()); var oliphaunt1 = pool.checkOut(); + String checkedOut = "Checked out {}"; + + LOGGER.info(checkedOut, oliphaunt1); + LOGGER.info(pool.toString()); var oliphaunt2 = pool.checkOut(); + LOGGER.info(checkedOut, oliphaunt2); var oliphaunt3 = pool.checkOut(); + LOGGER.info(checkedOut, oliphaunt3); + LOGGER.info(pool.toString()); + LOGGER.info("Checking in {}", oliphaunt1); pool.checkIn(oliphaunt1); + LOGGER.info("Checking in {}", oliphaunt2); pool.checkIn(oliphaunt2); + LOGGER.info(pool.toString()); var oliphaunt4 = pool.checkOut(); + LOGGER.info(checkedOut, oliphaunt4); var oliphaunt5 = pool.checkOut(); + LOGGER.info(checkedOut, oliphaunt5); + LOGGER.info(pool.toString()); +} ``` Program output: ``` -Pool available=0 inUse=0 -Checked out Oliphaunt id=1 -Pool available=0 inUse=1 -Checked out Oliphaunt id=2 -Checked out Oliphaunt id=3 -Pool available=0 inUse=3 -Checking in Oliphaunt id=1 -Checking in Oliphaunt id=2 -Pool available=2 inUse=1 -Checked out Oliphaunt id=2 -Checked out Oliphaunt id=1 -Pool available=0 inUse=3 +21:21:55.126 [main] INFO com.iluwatar.object.pool.App -- Pool available=0 inUse=0 +21:21:56.130 [main] INFO com.iluwatar.object.pool.App -- Checked out Oliphaunt id=1 +21:21:56.132 [main] INFO com.iluwatar.object.pool.App -- Pool available=0 inUse=1 +21:21:57.137 [main] INFO com.iluwatar.object.pool.App -- Checked out Oliphaunt id=2 +21:21:58.143 [main] INFO com.iluwatar.object.pool.App -- Checked out Oliphaunt id=3 +21:21:58.145 [main] INFO com.iluwatar.object.pool.App -- Pool available=0 inUse=3 +21:21:58.145 [main] INFO com.iluwatar.object.pool.App -- Checking in Oliphaunt id=1 +21:21:58.145 [main] INFO com.iluwatar.object.pool.App -- Checking in Oliphaunt id=2 +21:21:58.146 [main] INFO com.iluwatar.object.pool.App -- Pool available=2 inUse=1 +21:21:58.146 [main] INFO com.iluwatar.object.pool.App -- Checked out Oliphaunt id=2 +21:21:58.146 [main] INFO com.iluwatar.object.pool.App -- Checked out Oliphaunt id=1 +21:21:58.147 [main] INFO com.iluwatar.object.pool.App -- Pool available=0 inUse=3 ``` -## Class diagram - -![Object Pool](./etc/object-pool.png "Object Pool") - ## Applicability Use the Object Pool pattern when @@ -170,9 +181,9 @@ Trade-offs: ## Related Patterns -[Singleton](https://java-design-patterns.com/patterns/singleton/): Ensures a single instance of the pool is used, providing a global point of access. -[Flyweight](https://java-design-patterns.com/patterns/flyweight/): Shares fine-grained objects to reduce memory usage, complementing object pooling by managing object state efficiently. -[Factory Method](https://java-design-patterns.com/patterns/factory-method/): Often used to create objects within the pool, abstracting the instantiation process. +* [Singleton](https://java-design-patterns.com/patterns/singleton/): Ensures a single instance of the pool is used, providing a global point of access. +* [Flyweight](https://java-design-patterns.com/patterns/flyweight/): Shares fine-grained objects to reduce memory usage, complementing object pooling by managing object state efficiently. +* [Factory Method](https://java-design-patterns.com/patterns/factory-method/): Often used to create objects within the pool, abstracting the instantiation process. ## Credits diff --git a/object-pool/src/main/java/com/iluwatar/object/pool/Oliphaunt.java b/object-pool/src/main/java/com/iluwatar/object/pool/Oliphaunt.java index f882e26ba..6d7f5ce6d 100644 --- a/object-pool/src/main/java/com/iluwatar/object/pool/Oliphaunt.java +++ b/object-pool/src/main/java/com/iluwatar/object/pool/Oliphaunt.java @@ -25,6 +25,7 @@ package com.iluwatar.object.pool; import java.util.concurrent.atomic.AtomicInteger; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; /** @@ -35,6 +36,7 @@ public class Oliphaunt { private static final AtomicInteger counter = new AtomicInteger(0); + @Getter private final int id; /** @@ -49,10 +51,6 @@ public class Oliphaunt { } } - public int getId() { - return id; - } - @Override public String toString() { return String.format("Oliphaunt id=%d", id);