一見したところ、多数のネストされた if ステートメントを必要とする複雑なビジネス ロジックを処理するための適切な方法は何ですか?
例:
割引クーポン。になり得る:
1a) バリューディスカウント
1b) パーセンテージディスカウント
2a) 通常割引
2b) プログレッシブ割引
3a) アクセス クーポンが必要
3b) アクセス クーポンが不要
4a) 以前に購入した顧客のみに
適用 4b) すべての顧客に適用
5a) 国 (X、Y、…) のお客様のみに適用
これには、次のコードよりもさらに複雑なコードが必要です。
if (discount.isPercentage) {
if (discount.isNormal) {
if (discount.requiresAccessCoupon) {
} else {
}
} else if (discount.isProgressive) {
if (discount.requiresAccessCoupon) {
} else {
}
}
} else if (discount.isValue) {
if (discount.isNormal) {
if (discount.requiresAccessCoupon) {
} else {
}
} else if (discount.isProgressive) {
if (discount.requiresAccessCoupon) {
} else {
}
}
} else if (discount.isXXX) {
if (discount.isNormal) {
} else if (discount.isProgressive) {
}
}
IF を switch/case に置き換えても、まだ複雑すぎます。読みやすく、保守しやすく、テストしやすく、理解しやすいものにする方法は何ですか?