मैं के एक हजार लाइन पद्धति के साथ एक जावा वर्ग है, तो इस तरह/किसी और तर्क:पुनर्रचना अगर/किसी और तर्क
if (userType == "admin") {
if (age > 12) {
if (location == "USA") {
// do stuff
} else if (location == "Mexico") {
// do something slightly different than the US case
}
} else if (age < 12 && age > 4) {
if (location == "USA") {
// do something slightly different than the age > 12 US case
} else if (location == "Mexico") {
// do something slightly different
}
}
} else if (userType == "student") {
if (age > 12) {
if (location == "USA") {
// do stuff
} else if (location == "Mexico") {
// do something slightly different than the US case
}
} else if (age < 12 && age > 4) {
if (location == "USA") {
// do something slightly different than the age > 12 US case
} else if (location == "Mexico") {
// do something slightly different
}
}
मैं कैसे अधिक प्रबंधनीय कुछ में इस refactor करना चाहिए?
मैं समझता हूं कि यह एक त्वरित उदाहरण है, लेकिन स्ट्रिंग की तुलना में वास्तव में 'बराबर() 'के साथ किया जाना चाहिए। – BalusC
... जब आप बिल्कुल 12 होते हैं तो क्या होता है ??? – polygenelubricants
मैं सजावटी पैटर्न http://en.wikipedia.org/wiki/Decorator_pattern पर एक नज़र डालेगा। – Lumpy