Banking Transaction System
Apply Factory Method and Observer inside one domain model - creation variability and state-change notification, integrated properly.
advanced3-4 hours
What you will produce
- Factory Method UML
- Observer UML
- Integrated class diagram
- Pseudocode
Meridian Community Bank has hired you to redesign the core of its transaction engine. The bank processes four kinds of transactions - deposits, withdrawals, transfers, and standing orders - and each kind carries its own validation rules and fee schedule. A withdrawal must respect daily limits; a transfer must verify both accounts; a standing order must check its schedule. Today all of this lives in one 900-line method full of nested conditionals, and every new transaction kind means editing that method and re-testing everything around it.
There is a second problem. Compliance requires that the fraud-monitoring system and the audit log see every completed transaction, and marketing wants customers notified in near real time. Right now those systems poll the database on timers, which is slow, wasteful, and occasionally misses records.
This project asks you to solve both problems with two classic patterns - Factory Method for the creation variability, Observer for the completion notifications - and then, the harder part, to integrate them into one coherent class diagram without letting either pattern smear responsibilities across the design.
Before you start
You should be comfortable with:
- Class diagrams and generalization notation
- The Factory Method and Observer patterns individually
- The Open-Closed Principle, which motivates both patterns here
Open the observer-pattern starter example in DiaGO Studio - you will extend it in Step 4. Steps 1, 3, 6, and 7 are paper-or-text work; Steps 2, 4, and 5 happen in Studio.
Step 1: Identify the creation variability
On paper, list every transaction kind and what actually differs between them. All four share the same lifecycle - validate, execute, apply fee, complete - so the axis of variation is not what happens but how validation and fees behave for each kind. That is exactly the shape of problem Factory Method targets: a stable process that needs to work with a family of objects whose concrete type is decided at runtime.
Complete this table (two rows are done for you):
| Kind | Kind-specific validation | Fee rule |
|---|---|---|
| Deposit | Amount positive; target account not frozen | No fee |
| Withdrawal | Sufficient balance; daily withdrawal limit not exceeded | Flat fee above a threshold |
| Transfer | (you fill in) | (you fill in) |
| Standing order | (you fill in) | (you fill in) |
Hint: if a rule applies to every kind (amount must be positive, account must exist), it belongs to the shared lifecycle, not to a concrete class. Only genuine variation earns a subclass.
Step 2: Apply Factory Method
In Studio, model the Factory Method side:
- A
Transactioninterface withvalidate(),execute(), andcalculateFee(). - Four concrete classes -
Deposit,Withdrawal,Transfer,StandingOrder- connected to the interface with realization edges. - A
TransactionProcessorcreator that declares the factory methodcreateTransaction(request)and owns the shared lifecycle in aprocess(request)method.
If the bank later needs specialized processors (say, a BranchProcessor versus an OnlineProcessor that create different transaction variants), those subclass the creator via generalization edges. The reasoning: process is written once against the Transaction interface, so adding a fifth kind means adding a class, not editing the lifecycle.
abstract class TransactionProcessor {
protected abstract createTransaction(req: TransactionRequest): Transaction;
process(req: TransactionRequest): Receipt {
const txn = this.createTransaction(req); // the factory method
txn.validate();
txn.execute();
return txn.complete(txn.calculateFee());
}
}Step 3: Identify the state-change subscribers
Back to paper. Three systems must react when a transaction completes: FraudMonitor, NotificationService, and AuditLog. Before reaching for Observer, articulate why the current polling approach is worse: each poller adds database load, introduces latency equal to its polling interval, and can miss records if its query window is wrong. Worst of all, the pollers couple their schedules to the transaction table's schema - a change there breaks three other teams.
Fill in the missing row:
| Subscriber | Reacts how | Why a missed event is unacceptable |
|---|---|---|
| FraudMonitor | Scores the transaction against fraud patterns | A missed event is an unscored, possibly fraudulent transaction |
| AuditLog | Appends an immutable record | Regulators require a complete trail |
| NotificationService | (you fill in) | (you fill in) |
Step 4: Apply Observer
Open the observer-pattern starter in Studio and reshape it toward the banking domain, following the structure in the Observer reference:
- Rename the observer interface to
TransactionObserverwith a single operationonCompleted(txn: Transaction). - Add the three concrete observers from Step 3, each realizing the interface.
- The subject holds a collection of observers and exposes
attach,detach, and anotifyCompletedoperation that iterates the collection.
Hint: decide whether observers receive the whole Transaction (push model) or a lightweight event they can query (pull model). Push is simpler here; note your choice on the diagram.
Step 5: Integrate both patterns into one class diagram
Now merge the two diagrams: TransactionProcessor.process creates a transaction via the factory method and calls notifyCompleted at the end. The pragmatic move is to make TransactionProcessor the subject itself.
In Studio, connect the processor to the observer interface with an association (it holds the collection) and verify the issues badge in the status bar reports no errors.
Step 6: Write pseudocode for the processing flow
In a text file, trace one request end to end:
receive TransactionRequest
txn = createTransaction(request) // Factory Method dispatches on kind
if txn.validate() fails:
reject request, stop
txn.execute()
fee = txn.calculateFee()
mark txn completed
for each observer in observers: // Observer fan-out
observer.onCompleted(txn)
return receiptHint: note where a thrown exception in one observer could block the others - real systems isolate observer failures.
Step 7: Explain benefits and trade-offs honestly
Write a short paragraph comparing your design to the naive alternative:
| Concern | Switch statement + callback list | Factory Method + Observer |
|---|---|---|
| Adding a transaction kind | Edit and re-test the central method | Add one class |
| Adding a subscriber | Append a callback | Add one class |
| Class count | 2-3 classes | 10+ classes |
| Indirection | Minimal | Two extra hops per request |
Be honest: for a small bank with four frozen kinds and one downstream consumer, the switch plus a callback list is less code and easier to read. The patterns pay rent only when kinds and subscribers evolve independently.
Reach for it
- Transaction kinds are expected to grow or vary by deployment
- Several independent systems must react to completions without coupling to each other
- Kind-specific validation and fee logic is tangling shared conditionals
Avoid it
- The set of kinds is small and frozen and one team owns all the code
- Only one downstream system will ever care about completions
- You are prototyping and the extra classes slow the team down
Check your work
- Does Studio's Issues tab show zero errors on the integrated diagram?
- Is every observer reachable from the
notifyCompletedcall - no orphaned subscriber classes? - Do all four concrete transactions connect to
Transactionwith realization edges, not generalization? - Does the shared lifecycle live in exactly one place, written against the interface?
- Can you add a fifth transaction kind without editing
process? - Did you record push versus pull for the observer interface, and does the diagram match?
- Does your Step 7 paragraph name at least one real cost of each pattern?
Going further
- Async delivery. Replace synchronous notification with a queue and note what ordering guarantees each observer loses - a useful warm-up for microservices trade-offs.
- Persist the trail. Design the tables behind
AuditLogusing ER modeling, deciding whether transaction kind is a column or a table per kind. - Filtered subscriptions. Let observers subscribe to specific kinds only. Where does the filtering logic belong - subject, observer, or a new class?