假设我们有一个要写的文本,可以转换为"大写或小写",并可以"左,中,右"打印.
writeInUpperCaseAndCentered(char *str){//..}
writeInLowerCaseAndCentered(char *str){//..}
writeInUpperCaseAndLeft(char *str){//..}
and so on...
Run Code Online (Sandbox Code Playgroud)
VS
write( char *str , int toUpper, int centered ){//..}
Run Code Online (Sandbox Code Playgroud)
VS
writeComplex (char *str)
{
// analize str and perhaps some global variables and
// (under who knows what rules) put it center/left/right and upper/lowercase
}
Run Code Online (Sandbox Code Playgroud)
也许还有其他选择..(并欢迎)
问题是:
对于这种(经常性)三难问题,是否有任何良好实践或经验/学术建议?
我通常做的是结合"特定情况"实现,内部(我的意思是不在标题中)一般常见的多参数函数,只实现用例,隐藏丑陋的代码,但我不知道是否有一种我不知道的更好的方式.这种事情让我意识到为什么发明OOP.