1

X 类具有三个属性:left、right 和 sum。左 + 右 = 总和。它们存储精确值,例如,Left 是 51.2584165861,Right 是 55.8155984388,Sum 是 107.07401502496。

但在 UI 上,我必须将它们四舍五入为两位精度小数。他们将显示 Left = 51.26、Right = 55.82 和 Sum = 107.07,这是不正确的。51.26 + 55.82 应该是 107.08。

我认为计算其他两个值的一个值会很好,例如 Sum - Left 然后我们将 Right 显示在 Ui 上。

如果我为 Ui 创建一个新类,我会将所有属性移动到基类,然后更改当前类和 ui 类以从它派生。它们只有一个区别,原始类存储精确值,而 ui 类将存储 2 个精度值。也许我在 ui 类构造函数中应用了舍入。

我的问题是,在这个示例中为 Ui 创建类好吗?仅为 Ui 创建一个类的优缺点是什么?

4

1 回答 1

3

is it good to create class for Ui in this sample?

Yes.

What are pros and cons of create a class only for UI?

Pros: You're managing the UI data in one place. Your controller, in a model, view, controller architecture, deals with the UI model, rather than the UI components individually.

You can change the UI components without necessarily changing the UI model. As an example, you could change a check box to a toggle button without changing the model.

Cons: Your UI data model tends to be global across the UI. By definition, your UI model is a singleton.

于 2012-05-29T16:49:55.340 回答