refactor(banking-demo): introduce DemoApplication orchestrator and add controlled lifecycle management

- Replace DemoFactory with DemoApplication to centralize system setup, execution, and shutdown
- Move ExecutorService and ScheduledExecutorService lifecycle management into application layer
- Ensure proper shutdown sequence for worker and monitoring threads
- Simplify Main class to a minimal entry-point wrapper
- Improve clarity of execution flow: build → run → shutdown
- Prevent resource leakage by explicitly awaiting termination of thread pools
This commit is contained in:
2026-06-05 20:31:08 +03:30
parent 43a53bdabf
commit 353873f10d
5 changed files with 117 additions and 83 deletions
@@ -0,0 +1,108 @@
package dev.banking;
import dev.banking.model.*;
import dev.banking.monitor.LiveMonitor;
import dev.banking.processor.TransactionProcessor;
import dev.banking.service.BankingSystem;
import java.util.*;
import java.util.concurrent.*;
/**
* DemoApplication is responsible for:
* - Building the system
* - Running the simulation
* - Managing lifecycle (threads, schedulers)
*
* This class replaces DemoFactory as a full runner.
*/
public final class DemoApplication {
private DemoApplication() {
}
public static void run() {
System.out.println("Initializing Banking Simulation...\n");
/*
* 1. Create bank accounts
*/
BankAccount acc1 = new BankAccount(1, 1000);
BankAccount acc2 = new BankAccount(2, 2000);
BankAccount acc3 = new BankAccount(3, 1500);
Map<Integer, BankAccount> accounts = new HashMap<>();
accounts.put(1, acc1);
accounts.put(2, acc2);
accounts.put(3, acc3);
/*
* 2. Create transactions
*/
List<Transaction> transactions = List.of(
new DepositTransaction(1, 200),
new WithdrawTransaction(2, 300),
new TransferTransaction(1, 2, 150),
new TransferTransaction(2, 3, 400),
new DepositTransaction(3, 500),
new WithdrawTransaction(1, 100),
new TransferTransaction(3, 1, 250)
);
/*
* 3. Thread pool (workers)
*/
ExecutorService executor = Executors.newFixedThreadPool(4);
/*
* 4. Processor
*/
TransactionProcessor processor = new TransactionProcessor(accounts);
/*
* 5. Banking system
*/
BankingSystem bankingSystem = new BankingSystem(executor, processor);
/*
* 6. Live monitor (UI simulation)
*/
LiveMonitor monitor = new LiveMonitor();
ScheduledExecutorService monitorExecutor = Executors.newSingleThreadScheduledExecutor();
monitorExecutor.scheduleAtFixedRate(() -> {
monitor.update(accounts.values());
System.out.println("----------------------");
}, 0, 1, TimeUnit.SECONDS);
/*
* 7. Run simulation
*/
bankingSystem.processTransactions(transactions);
/*
* 8. Shutdown / lifecycle management
*/
shutdown(executor, monitorExecutor);
System.out.println("\nSimulation completed.");
}
private static void shutdown(
ExecutorService executor,
ScheduledExecutorService monitorExecutor
) {
try {
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
monitorExecutor.shutdown();
monitorExecutor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
@@ -1,55 +0,0 @@
package dev.banking;
import dev.banking.service.BankingSystem;
/**
* Creates a ready-to-run demonstration environment.
*
* Students do not need to modify this class.
*/
public final class DemoFactory {
private DemoFactory() {
}
/**
* Builds a fully configured banking simulation system.
*
* This includes:
* - Bank accounts
* - Transaction stream generator / loader
* - ExecutorService (thread pool)
* - Transaction processor
* - Live monitoring system
*
* @return configured BankingSystem instance
*/
public static BankingSystem createDemoSystem() {
/*
* TODO (Instructor Implementation Only)
*
* Example responsibilities:
*
* 1. Create bank accounts:
* - Account(1, 1000)
* - Account(2, 2000)
* - Account(3, 1500)
*
* 2. Create transaction list or stream
*
* 3. Initialize ExecutorService:
* Executors.newFixedThreadPool(n)
*
* 4. Create TransactionProcessor
*
* 5. Create BankingSystem and inject dependencies
*
* 6. Attach LiveMonitor (optional)
*/
throw new UnsupportedOperationException(
"Instructor implementation required."
);
}
}
+6 -18
View File
@@ -1,17 +1,11 @@
package dev.banking;
import dev.banking.service.BankingSystem;
import dev.banking.DemoFactory;
/**
* Entry point of the application.
*
* This class is provided for demonstration purposes only.
*
* IMPORTANT:
* - Do NOT modify this file.
* - This file is NOT used during grading.
* - All grading is performed through the provided JUnit tests.
* - Do NOT modify this file in student assignment.
* - This is only a wrapper for running the demo.
*/
public final class Main {
@@ -22,25 +16,19 @@ public final class Main {
System.out.println("=================================");
System.out.println(" Advanced Banking System");
System.out.println("=================================");
System.out.println("=================================\n");
try {
BankingSystem bankingSystem =
DemoFactory.createDemoSystem();
System.out.println("System initialized...");
System.out.println("Running simulation with concurrent workers...\n");
// bankingSystem.start();
DemoApplication.run();
System.out.println("\n=================================");
System.out.println("Simulation completed successfully.");
System.out.println(" System finished successfully ");
System.out.println("=================================");
} catch (Exception e) {
System.err.println("\nUnexpected error occurred:");
System.err.println("Unexpected error occurred:");
e.printStackTrace();
}
}
@@ -31,7 +31,7 @@ public class BankAccount {
* TODO
* Implement a thread-safe deposit operation.
*/
public void deposit(int amount) {
public void deposit(long amount) {
throw new UnsupportedOperationException();
}
@@ -39,7 +39,7 @@ public class BankAccount {
* TODO
* Implement a thread-safe withdrawal operation.
*/
public void withdraw(int amount) {
public void withdraw(long amount) {
throw new UnsupportedOperationException();
}
@@ -47,7 +47,7 @@ public class BankAccount {
* TODO
* Implement an atomic and deadlock-free transfer.
*/
public void transfer(BankAccount target, int amount) {
public void transfer(BankAccount target, long amount) {
throw new UnsupportedOperationException();
}
}
@@ -1,7 +0,0 @@
package dev.banking.model;
public enum TransactionType {
DEPOSIT,
WITHDRAW,
TRANSFER
}