Class X has three properties, left, right, and sum. Left + Right = Sum. They store precise value, for example, Left is 51.2584165861, Right is 55.8155984388, and Sum is 107.07401502496.
But on UI, I have to round them into two precision decimal. They will show Left = 51.26, Right = 55.82 and Sum = 107.07, which are incorrect. 51.26 + 55.82 should be 107.08.
I think it would be good to calculate one value of other two, for example, Sum – Left and then we get Right to show on Ui.
If I create a new class for Ui, I will move all properties to base class then change current class and ui class to derive from it. They will have only one difference, the original class with store precise value while ui class will store 2 precision value. Maybe I apply rounding in ui class constructor.
My question is, is it good to create class for Ui in this sample? What are pros and cons of create a class only for Ui?
Yes.
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.