In software engineering, the module pattern is a design pattern used to implement the concept of software modules, defined by modular programming, in a programming language with incomplete direct support for the concept.
This pattern can be implemented in several ways depending on the host programming language, such as the singleton design pattern, object-oriented static members in a class and procedural global functions. In Python, the pattern is built into the language, and each .py file is automatically a module. The same applies to Ada, where the package can be considered a module (similar to a static class).
Definition & Structure
The module software design pattern provides the features and syntactic structure defined by the modular programming paradigm to programming languages that have incomplete support for the concept.
Concept
In software development, source code can be organized into components that accomplish a particular function or contain everything necessary to accomplish a particular task. Modular programming is one of those approaches.
The concept of a "module" is not fully supported in many common programming languages.
Features
In order to consider that a Singleton or any group of related code implements this pattern, the following features must be supplied:
A portion of the code must have global or public access and be designed for use as global/public code. Additional private or protected code can be executed by the main public code. A module must have an initializer function that is equivalent to, or complementary to an object constructor method. This feature is not supported by regular namespaces. A module must have a finalizer function that is equivalent to, or complementary to an object destructor method. This feature is not supported by regular namespaces. Supporting members may require initialization/finalization code that is executed by the module's initializer/finalizer function. Most members are functions that perform operations on elements external to the class, provided as arguments by calling functions. Such functions are "utilities", "tools" or "libraries".
Implementations
The semantics and syntax of each programming language may affect the implementation of this pattern.
Object-oriented programming languages
Java
Although Java supports the notion of a namespace, a reduced version of a module, some scenarios benefit from employing the design pattern instead of using namespaces.
The following example uses the singleton pattern.
Definition
package consoles;
import java.io.InputStream; import java.io.PrintStream;
public final class MainModule {
private static MainModule singleton = null;
public InputStream input = null; public PrintStream output = null; public PrintStream error = null;
private MainModule() {
// does nothing on purpose !!! }
// ...
public static MainModule getSingleton() {
if (MainModule.singleton == null) {
MainModule.singleton = new MainModule(); }
return MainModule.singleton; }
// ...
public void prepare() {
//System.out.println("consoles::prepare();");
this.input = new InputStream(); this.output = new PrintStream(); this.error = new PrintStream(); }
public void unprepare() {
this.output = null; this.input = null; this.error = null;
//System.out.println("consoles::unprepare();"); }
// ...
public void printNewLine() {
System.out.println(); }
public void printString(String value) {
System.out.print(value); }
public void printInteger(int value) {
System.out.print(value); }
public void printBoolean(boolean value) {
System.out.print(value); }
public void scanNewLine() {
// to-do: ... }
public void scanString(String value) {
// to-do: ... }
public void scanInteger(int value) {
// to-do: ... }
public void scanBoolean(boolean value) {
// to-do: ... }
// ...
}
Implementation
import consoles.*;
class ConsoleDemo {
public static MainModule console = null;
public static void prepare() {
console = MainModule.getSingleton();
console.prepare(); }
public static void unprepare() {
console.unprepare(); }
public static void execute(String[] args) {
console.printString("Hello World"); console.printNewLine(); console.scanNewLine(); }
public static void main(String[] args) {
prepare(); execute(args); unprepare(); } }
C# (C Sharp .NET)