An Example Class Diagram


Figure 19-8 shows a simple class diagram of part of an ATM system. This diagram is interesting both for what it shows and for what it does not show. Note that I have taken pains to mark all the interfaces. I consider it crucial to make sure that my readers know what classes I intend to be interfaces and which I intend to be implemented. For example, the diagram immediately tells you that WithdrawalTransaction talks to a CashDispenser interface. Clearly, some class in the system will have to implement the CashDispenser, but in this diagram, we don't care which class it is.

Figure 19-8. ATM class diagram


Note that I have not been particularly thorough in documenting the methods of the various UI interfaces. Certainly, WithdrawalUI will need more than the two methods shown there. What about PromptForAccount or InformCashDispenserEmpty? Putting those methods in the diagram would clutter it. By providing a representative batch of methods, I've given the reader the idea. That's all that's necessary.

Again note the convention of horizontal association and vertical inheritance. This helps to differentiate these vastly different kinds of relationships. Without a convention like this, it can be difficult to tease the meaning out of the tangle.

Note how I've separated the diagram into three distinct zones. The transactions and their actions are on the left, the various UI interfaces are all on the right, and the UI implementation is on the bottom. Note also that the connections between the groupings are minimal and regular. In one case, it is three associations, all pointing the same way. In the other case, it is three inheritance relationships, all merged into a single line. The groupings, and the way they are connected, help the reader to see the diagram in coherent pieces.

You should be able to see the code as you look at the diagram. Is Listing 19-1 close to what you expected for the implementation of UI?

Listing 19-1. UI.cs

public abstract class UI :   WithdrawalUI, DepositUI, TransferUI {   private Screen itsScreen;   private MessageLog itsMessageLog;   public abstract void PromptForDepositAmount();   public abstract void PromptForWithdrawalAmount();   public abstract void InformInsufficientFunds();   public abstract void PromptForEnvelope();   public abstract void PromptForTransferAmount();   public abstract void PromptForFromAccount();   public abstract void PromptForToAccount();   public void DisplayMessage(string message)   {     itsMessageLog.LogMessage(message);     itsScreen.DisplayMessage(message);   } }




Agile Principles, Patterns, and Practices in C#
Agile Principles, Patterns, and Practices in C#
ISBN: 0131857258
EAN: 2147483647
Year: 2006
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net