Module pattern: Difference between revisions

Content deleted Content added
 
(21 intermediate revisions by 13 users not shown)
Line 1:
{{Short description|Software design pattern}}
{{no footnotes|date=July 2014}}
In [[software engineering]], the '''module pattern''' is a [[design pattern (computer science)|design pattern]] used to implement the concept of [[wiktionary:Softwaremodular moduleprogramming|software modulemodules]]s, 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 pattern|singleton design pattern]], object-oriented [[staticStatic member variable|static members]]s in a [[class (software)|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 ==
Line 21 ⟶ 22:
 
* 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 [[Constructor (object-oriented programming)|object constructor]] method. This feature is not supported by regular [[namespace]]s.
* 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.
Line 40 ⟶ 41:
===== Definition =====
 
<sourcesyntaxhighlight lang="java">
package consoles;
 
Line 123 ⟶ 124:
}
</syntaxhighlight>
</source>
 
===== Implementation =====
 
<sourcesyntaxhighlight lang="java">
import consoles.*;
 
Line 155 ⟶ 156:
}
}
</syntaxhighlight>
</source>
 
==== C# (C Sharp .NetNET) ====
 
[[C Sharp (programming language)|C#]], like Java, supports namespaces although the pattern remains useful in specific cases.
Line 165 ⟶ 166:
===== Definition =====
 
<sourcesyntaxhighlight lang="csharp">
using System;
using System.IO;
using System.Text;
 
namespace Consoles {;
 
public sealed class MainModule
{
private static MainModule Singleton = null;
public InputStream input = null;
Line 180 ⟶ 182:
// ...
 
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();");
//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: ...
}
 
// ...
}
}
</syntaxhighlight>
</source>
 
===== Implementation =====
 
<sourcesyntaxhighlight lang="csharp">
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();
}
}
</syntaxhighlight>
</source>
 
=== Prototype-based programming languages ===
Line 294 ⟶ 307:
===== Definition =====
 
<sourcesyntaxhighlight lang="javascript">
function ConsoleClass() {
var Input = null;
Line 351 ⟶ 364:
}
</syntaxhighlight>
</source>
 
===== Implementation =====
 
<sourcesyntaxhighlight lang="javascript">
function ConsoleDemo() {
var Console = null;
Line 380 ⟶ 393:
}
}
</syntaxhighlight>
</source>
 
=== Procedural programming languages ===
Line 394 ⟶ 407:
===== Definition =====
 
<sourcesyntaxhighlight lang="php">
<?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 ouputsoutputs 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
}
</syntaxhighlight>
?>
</source>
 
===== Implementation =====
 
<sourcesyntaxhighlight lang="php">
// filename: consoledemo.php
<?php
// 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();
}
}
</syntaxhighlight>
?>
</source>
 
==== C ====
Line 482 ⟶ 502:
===== Definition header module =====
 
<sourcesyntaxhighlight lang="c">
// filename: "consoles.h"
 
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void consoles_prepare();
void consoles_unprepare();
 
void consoles_prepare();
// ...
void consoles_unprepare();
 
void consoles_printNewLine();
// ...
 
void consoles_printString(char* Value);
void consoles_printNewLine();
void consoles_printInteger(int Value);
 
void consoles_printBoolean(bool Value);
void consoles_printString(char* Value);
void consoles_scanNewLineconsoles_printInteger(int Value);
void consoles_printBoolean(bool Value);
 
void consoles_scanString(char* Value);
void consoles_scanIntegerconsoles_scanNewLine(int* Value);
 
void consoles_scanBoolean(bool* Value);
void consoles_scanString(char* Value);
</source>
void consoles_scanInteger(int* Value);
void consoles_scanBoolean(bool* Value);
</syntaxhighlight>
 
===== Definition body module =====
 
<sourcesyntaxhighlight lang="c">
// 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_prepare() {
// ...
// code that prepares console
}
void consoles_printNewLine() {
 
printf("\n");
void consoles_unprepare() {
}
// code that unprepares console
}
void consoles_printString(char* Value) {
 
printf("%s", Value);
// ...
}
 
void consoles_printIntegerconsoles_printNewLine(int Value) {
printf("%d\n", &Value);
}
 
void consoles_printBooleanconsoles_printString(boolchar* Value) {
printf("%s", if (Value);
}
{
 
printf("true");
void consoles_printInteger(int Value) {
}
printf("%d", &Value);
else
}
{
 
printf("false");
void consoles_printBoolean(bool Value) {
}
printf((Value) ? ("true") : ("false"));
}
}
 
void consoles_scanNewLine() {
void consoles_scanNewLine() {
getch();
getch();
}
}
 
void consoles_scanString(char* Value) {
void scanfconsoles_scanString("%s",char* Value); {
scanf("%s", Value);
}
}
 
void consoles_scanInteger(int* Value) {
void scanfconsoles_scanInteger("%d",int* Value); {
scanf("%d", Value);
}
}
 
void consoles_scanBoolean(bool* Value) {
void consoles_scanBoolean(bool* Value) {
char temp[512];
scanf("%s",char temp)[512];
scanf("%s", temp);
 
*Value = (strcmp(Temp, "true") == 0);
*Value = (strcmp(Temp, "true") == 0);
}
}
</source>
</syntaxhighlight>
 
===== Implementation =====
 
<sourcesyntaxhighlight lang="c">
// 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();
int main()
return ErrorCode;
{
}
ErrorCode Result = 0;
</source>
 
consoledemo_prepare();
ErrorCode = consoledemo_execute();
consoledemo_unprepare();
 
return ErrorCode;
}
</syntaxhighlight>
 
==== Procedural Pascal ====
Line 619 ⟶ 632:
===== Definition =====
 
<sourcesyntaxhighlight lang="pascal">
unit consoles;
(* filename: "consoles.pas" *)
Line 691 ⟶ 704:
end;
end;
</syntaxhighlight>
</source>
 
===== Implementation =====
 
<sourcesyntaxhighlight lang="pascal">
program consoledemo;
// filename: "consoles.pas"
Line 725 ⟶ 738:
unprepare();
end.
</syntaxhighlight>
</source>
 
== Comparisons to other concepts ==