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 org.wikipedia.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 org.wikipedia.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)
C#, like Java, supports namespaces although the pattern remains useful in specific cases.
The following example uses the singleton pattern.
Definition
namespace Wikipedia.Consoles;
using System; using System.IO; using System.Text;
public sealed class MainModule { private static MainModule Singleton = null; public InputStream input = null; public OutputStream output = null; public ErrorStream error = null;
// ...
public MainModule()
// does nothing on purpose !!!
// ...
public static MainModule GetSingleton()
if (MainModule.Singleton == null)
MainModule.Singleton = new();
return MainModule.Singleton;
// ...
public void Prepare()
// System.WriteLine("console::prepare();");
this.input = new(); this.output = new(); this.error = new();
public void Unprepare()
this.output = null; this.input = null; this.error = null;
// Console.WriteLine("console::unprepare();");
// ...
public void PrintNewLine()
Console.WriteLine("");
public void PrintString(string Value)
Console.Write(Value);
public void PrintInteger(int Value)
Console.Write(Value);
public void PrintBoolean(bool Value)
Console.Write(Value);
public void ScanNewLine()
// to-do: ...
public void ScanString(string Value)
// to-do: ...
public void ScanInteger(int Value)
// to-do: ...
public void ScanBoolean(bool Value)
// to-do: ...
// ... }
Implementation
class ConsoleDemo { public static Consoles.MainModule Console = null;
public static void Prepare()
Console = Consoles.MainModule.GetSingleton();
Console.Prepare();
public static void Unprepare()
Console.Unprepare();
public static void Execute()
Console.PrintString("Hello World"); Console.PrintNewLine(); Console.ScanNewLine();
public static void Main(string[] args)
Prepare(); Execute(args); Unprepare();
Prototype-based programming languages
JavaScript JavaScript is commonly used to automate web pages.
Definition
function ConsoleClass() { var Input = null; var Output = null; var Error = null;
// ...
this.prepare = function() { this.Input = new InputStream(); this.Output = new OutputStream(); this.Error = new ErrorStream();
this.unprepare = function() { this.Input = null; this.Output = null; this.Error = null;
// ...
var printNewLine = function() { // code that prints a new line
var printString = function(params) { // code that prints parameters
var printInteger = function(params) { // code that prints parameters
var printBoolean = function(params) { // code that prints parameters
var ScanNewLine = function() { // code that looks for a newline
var ScanString = function(params) { // code that inputs data into parameters
var ScanInteger = function(params) { // code that inputs data into parameters
var ScanBoolean = function(params) { // code that inputs data into parameters
// ...
Implementation
function ConsoleDemo() { var Console = null;
var prepare = function() { Console = new ConsoleClass();
Console.prepare();
var unprepare = function() { Console.unprepare();
var run = function() { Console.printString("Hello World"); Console.printNewLine();
var main = function() { this.prepare(); this.run(); this.unprepare();
Procedural programming languages
This pattern may be seen as a procedural extension to object-oriented languages.
Although the procedural and modular programming paradigms are often used together, there are cases where a procedural programming language may not fully support modules, hence requiring a design pattern implementation.
PHP (procedural)
This example applies to procedural PHP before namespaces (introduced in version 5.3.0). It is recommended that each member of a module is given a prefix related to the filename or module name in order to avoid identifier collisions.
Definition
<?php // filename: console.php
function console_prepare() { // code that prepares a "console" }
function console_unprepare() { // code that unprepares a "console" }
// ...
function console_printNewLine() { // code that outputs a new line }
function console_printString(/* String */ Value) { // code that prints parameters }
function console_printInteger(/* Integer */ Value) { // code that prints parameters }
function console_printBoolean(/* Boolean */ Value) { // code that prints parameters }
function console_scanNewLine() { // code that looks for a new line }
function console_scanString(/* String */ Value) { // code that stores data into parameters }
function console_scanInteger(/* Integer */ Value) { // code that stores data into parameters }
function console_scanBoolean(/* Boolean */ Value) { // code that stores data into parameters }
Implementation
// filename: consoledemo.php
require_once("console.php");
function consoledemo_prepare() { console_prepare(); }
function consoledemo_unprepare() { console_unprepare(); }
function consoledemo_execute() { console_printString("Hello World"); console_printNewLine(); console_scanNewLine(); }
function consoledemo_main() { consoledemo_prepare(); consoledemo_execute(); consoledemo_unprepare(); }
C
Note that this example applies to procedural C without namespaces. It is recommended that each member of a module is given a prefix related to the filename or module name in order to avoid identifier collisions.
Definition header module
// filename: "consoles.h" #pragma once
void consolesPrepare(); void consolesUnprepare();
// ...
void consolesPrintNewLine();
void consolesPrintString(char* value); void consolesPrintInteger(int value); void consolesPrintBoolean(bool value);
void consolesScanNewLine();
void consolesScanString(char* value); void consolesScanInteger(int* value); void consolesScanBoolean(bool* value);
Definition body module
// filename: "consoles.c"
#include <ctype.h> #include <stdio.h> #include <string.h>
#include "consoles.h"
void consolesPrepare() { // code that prepares console }
void consolesUnprepare() { // code that unprepares console }
// ...
void consolesPrintNewLine() { printf("\n"); }
void consolesPrintString(char* value) { printf("%s", value); }
void consolesPrintInteger(int value) { printf("%d", &value); }
void consolesPrintBoolean(bool value) { printf((value) ? ("true") : ("false")); }
void consolesScanNewLine() { getch(); }
void consolesScanString(char* value) { scanf("%s", value); }
void consolesScanInteger(int* value) { scanf("%d", Value); }
void consolesScanBoolean(bool* value) { char temp; scanf("%s", temp);
*value = (strcmp(temp, "true") == 0); }
Implementation
// filename: "consoledemo.c"
#include "consoles.h"
void consoledemoPrepare() { consolesPrepare();
void consoledemoUnprepare() { consolesUnprepare();
int consoledemoExecute() { consolesPrintString("Hello World"); consolesPrintNewLine(); consolesScanNewLine();
return 0;
int main() { int result = 0;
consoledemoPrepare(); result = consoledemoExecute(); consoledemoUnprepare();
return result; }
Procedural Pascal
Note that this example applies to procedural non-modular Pascal. Many Pascal dialects have namespace support, called "unit (s)". Some dialects also support initialization and finalization.
If namespaces are not supported, it is recommended to give all member names a prefix related to the filename or module name in order to prevent identifier collisions.
Definition
unit consoles; (* filename: "consoles.pas" *)
uses crt;
procedure prepare(); begin (* code that prepares console *) end;
procedure unprepare(); begin (* code that unprepares console *) end;
// ...