面向对象设计:ATM案例探讨接口隔离原则与设计模式

需积分: 17 4 下载量 4 浏览量 更新于2024-07-11 收藏 5.4MB PPT 举报
在面向对象编程的世界里,自动取款机(ATM)是一个经典的案例,展示了设计模式的应用。本文主要关注于设计模式中的接口隔离原则(Interface Segregation Principle,ISP),它强调在设计系统时,应确保客户端无需依赖它们实际上并不使用的接口。 首先,设计模式的起源可以追溯到建筑大师Christopher Alexander的作品,他提倡通过模式来描述和解决问题。而在软件开发中,Gang of Four(GoF,Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides)在1995年的《设计模式:可复用面向对象软件元素》一书中,将这一概念引入并定义了设计模式。设计模式是一种通用的解决方案,用于解决软件开发中反复出现的问题,使得代码复用成为可能,提高开发效率。 考虑ATM的设计,抽象基类作为核心,用于实现一个灵活的用户界面,这体现了单一职责原则。每个操作,如存款(DepositTransaction)、取款(WithdrawTransaction)和转账(TransferTransaction),被封装为独立的类,这些类继承自交易(Transaction)基类,并调用公共的UI方法来展示或传递信息。这样的设计确保了每个交易类只关注自身特定的功能,避免了不必要的耦合。 然而,当设计多个交易类时,可能会涉及到接口隔离问题。ISP要求我们只提供客户所需的功能,防止过度设计导致接口过于庞大。例如,如果一个ATM只需要支持现金交易,那么就不应该强迫它包含语音交互的接口,即使这个接口在其他类型的ATM中是有用的。通过遵循ISP,我们可以创建更简洁、易于维护的接口,减少潜在的维护成本。 设计模式的实践不仅限于具体的技术栈,比如与Java、C#等编程语言,或者与组件技术(如CORBA)、系统结构以及泛型编程的集成。设计模式在任何面向对象的软件开发环境中都能发挥作用,帮助开发者在快速变化的软件世界中找到既高效又可复用的解决方案。 总结来说,ATM设计案例展示了如何运用设计模式,特别是接口隔离原则,来提升软件的灵活性、可维护性和复用性。通过理解并遵循设计模式的基本原则,开发者可以创建出更加模块化、适应性强的软件架构,使设计更具可扩展性和可读性。
2010-12-11 上传
面有两个帐号: 帐号:1 PIN:42 帐号:2 PIN:1234 原文是如下: To run the simulation above, you need to do the following: 1.Click on the "ON" button (lower right-hand corner) to turn the ATM on. 2.Enter the number of $20 bills you want to have be in the cash dispenser at the start of the simulation when you are prompted to do so, and press RETURN 3.Perform any number of sessions, as follows: 1.Click on the "Click to insert card" button to simulate inserting a card 2.Type in the card number when you are prompted to do so (see below), and press RETURN 3.Enter the PIN associated with the card (see below). Although you can use your regular keyboard, it's more fun to click on the keys on the simulated ATM keyboard as displayed. 4.Perform any number of transactions, using your mouse to click the keys on the simulated ATM keyboard. Note that the machine will simulate ejecting your card when you indicate you do not wish to perform any more transactions (unless, of course, your card is retained due to too many invalid PINs). 4.Turn off the ATM by clicking on the "OFF" button (same position as the "ON" button". Note that you cannot turn the ATM off while in the middle of a customer session. 5.The entire simulation may be repeated as many times as you want, by turning the machine ON again. For demonstration purposes, this program uses a very simple simulation of the bank, with hardwired card numbers and PIN's, etc. The following are the available cards: Card Number PIN Available accounts 1 42 Checking account #1, Savings account #2 2 1234 Checking account #1, Money market account #3 (Note that both cards link to the same checking account) All the features of the bank work - both current and available balances (initially the same) are maintained for each account and a $300 daily withdrawal limit per card is enforced.