DAY 9
7

## [如何提升系統品質-Day9]重構-簡化判斷式

[如何提升系統品質]系列文章連結

``````/// <summary>
/// </summary>
public class DrinkingStore
{
public Person Boss { get; set; }

public bool IsBusinessDay(DateTime date, Person customer)
{
//一三五不賣酒
if (date.DayOfWeek == DayOfWeek.Monday || date.DayOfWeek == DayOfWeek.Wednesday || date.DayOfWeek == DayOfWeek.Friday ||
//初一十五不賣酒
date.Day == 1 || date.Day == 15 ||
//老闆不爽不賣酒, 老闆哥哥不在家不賣酒
this.Boss.IsAngry || this.Boss.Brother == null ||
//客人沒錢不賣酒//客人太醜不賣酒//客人太胖不賣酒
customer.IsPoor || customer.IsUgly || customer.IsFat)
{
return false;
}
else
{
return true;
}
}
}
``````

1.根據條件來決定，賣不賣酒
2.條件有分成幾種類型：
(1)根據『日期中的星期幾』來決定
(2)根據『日期中的日子』來決定
(3)根據『老闆的一堆毛』來決定
(4)根據『客人的一堆毛』來決定

``````/// <summary>
/// </summary>
public class DrinkingStore
{
public Person Boss { get; set; }

public bool IsBusinessDay(DateTime date, Person customer)
{
if (WithoutSellingByDayOfWeek(date) ||
WithoutSellingByDay(date) ||
WithoutSellingByBoss() ||
WithoutSellingByCustomer(customer))
{
return false;
}
else
{
return true;
}
}

/// <summary>
/// 一三五不賣酒
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
private static bool WithoutSellingByDayOfWeek(DateTime date)
{
return date.DayOfWeek == DayOfWeek.Monday || date.DayOfWeek == DayOfWeek.Wednesday || date.DayOfWeek == DayOfWeek.Friday;
}

/// <summary>
/// 初一十五不賣酒
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
private static bool WithoutSellingByDay(DateTime date)
{
return date.Day == 1 || date.Day == 15;
}

/// <summary>
/// 老闆不爽不賣酒, 老闆哥哥不在家不賣酒
/// </summary>
/// <returns></returns>
private bool WithoutSellingByBoss()
{
return this.Boss.IsAngry || this.Boss.Brother == null;
}

/// <summary>
/// 客人沒錢不賣酒
/// 客人太醜不賣酒
/// 客人太胖不賣酒
/// </summary>
/// <returns></returns>
private bool WithoutSellingByCustomer(Person customer)
{
return customer.IsPoor || customer.IsUgly || customer.IsFat;
}
}
``````

### 5 則留言

0
billchung
iT邦新手 3 級 ‧ 2011-10-19 07:02:15

0
chiounan
iT邦研究生 1 級 ‧ 2011-10-19 09:38:58

0
pantc328
iT邦研究生 1 級 ‧ 2011-10-19 10:49:27

pantc328 iT邦研究生 1 級 ‧ 2011-10-20 08:57:13 檢舉

OO是一種

....

pantc328提到：

0
billchung
iT邦新手 3 級 ‧ 2011-10-19 12:29:50

0
billchung
iT邦新手 3 級 ‧ 2011-10-22 01:02:37