next up previous contents index
Next: 13.8 Conclusion Up: 13. Object Oriented Computing Previous: 13.6 More about Inheritance

13.7 Structure of the Object Oriented Program

We conclude this chapter by describing shortly the typical structure of the OOP program.

As we have seen in previous sections, an OOP program consists of objects that collaborate by messages. In this structure, one object must play the role of a starting object. This means that one of the methods of this object will be called as the program start. The starting object typically creates other objects in the program and manages the their lifetime.

All the other objects represent various parts of the problem solved and are responsible for the associated resource management, computation, etc.

Figure 13.8: Basic structure of the simulation program
\includegraphics[width=11.5cm]{text/2-13/Vir08.eps}

Example 16   Here we finish the Monte Carlo simulation of an experiment with particles. We have already mentioned the Experiment class covering the application as a whole. The only instance of this class in the program will be the starting object. The Experiment class will contain the public method Run() that will represent the run of the experiment.

As we are in C++, our program must have the main() function where the program starts. It will create the starting object and let it run:

int main() {            // Create the starting object
    Experiment().Run(); // and run it
}
The Experiment class could be defined as follows:
class Experiment{
public:
    Experiment();       // Constructor
    void Run();         // Run the experiment
private:
    Detector *det;      // Detector in this experiment
    Source *sce;        // Particle source
    ResultFile *rfile;  // Result file
 };

The Experiment::Experiment() constructor reads the input data (e.g., cross sections describing the probabilities of the interactions) and creates and initializes the attributes (particle source, result file etc.).

The Experiment::run() methods does the work - it starts particle emission in the source using the appropriate method of the Source class, determines, whether the given particle hits the detector using the appropriate Detector method, records the results of the detection into the ResultFile instance and in the end processes the results using the appropriate ResultFile methods.

Class diagram of the program at the level we have given here is presented in Fig. 13.8.

Note that this is top-level design only. The extent of this chapter allows us to demonstrate only the beginning of the object oriented approach to the example.


next up previous contents index
Next: 13.8 Conclusion Up: 13. Object Oriented Computing Previous: 13.6 More about Inheritance