Why Use Cases Are Not "Functions"

本文探讨了用例在系统设计中的重要性和常见误区,强调用例应当关注为客户创造的价值而非简单功能分解,提供了如何构建有效用例的指导。

by Kurt Bittner
General Manager
Rational Unified Process Business Unit

 

多数人从用例开始就走入了迷途,也许是用例图和数据流图的相似性导致人们把用例定义为简单的功能或者菜单项。不论原因是什么,这都是新手最容易犯的错误.


1 错误的方式:用例是菜单项或者功能

 

这幅图有什么错误?用最简单的定义,我倾向于把用例看作是关于使用系统作某些有用的事情的方式的故事。利用这个定义,是不是所有的“用例”都是独立的有用的呢?
        答案当然是不是,在这个例子中,用例表示了系统需要做的所有的事情,但是他们也描述了用户需要通过系统去做的一件单独的事情:定购。所有保留的元素都是这 一个用例的备选支流,它们是在定购时可能发生的步骤。在只做一件有用的事情的地方,只应该有一个用例。图1就是一个功能分解的例子,或者“四轮马车”格式 的例子――一个参与者在中间,周围是一圈用例。
        这是一个常见的问题,为什么人们会陷入这个陷阱呢?我们有一个有对定购的内在需要,并且在不存在的地方如果我们需要那么就利用它。在功能分解的情况下,我 们有一种自然倾向把问题分解为越来越小的块。有一种天真的想法那就是把用例分解为越来越小的单位会简化问题。这种理解是完全错误的。当我们分解用例时,实 际上会把问题复杂化。

问题在这里

        用例的目的是描述某人某物如何使用系统来完成对他们有价值的事情,它描述了系统在概念层次上做什么,使我们足够理解系统以便决定系统是否要做恰当的事。用例是我们能够形成一个系统的概念模型。
        再看看图1,现在问你自己,如果我从来没有定购过,我想通过系统查询定单的状态吗?这是不太可能的。或者如果我从来没有定购过,我想变更订单吗?不,也许不。通常这些东西只有当我订购过的时候才对我有用。然而,为了系统能够允许我订购,这些都是必要的。
        把系统分解为越来越小的用例模糊了系统的真实目的,极端情况下,我们将以得到一堆隔绝的行为的细致末节而告终。结果我们不能说明系统做什么。这就像看到一 辆被拆解为零件的汽车,或许你会说这是辆汽车,你也知道零件们是有用的,但是你确实不能说明他们如何组装在一起。
        当使用用例的时候,记住用例是考虑整个系统的一种方式,并且要组织成可管理的功能块,这些功能块完成一些有价值的事情。为了得到正确的用例集合,问你自己一个问题:“参与者实际上想使用系统做什么?”
如果你在想图1的改进后的版本是什么样的,图2展示了改进后的版本。
 
图 2一种更好,更加简单的方法: 合并功能以反映对参与者的真正价值
            这一个用例包含了以前的图中所分解为用例的所有“功能”。你也许会问为什么这样做比较好,答案很简单:它关注于客户想要系统提供的价值,而不是我们在系统 内部如何细分和构造的。如果把所有的功能分解成单独的用例,你将迫使客户(为系统付钱的人)把分解过的用例装配成一件对他们有意义的东西,以便理解系统所 描述的是不是他们所想要的(即他们想为之付钱的)。

关注价值

        很多小用例是常见的问题,尤其是在一个习惯于功能分解的团队中,他们的用例名称读起来就象是一个系统所执行的功能的列表,如“输入订单”、“审查订单”、 “取消订单”、“履行订单”,这起先听起来也不是很坏,但不仅仅这些。即使对于一个小规模的订购系统,用例列表也很容易达到上百个,如果谁走到了这一步, 他们不久就会淹死在用例的海洋中,尤其是当系统规模确实比较大时,在这种情况下,你最终也许会有几百个,甚至上前个用例。

这么做有什么错呢?

        这些用例的价值将会被错过。用例的唯一目的是为参与者产生价值。在一定层次上能够输入订单也是某种有价值的事情,但是,如果这些订单从来不被履行,那还有价值吗?也许没有吧。
怎样输入订单、修改订单以及取消订单等等,所有这些事情都是与客户真正想要做的事情有关的,那就是接受订购的货物。这些活动对公司想要的事情是必须的,那就是收到货款。

        这种看起来是支离破碎的没有任何明显关系的功能集合的另一个问题是导致难以使用的系统。很多系统跟这个很类似,它们只是一堆杂乱的特性。记住,用例帮助我 们关注与什么是真正重要的,也就是具有真正价值的事物,并且使我们能够围绕那些元素来定义系统。用例不要是系统按照功能分解的蓝图。

举例

        考虑一个你在互联网上用过的一个电子商务系统,当你登录这个站点的时候,你的目标可能是查找产品相关的信息、选择要购买的产品、安排支付及送货约定。在做 这些事的过程当中,你可能会改变主意、输入错误的信息以至不得不修改它、改变邮递或送货地址,以及其他的一些东西。如果这个网站不允许你通过一种吸引人的 方式来找到并订购产品,你也许不会完成你的订购,更不用说再次来到这个网站。

        当建造一个系统时,始终要记住用例的核心定义:关于采用某种方式使用系统来做有价值的事情的故事。如果你能够实施这个定义,展示用户希望通过系统获得的价值,然后创建反映这些价值的用例的时候,你的系统将会更好地满足用户的期望。

用中文解答 1. 2. 3. 4. 5. 6. 7. 8. 9. What is a function in C++? Name the three core benefits of using functions in a program. What is the difference between a function declaration (prototype) and a function definition? Provide examples. What is a function signature? Which elements are part of the signature, and which are not? What methods of passing parameters to a function do you know? Explain the difference between pass-by-value, pass-by-pointer, and pass-by-reference. Why can't you pass an array to a function by value? What is the correct way to pass an array to a function? What is variable scope? How is it related to functions? How does a function return a value? What happens if a function with a non-void return type does not return a value on all control paths? Can you use multiple return statements in a single function? Provide an example. What is function overloading? What is it based on? 10. How is interaction between functions organized in a program? Provide an example program with several functions. 11. What are default parameters? How are they specified, and in what cases are they useful? 12. How can you prevent a function from modifying the data passed to it? What modifiers are used for this? 13. What is recursion? Provide an example of a recursive function. 14. What common errors occur when working with functions? How can they be avoided? 15. How do you use pointers to functions? Provide an example of declaring and calling a function through a pointer
最新发布
12-13
1. What is an IDE (Integrated Development Environment), and what are its main components? 2. What is the role of a compiler in the C++ development process? 3. What is the difference between source code (e.g., a .cpp file) and an executable file? 4. In the "Hello, World!" program, what is the purpose of the line #include <iostream>? 5. What is special about the main() function in a C++ program? 6. Why do computers fundamentally operate using the binary (base-2) system? 7. What is the base of the hexadecimal system? Why is it often used by programmers as a shorthand for binary numbers? 8. Explain the "triad" method for converting an octal number to binary. 9. Briefly describe the "division by 2" method for converting a decimal number to binary. 10. What is the decimal value of the binary number 1011? 1. What is the purpose of the std::cout object? Which header file must be included to use it? 2.What is the difference between an escape sequence like \n and a manipulator like std::endl? (Hint: Both create a new line, but they have a subtle difference). 3.How would you print the following text to the console, including the quotes and the backslash: He said: "The file is in C:\Users\"? 4.Is it possible to write an entire multi-line text output using only one std::cout statement? If yes, how? 5.What is a syntax error? Give an example of a syntax error from Task 2. (Task 2: Debugging The following program contains several syntax errors. Copy the code into your IDE, identify the errors, fix them, and run the program to ensure it works correctly. Incorrect Code: */ Now you should not forget your glasses // #include <stream> int main { cout << "If this text" , cout >> " appears on your display, cout << " endl;" cout << 'you can pat yourself on ' << " the back!" << endl. return 0; "; ) Hint: Pay close attention to comments, header files, brackets ({}), operators (<<), semicolons, and how strings and manipulators are written.) 1. What is the difference between variable declaration and initialization? 2.What will be the result of the expression 7 / 2 in C++? Why? 3.What will be the result of the expression 10 % 3? What is the main purpose of the modulus operator? 4. What is the purpose of std::cin and the >> operator? 5. A beginner tries to swap two integer variables a and b with the code a = b; b = a;. Why will this not work correctly? 1. What is an algorithm? Name the primary ways to represent an algorithm. 2.List the main flowchart symbols and explain their purpose. 3.What are the three fundamental types of algorithm structures? Briefly describe each. 4.In a branching algorithm, what determines the flow of execution? 5.What is the key characteristic of a linear algorithm? 6.When is a cyclic algorithm structure used?7. 8. 9. 7.Explain the purpose of a connector in a flowchart. 8.What is the difference between a predefined process block and a standard process block? 9.In the context of solving a quadratic equation algorithm, what condition must be checked before calculating the roots? Why? 1. What are the three main approaches to data input and output offered by C++? 2. What is the purpose of the SetConsoleOutputCP(65001) and SetConsoleCP(65001)
functions in the provided C++ program example? 3. Explain the difference between the cin and cout objects in Stream 1/0. 4. When using formatted 1/0, which header file must be included to use manipulators like setw and setprecision? 5. List three manipulators used for data output in C++ and briefly describe what each one does. 6. In Formatted I/0 using printf), what are the conversion specifications for a decimal integer and a real number in exponential form? 7. What is the difference in how the & (address-of) operator is used when inputting a value for an integer variable versus a string variable using the scanf() function? 8. Which Character I/O function is used to output a single character to the screen, and which is used to output a string? 9. Describe the syntax and function of the ternary operator in C++. 10. What is the difference between the logical AND (&&) and logical OR (I|) operators when combining multiple conditions? 11. When is the default label executed in a C++ switch statement? 12. What is the primary purpose of the break statement within a switch block? 1. What is the main purpose of using loops in programming? 2. Explain the key difference between the for, while, and do while loops. 3. What happens if you forget to include the increment/decrement statement in a while loop? 4. How can you interrupt an infinite loop during program execution? 5. What is the role of the setw() and setfill) manipulators in C++? 6. In a nested loop, how does the inner loop behave relative to the outer loop? 7. What is type casting, and why is it used in loop calculations? 8. How does the do while loop differ from the while loop in terms of condition checking? 9. What output formatting options can be used to align numerical results in columns? 10*. How would you modify a loop to skip certain iterations based on a condition? 1. List the six main biwise operators in C++ and explain the function of each. 2. Why cannot bitwise operations be applied to variables of floating-point type? 3. Explain the purpose of the << (left shift) and >> (right shift) operators. What is the typical effect on the decimal value of a number when it is shifted left by 1? Shifted right by 1? 4. Describe the process of using a mask to check the value of a specific bit within an
integer. 5. How can you use the bitwise AND operator (&) to check if a number is even or odd?
Explain the logic. 6. What is the difference between the logical AND (&&) and the bitwise AND (&)? Provide an example scenario for each. 7. Explain the purpose of the ~ (bitwise NOT) operator. What is the result of applying it to a mask, and how can this be useful? 1. What is the primary goal of program debugging? What types of errors can it help identify? 2. Describe the difference between Step Over (F10) and Step Into (F11) debugging commands. When would you choose one over the other? 3. What is the purpose of a breakpoint in planned debugging? How do you set and remove a breakpoint in Visual Studio? 4. Explain the utility of the "Watch" window compared to the "Autos" or "Locals" windows during a debugging session. 5. What is the key difference between the Debug and Release configurations when building a project? Why is it necessary to create a Release version after successful debugging? 6. List at least three types of files commonly found in a project's Debug folder and briefly state their purpose (e.g., *.pdb). 7. During debugging, you notice a variable has an incorrect value. How can you change its value during runtime to test a hypothesis without modifying the source code? 8. What command is used to exit the debug mode and stop the current debugging session? 1. What is an array in C++? List its three main characteristics. 2. How are array elements numbered in C++? What is the valid index range for an array declared as int data[25];? 3. Explain the difference between array declaration and initialization. Provide an example of each. 4. What is an initializer list? What happens if the initializer list is shorter than the array size? 5. How can you let the compiler automatically determine the size of an array during initialization? 6. What values do elements of a local array contain if it is declared but not explicitly initialized? How does this differ from a global array? 7. What is an array out-of-bounds error? Why is it dangerous, and what are its potential consequences? 8. How do you calculate the number of elements in an array using the sizeof operator?
Provide the formula. What is a significant limitation of this method? 9. Why is it impossible to copy the contents of one array into another using the assignment
operator (arrayB = arrayA;)? What is the correct way to perform this operation? 10. Why does comparing two arrays using the equality operator (arrayA == arrayB) not check if their elements are equal? How should array comparison be done correctly? 11. What does the name of an array represent in terms of memory? 1. What is a pointer in C++ and what are its two main attributes? 2. Explain the difference between the & and * operators when working with pointers. 3. Why is pointer initialization critical and what dangers do uninitialized pointers pose? 4. What is the fundamental relationship between arrays and pointers in C++? 5. How does pointer arithmetic work and why does ptr + 1 advance by the size of the pointed type rather than 1 byte? 6. What is the difference between an array name and a pointer variable? Why can't you increment an array name? 7. What are the differences between const int*, int* const, and const int* const? 8. How can you safely iterate through an array using pointers, and what are the boundary risks? 9. What is a null pointer and why should you check for nullptr before dereferencing? 10. How do you access array elements using pointer syntax, and how does the compiler translate arr[i] internally? 1. What is a multidimensional array? How is a two-dimensional array structured in memory? 2. Explain the concept of an "array of arrays". How does this relate to the declaration int arr/ROWS//COLS;? 3. The name of a two-dimensional array without indices is a pointer constant. What does this pointer point to? What do the expressions *(A + i) and *(*(A + i) +j) mean for a two-dimensional array A? 4. Describe the different ways to access the element A/1/[2/ of a two-dimensional array
using pointers. 5. What is the rule for omitting the size of dimensions when initializing and when passing a multidimensional array to a function? Why is it allowed to omit only the first dimension? 6. Explain the principle of "row-major order" for storing two-dimensional arrays in memory.
How does this affect element access? 7. Why are nested loops the standard tool for processing multidimensional arrays?
Describe the typical pattern for iterating through a matrix. 1. How is a character string stored in memory in C++? What is the role of the null terminator (10), and why is it critical for C-style strings? 2. Why must the size of a char array declared to hold a string be at least one greater than the number of characters you intend to store? 3. The array name without an index is a pointer constant. What does the name of a char array point to? 4. What are the two main ways to initialize a C-style string? What is a common mistake when using the initializer list method, and what is its consequence? 5. Why is it necessary to add _CRT_SECURE_NO_WARNINGS to the preprocessor definitions in Visual Studio when working with many standard C library functions?
What is the alternative approach? 6. What is the key difference between stropy and strncpy? Why might strncpy be considered safer? 7. How does the stremp function determine if one string is "less than" another? Why can't you use the == operator to compare two C-style strings for content equality? 8. Describe the purpose and parameters of the strok function. How do you get all tokens from a string? 9. What do the functions strchr and strrchr do? How do they differ? 10. Explain what the strstr function returns and what it is commonly used for. 11. What is the purpose of the functions in the < cctype> header? Give three examples of such functions and their use. 12. What is the difference between tolower(c) and_tolower(c)? When should you use each? 1. What is a function in C++? Name the three core benefits of using functions in a program. 2. What is the difference between a function declaration (prototype) and a function definition? Provide examples. 3. What is a function signature? Which elements are part of the signature, and which are not? 4. What methods of passing parameters to a function do you know? Explain the difference between pass-by-value, pass-by-pointer, and pass-by-reference. 5. Why can't you pass an array to a function by value? What is the correct way to pass an array to a function? 6. What is variable scope? How is it related to functions? 7. How does a function return a value? What happens if a function with a non-void return type does not return a value on all control paths? 8. Can you use multiple return statements in a single function? Provide an example. 9. What is function overloading? What is it based on? 10. How is interaction between functions organized in a program? Provide an example program with several functions. 11. What are default parameters? How are they specified, and in what cases are they useful? 12. How can you prevent a function from modifying the data passed to it? What modifiers are used for this? 13. What is recursion? Provide an example of a recursive function. 14. What common errors occur when working with functions? How can they be avoided? 15. How do you use pointers to functions? Provide an example of declaring and calling a function through a pointer. 用中文解答
11-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值