Object Model for complex Calculator [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this questionI'm thinking of a object-structure, which shall do complex calculations. The calculations are done, based upon the entries on a GUI of the user.
The formular looks like this:
A = B+C;
B = D*E;
C = C; (simply value insert by the user)
D = C/E;
E = C*C;
The user can enter values on all values (A to E / in real project, there are about 210 variables). If 开发者_运维问答it is not possible to develop a mathematical formular to calculate the other values, it will be done by iterating the values, till finding a "working setup".
I'm thinking now about the following structure: Every variable is an object of a data-class. Each dataclass can register a listener on other dataclasses to get value-change events. If a dataclass get's a value change event, it recalculates itselfs and informs all listeners about it's own datachange.
The problem is now the following: If the last calculation in this chain get's a result, that the entered value is not possible ("division by zero" for example), it will set it's value to the nearest possible value (in this case 1). Then all calculations in the chain before has to be done again in reverse order.
Is there -in your eyes- a better structure than this listener-based structure where everybody recalculates, if he detects a change-event on observed variables (with 210 veriables I'm about to loose the overview who calculates what, when and why; A central calculation-structure seems to be more maintainable to me, but I can't imagine a good structure for a central calculation-unit).
Any opinions and recommendations are welcome! Cheers, Joan
After a lot of time spending in google, I found an interesting project for reactive java at google code: reactive4java
I have not tried this project yet, but unlike the other projects I found (Frappé, Junior and SugarCubes), it seems to be still active and under development. The other mentioned projects are either not available any more, or has their last change years ago.
Update: After playing around with reactive4java, I came to the conclusion, that this project can not be used in an oop-context, to do your calculations. It was not possible for me (up to now) to bind any objects or values from the project against the Reactive-kernel or vice versa. If I find out more about this, I'll post it here again.
Update 2: After talking to the maintainer of the project, I was pointed out how do bind an data-object against the reactive4java framework, so the framework does the calculations, when a dataobject changes. I'll post a detailed working example after playing around with the framework.
What you are implementing is called reactive programming. The article also mentions that using the observer pattern directly probably results in performance problems.
There are several libraries implementing support for this kind of programming model, you could look into some of them and find out what they are doing. As far as I know many of them use a graph view of the dependencies and only recalculate the value of an expression when all reachable inputs have been reached by new values.
I've seen reactive programming years ago - but I think you won't get a better overview over your project with this kind of coding. It just gets you rid of handling the Observers and events.
精彩评论