Jump to content

Module pattern

From Wikipedia, the free encyclopedia

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

[edit]

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.

The object module pattern expressed in UML.
The object module pattern expressed in UML.

Concept

[edit]

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

[edit]

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

[edit]

The semantics and syntax of each programming language may affect the implementation of this pattern.

Object-oriented programming languages

[edit]

Java

[edit]

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
[edit]
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
[edit]
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)

[edit]

C#, like Java, supports namespaces although the pattern remains useful in specific cases.

The following example uses the singleton pattern.

Definition
[edit]
using System;
using System.IO;
using System.Text;

namespace Consoles;

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 MainModule();
        }

        return MainModule.Singleton;
    }

    // ...

    public void Prepare()
    {
        //System.WriteLine("console::prepare();");

        this.input = new InputStream();
        this.output = new OutputStream();
        this.error = new ErrorStream();
    }

    public void Unprepare()
    {
        this.output = null;
        this.input = null;
        this.error = null;

        //System.WriteLine("console::unprepare();");
    }

    // ...

    public void PrintNewLine()
    {
        System.Console.WriteLine("");
    }

    public void PrintString(String Value)
    {
        System.Console.Write(Value);
    }

    public void PrintInteger(Integer Value)
    {
        System.Console.Write(Value);
    }

    public void PrintBoolean(Boolean Value)
    {
        System.Console.Write(Value);
    }

    public void ScanNewLine()
    {
        // to-do: ...
    }

    public void ScanString(String Value)
    {
        // to-do: ...
    }

    public void ScanInteger(Integer Value)
    {
        // to-do: ...
    }

    public void ScanBoolean(Boolean Value)
    {
        // to-do: ...
    }

    // ...
}
Implementation
[edit]
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()
    {
        Prepare();
        Execute(args);
        Unprepare();
    }
}

Prototype-based programming languages

[edit]

JavaScript

[edit]

JavaScript is commonly used to automate web pages.

Definition
[edit]
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
[edit]
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

[edit]

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)

[edit]

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
[edit]
<?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
[edit]
// 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

[edit]

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
[edit]
// filename: "consoles.h"

#include <stdio.h>
#include <string.h>
#include <ctype.h>

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
[edit]
// filename: "consoles.c"

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <consoles.h>

void consoles_prepare() {
  // code that prepares console
}

void consoles_unprepare() {
  // code that unprepares console
}

// ...

void consoles_printNewLine() {
  printf("\n");
}

void consoles_printString(char* Value) {
  printf("%s", Value);
}

void consoles_printInteger(int Value) {
  printf("%d", &Value);
}

void consoles_printBoolean(bool Value) {
  printf((Value) ? ("true") : ("false"));
}

void consoles_scanNewLine() {
  getch();
}

void consoles_scanString(char* Value) {
  scanf("%s", Value);
}

void consoles_scanInteger(int* Value) {
  scanf("%d", Value);
}

void consoles_scanBoolean(bool* Value) {
  char temp[512];
  scanf("%s", temp);

  *Value = (strcmp(Temp, "true") == 0);
}
Implementation
[edit]
// filename: "consoledemo.c"

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <consoles.h>

void consoledemo_prepare()
{
  consoles_prepare();
}
 
void consoledemo_unprepare()
{
  consoles_unprepare();
}
 
int consoledemo_execute()
{
  consoles_printString("Hello World");
  consoles_printNewLine();
  consoles_scanNewLine();
 
  return 0;
}
 
int main()
{
  ErrorCode Result = 0;

  consoledemo_prepare();
  ErrorCode = consoledemo_execute();
  consoledemo_unprepare();

  return ErrorCode;
}

Procedural Pascal

[edit]

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
[edit]
  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
[edit]
  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

[edit]

Namespaces

[edit]

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

[edit]

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

[edit]

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

[edit]

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

[edit]

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

[edit]