本文介绍了具体功能VS很多争论VS上下文相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个文本编写,并且可以转换为大写或小写,并能在左,中,右被打印出来。

Suppose we have a text to write and could be converted to "uppercase or lowercase", and can be printed "at left, center or right".

writeInUpperCaseAndCentered(char *str){//..}
writeInLowerCaseAndCentered(char *str){//..}
writeInUpperCaseAndLeft(char *str){//..}
and so on...

VS

write( char *str , int toUpper, int centered ){//..}

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
}

也许还有其他的选择。(和欢迎)

现在的问题是:

时有任何的好习惯经验/学术建议的这个(经常性)三元悖论?

Is there is any good practice or experience/academic advice for this (recurrent) trilemma ?

我最常做的是结合特定情况的实施,与内部(我的意思是不是头)一般常见的许多参数的功能,实现只用的情况下,和隐藏的丑陋code,但我不知道,如果有更好的办法,我不知道。这样的事情让我明白为什么OOP发明的。

What I usually do is to combine "specific case" implementation, with an internal (I mean not in header) general common many-argument function, implementing only used cases, and hiding the ugly code, but I don't know if there is a better way that I don't know. This kind of things make me realize of why OOP was invented.

推荐答案

我会避免你的第一选择,因为就像你说的,你最终不得不实现(尽管可能只是作为宏)函数的数量可能会失控。当你决定要添加斜体支持计数双打,和下划线再次翻倍。

I'd avoid your first option because as you say the number of function you end up having to implement (though possibly only as macros) can grow out of control. The count doubles when you decide to add italic support, and doubles again for underline.

我可能避免第二个选项为好。 Againg考虑当你觉得有必要增加对斜体,下划线的支持会发生什么。现在,你需要另一个参数添加到功能,找到所有你在哪里调用的函数和更新这些电话的情况。总之,烦人,但再一次,你也许可以简化适当使用宏的过程。

I'd probably avoid the second option as well. Againg consider what happens when you find it necessary to add support for italics or underlines. Now you need to add another parameter to the function, find all of the cases where you called the function and updated those calls. In short, anoying, though once again you could probably simplify the process with appropriate use of macros.

这使得第三个选项。实际上,你可以得到一些与此使用bitflags其他替代品的好处。例如:

That leaves the third option. You can actually get some of the benefits of the other alternatives with this using bitflags. For example

#define WRITE_FORMAT_LEFT   1
#define WRITE_FORMAT_RIGHT  2
#define WRITE_FORMAT_CENTER 4
#define WRITE_FORMAT_BOLD   8
#define WRITE_FORMAT_ITALIC 16
....
write(char *string, unsigned int format)
{
  if (format & WRITE_FORMAT_LEFT)
  {
     // write left
  }

  ...
}

编辑:要回答格雷格小号

To answer Greg S.

我认为,最大的改进是,它意味着,如果我决定,在这一点上,增加了带下划线的文本支持,我需要两个步骤

I think that the biggest improvement is that it means that if I decide, at this point, to add support for underlined text I it takes two steps


  1. 添加的#define WRITE_FORMAT_UNDERLINE 32 来的标题

  2. 添加下划线的支持的write()

  1. Add #define WRITE_FORMAT_UNDERLINE 32 to the header
  2. Add the support for underlines in write().

在这一点上可以调用write(...,... | WRITE_FORMAT_UNLDERINE)曾经我很喜欢那里。更重要的一点,我并不需要修改pre-现有呼叫来写,这是我必须做的,如果我增加了一个参数的签名。

At this point it can call write(..., ... | WRITE_FORMAT_UNLDERINE) where ever I like. More to the point I don't need to modify pre-existing calls to write, which I would have to do if I added a parameter to its signature.

另一个潜在的好处是,它可以让你做一些这样的:

Another potential benefit is that it allows you do something like the following:

#define WRITE_ALERT_FORMAT  (WRITE_FORMAT_CENTER | \
                             WRITE_FORMAT_BOLD |   \
                             WRITE_FORMAT_ITALIC)

这篇关于具体功能VS很多争论VS上下文相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:23