Module pattern explained

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:

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

Implementation

import consoles.*;

class ConsoleDemo

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

using System;using System.IO;using System.Text;

namespace Consoles;

public sealed class MainModule

Implementation

class ConsoleDemo

Prototype-based programming languages

JavaScript

JavaScript is commonly used to automate web pages.

Definition

function ConsoleClass

Implementation

function ConsoleDemo

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

function console_prepare

function console_unprepare

// ...

function console_printNewLine

function console_printString(/* String */ Value)

function console_printInteger(/* Integer */ Value)

function console_printBoolean(/* Boolean */ Value)

function console_scanNewLine

function console_scanString(/* String */ Value)

function console_scanInteger(/* Integer */ Value)

function console_scanBoolean(/* Boolean */ Value)

Implementation

// filename: consoledemo.php

require_once("console.php");

function consoledemo_prepare

function consoledemo_unprepare

function consoledemo_execute

function consoledemo_main

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"

  1. include
  2. include
  3. include

void consoles_prepare; void consoles_unprepare;

// ...

void consoles_printNewLine;

void consoles_printString(char* Value); void consoles_printInteger(int Value); void consoles_printBoolean(bool Value);

void consoles_scanNewLine;

void consoles_scanString(char* Value); void consoles_scanInteger(int* Value); void consoles_scanBoolean(bool* Value);

Definition body module

// filename: "consoles.c"

  1. include
  2. include
  3. include
  4. include

void consoles_prepare

void consoles_unprepare

// ...

void consoles_printNewLine

void consoles_printString(char* Value)

void consoles_printInteger(int Value)

void consoles_printBoolean(bool Value)

void consoles_scanNewLine

void consoles_scanString(char* Value)

void consoles_scanInteger(int* Value)

void consoles_scanBoolean(bool* Value)

Implementation

// filename: "consoledemo.c"

  1. include
  2. include
  3. include
  4. include

void consoledemo_prepare void consoledemo_unprepare int consoledemo_execute int main

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;

// ... procedure printNewLine; begin WriteLn; end; procedure printString(Value: string); begin Write(Value); end; procedure printInteger(Value: integer); begin Write(Value); end; procedure printBoolean(Value: boolean); begin if (Value) then begin Write('true'); end else begin Write('false'); end; end; procedure scanNewLine; begin SeekEoLn; end; procedure scanString(Value: string); begin ReadLn(Value); end; procedure scanInteger(Value: Integer); begin ReadLn(Value); end; procedure scanBoolean(Value: Boolean); var temp: string; begin ReadLn(temp); if (Temp = 'true') then begin Value := true; end else begin Value := false; end; end;

Implementation

program consoledemo; // filename: "consoles.pas"

uses consoles;

procedure prepare; begin consoles.prepare; end; procedure unprepare; begin consoles.unprepare; end;

function execute: Integer; begin consoles.printString('Hello World'); consoles.printNewLine; consoles.scanNewLine; execute := 0; end; begin prepare; execute; unprepare; end.

Comparisons to other concepts

Namespaces

Both namespaces and modules allow to group several related entities by a single identifier, and in some situations, used interchangeably. Those entities can be globally accessed. The main purpose of both concepts is the same.

In some scenarios a namespace requires that the global elements that compose it are initialized and finalized by a function or method call.

In many programming languages, namespaces are not directly intended to support an initialization process nor a finalization process, and are therefore not equivalent to modules. That limitation can be worked around in two ways. In namespaces that support global functions, a function for initialization and a function for finalization are coded directly, and called directly in the main program code.

Classes and namespaces

Classes are used sometimes used as or with namespaces. In programming languages that don't support namespaces (e.g., JavaScript) but do support classes and objects, classes are often used to substitute for namespaces. These classes are usually not instantiated and consist exclusively of static members.

Singleton classes and namespaces

In object-oriented programming languages where namespaces are incompletely supported, the singleton pattern may be used instead of static members within a non-instantiable class.

Relationship with other design patterns

The module pattern can be implemented using a specialization of the singleton pattern. However, other design patterns may be applied and combined, in the same class.

This pattern can be used as a decorator, a flyweight, or an adapter.

Module as a design pattern

The Module pattern can be considered a creational pattern and a structural pattern. It manages the creation and organization of other elements, and groups them as the structural pattern does.

An object that applies this pattern can provide the equivalent of a namespace, providing the initialization and finalization process of a static class or a class with static members with cleaner, more concise syntax and semantics.

It supports specific cases where a class or object can be considered structured, procedural data. And, vice versa, migrate structured, procedural data, and considered as object-oriented.

See also