Object-oriented design patterns in Fortran 90/95: mazev1, mazev2 and mazev3☆
Introduction
As computers become more powerful, there is a growing desire to make scientific codes increasingly complex. In the computer science literature, researchers have discovered that similar solutions to programming complex problems have appeared in different contexts, and they have called such solutions design patterns. The seminal work by Gamma, Helm, Johnson, and Vlissides [1], identified 23 recurring design patterns. These patterns are intended to be language independent, and, indeed, a number of texts have appeared discussing their application to C++, Java, and other languages (see, for example, [2], [3]). But the treatment of design patterns in Fortran 90/95 is quite lacking by comparison to these languages.
Fortran 90/95 is a non-object-oriented language but it contains a language construct, the module, which can be used to emulate “classes”. The resulting “object-based” style of programming has been recommended by some authors [4], [5], [6], [7], [8] as a way of enhancing the encapsulation and reuse of Fortran 90/95 code “components”.
In the following two sections we review the basic concepts behind object-based programming in Fortran 90/95 and introduce the running example of the maze game. This example is used to illustrate the implementation of three important design patterns, Template, Strategy and Factory, in Sections 4 The template and strategy patterns:, 5 A factory pattern:. The way in which a design patterns approach has been used to construct a framework for Particle-in-Cell simulation is described in Section 6 and the paper concludes with Section 7.
Section snippets
Classes in Fortran 90/95
In the following sections we will illustrate how some object-oriented (OO) design patterns can be usefully emulated in Fortran 90/95. As a vehicle for this discussion, we will use a running example which has been adapted from [1]. This is a game program which creates a two-dimensional maze through which a player needs to navigate in order to find an exit. The various versions of the software that we discuss will be available through the CPC software archive.
In our discussions, we will refer to
The maze game: mazev1
In this section we present our simple programming example as if it were a larger software development project.
Imagine that you set out to write a Fortran 90/95 program for a maze game. Good software engineering practice would dictate that you would start this exercise by defining the requirements of your system, that you would follow this phase by an analysis of these requirements before moving on to design and, subsequently, implement and test your software. Standard software process
The template and strategy patterns: mazev2
With all of the attention to encapsulation and information hiding described in the previous section, our mazev1 program represents good software engineering practice for the implementation of a single maze game. Design patterns will now be introduced for the first time to enable the program structure to be generalized to support different maze games.
Suppose we wanted to have several options for an algorithm such as the game logic of the maze game. The Template design pattern is used to
A factory pattern: mazev3
We now consider a case where the maze game is further extended to have rooms in the maze with different properties. Specifically, we will introduce a TreasureRoom_class module which will contain a number of treasures which must be collected by entering each room. A treasure room is a room with additional properties and in object-oriented languages this relationship would be modeled by inheritance.
Factory patterns encapsulate the creation of objects within an inheritance hierarchy. In the case
Design patterns in a Particle-in-Cell simulation framework
In this section, we briefly describe how the design patterns introduced above have been applied in a living scientific simulation code. The application is a framework for Particle-in-Cell (PIC) plasma simulation. PIC plasma codes integrate the self-consistent equations of motion of a large number of charged particles in electromagnetic fields. Their basic structure is to calculate the density of charge, and possibly current, on a fixed grid (by calling “deposit” procedures). Maxwell's
Conclusions
In this paper we have discussed how some object-oriented design patterns can usefully be incorporated into software written in Fortran 90/95. The design patterns that we have mentioned, Factory, Strategy, Template, Abstract Factory and Facade, are all concerned with the encapsulation and reuse of subsystems of Fortran 90/95 programs. If there is one message from this paper, it is that Fortran 90/95 programmers can profitably direct their design focus to encapsulation at the subsystem level. As
Acknowledgements
The authors express their appreciation for a reviewer's recommendation that the maze game would make a good study for illustrating design patterns in Fortran 90/95. They also greatly appreciate the work done by Joseph Antony on the maze game simulation software.
Viktor Decyk's work was performed, in part, at the Jet Propulsion Laboratory, California Institute of Technology, under a contract with NASA. Decyk was also partly supported by the US Department of Energy, under the SCIDAC program.
Henry
References (13)
- et al.
Computer Physics Communications
(1998) - et al.
Design Patterns: Elements of Reusable Object Oriented Software
(1995) - et al.
Design Patterns Explained
(2002) Design Patterns Java Workbook
(2002)- et al.
Scientific Programming
(1997) - et al.
Computers in Physics
(1997)
Cited by (12)
The vmf90 program for the numerical resolution of the Vlasov equation for mean-field systems
2014, Computer Physics CommunicationsCitation Excerpt :Fortran 90 provides, with respect to older revisions of the Fortran programming language, many improvements for the structured coding of scientific programs. The reader is referred to Refs. [19–21] for more information on this topic. In addition to these ideas, vmf90 takes profit of revision control with the git software [22] and extensive in-code documentation.
An experimental approach to the performance penalty of the use of classes in Fortran 95
2011, Advances in Engineering SoftwareCitation Excerpt :Additional information on the use of Fortran 95 for OOP in general can be found in e.g. [12,1,21,17]. Information on how to use design patterns in Fortran can be found in [9]. In the object oriented paradigm, the problem that is to be solved by a program is represented by “objects”.
Object construction and destruction design patterns in Fortran 2003
2010, Procedia Computer ScienceBridging the chasm: A survey of sofware engineering practice in scientific programming
2017, ACM Computing Surveys
- ☆
This paper and its associated computer program are available via the Computer Physics Communications homepage on ScienceDirect (http://www.sciencedirect.com/science/journal/00104655).