ITADN

Improve ATM extensibility by replacing fixed properties with a dynamic list

#130Pull Requestsookim-1 创建于 2025-02-04
S
sookim-1commented
- [X] Read [CONTRIBUTING.md](https://github.com/ochococo/Design-Patterns-In-Swift/blob/master/CONTRIBUTING.md]) - [X] Only edited files inside `source` folder (IMPORTANT) and commited them with a meaningful message - [X] Ran `generate-playground.sh`, no errors - [X] Opened playground, it worked fine - [X] Did not commit the changes caused by `generate-playground.sh` - [ ] Linked to and/or created issue - [X] Added a description to PR ## 📌 Summary This PR enhances the flexibility of the `ATM` class by replacing its fixed currency properties (`hundred`, `fifty`, `twenty`, `ten`) with a dynamic `moneyPileList` array. ## 🔍 Problem - The original `ATM` implementation had hardcoded properties for each denomination (`hundred`, `fifty`, `twenty`, `ten`). - Adding a new denomination required modifying the `ATM` class. - The withdrawal process was fixed to start from `hundred`, making it difficult to change the order dynamically. ## ✅ Solution - Introduced a `moneyPileList` array to hold `MoneyPile` instances. - `ATM` now starts the withdrawal process from the first element of `moneyPileList`, allowing for a flexible chain configuration. - Now, adding new denominations or modifying the withdrawal order requires **no changes to the `ATM` class**, improving scalability. ## 🛠 Changes - Replaced fixed `hundred, fifty, twenty, ten` properties with `moneyPileList: [Withdrawing]`. - The withdrawal process starts from `moneyPileList.first!`, making it adaptable. ## 🔬 Example Usage ```swift let five = MoneyPile(value: 5, quantity: 10, next: nil) let ten = MoneyPile(value: 10, quantity: 6, next: five) let twenty = MoneyPile(value: 20, quantity: 2, next: ten) let atm = ATM(moneyPileList: [twenty, ten, five]) // Customizable order! atm.withdraw(amount: 25) // Can withdraw using 1x20 + 1x5 ``` ## 🚀 Benefits - More maintainable: No need to modify ATM for new denominations. - More flexible: Users can configure withdrawal order dynamically. - Backward-compatible: Works with existing MoneyPile implementation. Would love to hear any feedback! 😊
合并状态:未合并 关闭于 2025-02-04 0 条评论