How to write use cases

Source: UML Distilled, Third Edition by Martin Fowler (Chapter 9: Use Cases)

Use cases are a technique for capturing the functional requirements of a system. Use cases work by describing the typical interactions between the users of a system and the system itself.

A use case is a set of scenarios tied together by a common user goal. There is no standard way to write the content of a use case, and different formats work well in different cases. The following figure shows a common style to use:

use case

Figure: Example use case text from UML Distilled by M. Fowler

We can start by writing the main success scenario as a sequence of numbered steps. Then we take the other scenarios and write them as extensions. Extensions can be successes—user achieves the goal, as in 3a—or failures, as in 6a. This structure is a great way to brainstorm alternatives to the main success scenario. For each step, we could ask, How could this go differently? and in particular, What could go wrong?

Keep in mind each step in a use case is an interaction between an actor and the system – a simple statement clearly showing 1) who is carrying out the step, 2) the intent of the actor.

A use case should not include 1) the mechanics of what the actor does, nor 2) any description of the user interface.

In the picture , the first step includes another use case “browse catalog and select items to buy.” Here underlining is used to show an included use case. There’s no standard way to show an included use case in the text. Try not to break down use cases into sub-use cases.

As well as the steps in the scenarios, you can add some other common information to a use case.

  • A pre-condition describes what the system should ensure is true before the system allows the use case to begin.
  • A guarantee describes what the system will ensure at the end of the use case. Success guarantees hold after a successful scenario; minimal guarantees hold after any scenario.
  • A trigger specifies the event that gets the use case started.

We should work hard to keep the use case brief and easy to read. Long, detailed use cases may not get read, which rather defeats the purpose.

How to use CRC cards for OO design

[Source: UML Distilled, Third Edition by Martin Fowler (Chapter 4: Sequence Diagrams)]

CRC (Class-Responsibility-Collaboration) Cards is a helpful technique to model a good object oriented design and to explore object interactions.

How to use CRC Cards

To use CRC cards, take various scenarios from your project and write them down on cards (a card can be just a piece of paper), then pick them up in any order that make sense to your team and move them to suggest how they send messages to each other and pass them around.

As you do this, you form ideas about responsibilities and write them on the cards. Thinking about responsibilities is important, because it eases the team members toward understanding the higher-level behaviour of each class.

crc

Figure: A sample CRC Card

 

Understanding the responsibilities of an object 

An important part of CRC thinking is identifying responsibilities. A responsibility is a short sentence that summarizes something that an object should do. It can be an action the object performs, some knowledge the object maintains, or some important decisions the object makes. The idea is that you should be able to take any class and summarize it with a handful of responsibilities. Doing that can help you think more clearly about the design of your classes.

Try to avoid writing down too many low-level responsibilities. The responsibilities should easily fit on one card. Ask yourself whether the class should be split or whether the responsibilities would be better stated by rolling them up into higher-level statements.

Collaboration with other classes

Here collaborators are the other classes that this class needs to work with. This gives you some idea of the links between classes— still at a high level.

Here is good online video by Simon Allardice that demonstrates how to use CRC Cards:

 

 

UML Distilled by Martin Fowler

This is a good book if you want to learn about UML.

http://www.amazon.com/UML-Distilled-Standard-Modeling-Language/dp/0321193687/ref=sr_1_1?s=books&ie=UTF8&qid=1417561005&sr=1-1&keywords=uml+distilled

Some topics in this book might be little difficult to understand at the beginning. You can also look at Learning UML 2.0 by Russ Mile and Kim Hamilton.

http://www.amazon.com/Learning-UML-2-0-Russ-Miles/dp/0596009828/ref=sr_1_1?s=books&ie=UTF8&qid=1428352111&sr=1-1&keywords=uml