Object Model for a Comprehensive Calculator

I am thinking of an object structure that will perform complex calculations. Calculations are performed based on entries in the graphical user interface.

The form is as follows:

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 ​​for all values ​​(from A to E / in a real project, there are about 210 variables). If it is impossible to develop a mathematical formula for calculating other values, this will be done by iterating over the values ​​until you find the “working setting”.

Now I think of the following structure: Each variable is an object of a data class. Each dataclass can register a listener with other dataclasses to receive value change events. If a dataclass receives a value change event, it recounts itself and informs all listeners about this own datachange.

The problem is this: If the last calculation in this chain gets the result, then the entered value is impossible (for example, "division by zero"), it will set the value to the nearest possible value (in this case 1). Then all the calculations in the chain before this should be performed again in the reverse order.

Is there a better structure in your eyes than this listener-based structure, where everyone recounts if he detects an event of a change in the observed variables (with 210 veriables I'm going to lose sight, which calculates what, when and why: The central calculation structure seems to be is more convenient for me, but I can’t imagine a good structure for a central computing unit).

Any opinions and recommendations are welcome! Hooray, Joan

+4
source share
3 answers

After a lot of time in google, I found an interesting project for reactive java in google code: reactive4java

I have not tried this project yet, but unlike other projects that I found ( Frappé , Junior and SugarCubes ), it is still active and is in development. Other mentioned projects are either not available or have the latest change years ago.

Update: After playing with reactive 4java, I came to the conclusion that this project cannot be used in an oop context to perform your calculations. I have not been able (so far) to associate any objects or values ​​from the project with Reactive-kernel or vice versa. If I find out more about this, I will post it here again.

Update 2:. After talking with the project developer, I was told how to bind the data object to the reactive4java infrastructure, so the structure performs calculations when the data object changes. I will post a detailed working example after playing with a wireframe.

+3
source

What you implement is called reactive programming . The article also mentions that using an observer pattern directly can lead to performance problems.

There are several libraries that support this programming model, you can study some of them and find out what they do. As far as I know, many of them use a graphical representation of dependencies and only recount the value of the expression when all reachable values ​​are reached by new values.

+2
source

I saw reactive programming many years ago - but I think that you will not get a better overview of your project with this type of coding. It just saves you the handling of observers and events.

+1
source

All Articles