mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
Merge pull request #29465 from wxy/20210204-How-to-implement-business-requirements-in-software-development
RP:published/20210204 How to implement business requirements in software development.md
This commit is contained in:
commit
4de6686e94
@ -1,29 +1,32 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (toknow-gh)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-15860-1.html)
|
||||
[#]: subject: (How to implement business requirements in software development)
|
||||
[#]: via: (https://opensource.com/article/21/2/exceptional-behavior)
|
||||
[#]: author: (Alex Bunardzic https://opensource.com/users/alex-bunardzic)
|
||||
|
||||
ZOMBIES:如何在软件开发中实现业务需求(四)
|
||||
======
|
||||
完善你的电商 app,使它能够正确处理业务规则。
|
||||
![Working on a team, busy worklife][1]
|
||||
|
||||
> 完善你的电商应用,使它能够正确处理业务规则。
|
||||
|
||||
![][0]
|
||||
|
||||
在前面的文章中,我已经解释了为什么将编程问题看作一整群丧尸来处理是错误的。我用 ZOMBIES 方法来解释为什么循序渐进地处理问题更好。
|
||||
|
||||
ZOMBIES 表示以下首字母缩写:
|
||||
|
||||
**Z** – 最简场景(Zero)
|
||||
**O** – 单元素场景(One)
|
||||
**M** – 多元素场景(Many or more complex)
|
||||
**B** – 边界行为(Boundary behaviors)
|
||||
**I** – 接口定义(Interface definition)
|
||||
**E** – 处理特殊行为(Exercise exceptional behavior)
|
||||
**S** – 简单场景用简单的解决方案(Simple scenarios, simple solutions)
|
||||
- **Z** – 最简场景(Zero)
|
||||
- **O** – 单元素场景(One)
|
||||
- **M** – 多元素场景(Many or more complex)
|
||||
- **B** – 边界行为(Boundary behaviors)
|
||||
- **I** – 接口定义(Interface definition)
|
||||
- **E** – 处理特殊行为(Exercise exceptional behavior)
|
||||
- **S** – 简单场景用简单的解决方案(Simple scenarios, simple solutions)
|
||||
|
||||
在系列的前三篇文章中,我展示了 ZOMBIES 方法的前五项。第一篇中 [实现了最简场景][2],它为代码提供了最简可行路径。第二篇文章中执行了 [单元素场景和多元素场景上的测试][3]。第三篇中介绍了[边界和接口][4]。在本文中,我将带你了解倒数第二个方法:处理特殊行为。
|
||||
在系列的前三篇文章中,我展示了 ZOMBIES 方法的前五项。第一篇中 [实现了最简场景][2],它为代码提供了最简可行路径。第二篇文章中执行了 [单元素场景和多元素场景上的测试][3]。第三篇中介绍了 [边界和接口][4]。在本文中,我将带你了解倒数第二个方法:处理特殊行为。
|
||||
|
||||
### 处理特殊行为
|
||||
|
||||
@ -33,7 +36,6 @@ ZOMBIES 表示以下首字母缩写:
|
||||
|
||||
现在将这个销售策略转换为可运行期望:
|
||||
|
||||
|
||||
```
|
||||
[Fact]
|
||||
public void Add2ItemsTotal600GrandTotal540() {
|
||||
@ -45,15 +47,14 @@ public void Add2ItemsTotal600GrandTotal540() {
|
||||
|
||||
这个正面样例表示的销售策略是,如果订单总额为 ¥600.00,那么 `shoppingAPI` 会将其减价为 ¥540.00。上面的代码伪造了一个失败验证用例。现在修改它,让它能够通过测试:
|
||||
|
||||
|
||||
```
|
||||
[Fact]
|
||||
public void Add2ItemsTotal600GrandTotal540() {
|
||||
var expectedGrandTotal = 540.00;
|
||||
Hashtable item = [new][5] Hashtable();
|
||||
Hashtable item = new Hashtable();
|
||||
item.Add("00000001", 200.00);
|
||||
shoppingAPI.AddItem(item);
|
||||
Hashtable item2 = [new][5] Hashtable();
|
||||
Hashtable item2 = new Hashtable();
|
||||
item2.Add("00000002", 400.00);
|
||||
shoppingAPI.AddItem(item2);
|
||||
var actualGrandTotal = shoppingAPI.CalculateGrandTotal();
|
||||
@ -65,7 +66,6 @@ public void Add2ItemsTotal600GrandTotal540() {
|
||||
|
||||
这个微测试能够通过吗?
|
||||
|
||||
|
||||
```
|
||||
[xUnit.net 00:00:00.57] tests.UnitTest1.Add2ItemsTotal600GrandTotal540 [FAIL]
|
||||
X tests.UnitTest1.Add2ItemsTotal600GrandTotal540 [2ms]
|
||||
@ -82,7 +82,6 @@ Actual: 600
|
||||
|
||||
在 `ShippingAPI` 类中实现该处理逻辑:
|
||||
|
||||
|
||||
```
|
||||
private double Calculate10PercentDiscount(double total) {
|
||||
double discount = 0.00;
|
||||
@ -97,16 +96,17 @@ private double Calculate10PercentDiscount(double total) {
|
||||
|
||||
你还需要告诉系统怎么从订单总额中减去 10%。改动非常直接:
|
||||
|
||||
|
||||
```
|
||||
`return grandTotal - Calculate10PercentDiscount(grandTotal);`
|
||||
return grandTotal - Calculate10PercentDiscount(grandTotal);
|
||||
```
|
||||
|
||||
到此,所有测试都能够通过。你又一次享受到系统处于稳态的欢愉。你的代码通过处理特殊行为实现了需要的销售策略。
|
||||
|
||||
### 最后一步
|
||||
|
||||
现在我已经介绍完 ZOMBIE 了,只剩下 S 了。我将会在最后一篇中介绍它。
|
||||
|
||||
*(题图:MJ/7f8bf5d2-54ce-4d6e-9dbf-13abf6df966a)*
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -115,14 +115,15 @@ via: https://opensource.com/article/21/2/exceptional-behavior
|
||||
作者:[Alex Bunardzic][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[toknow-gh](https://github.com/toknow-gh)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/alex-bunardzic
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/team_dev_email_chat_video_work_wfm_desk_520.png?itok=6YtME4Hj (Working on a team, busy worklife)
|
||||
[2]: https://opensource.com/article/21/1/zombies-zero
|
||||
[3]: https://opensource.com/article/21/1/zombies-2-one-many
|
||||
[4]: https://opensource.com/article/21/1/zombies-3-boundaries-interface
|
||||
[2]: https://linux.cn/article-15808-1.html
|
||||
[3]: https://linux.cn/article-15817-1.html
|
||||
[4]: https://linux.cn/article-15859-1.html
|
||||
[5]: http://www.google.com/search?q=new+msdn.microsoft.com
|
||||
[0]: https://img.linux.net.cn/data/attachment/album/202305/30/094226xz66t662t42auuht.jpg
|
Loading…
Reference in New Issue
Block a user