e01.org

module pattern

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).

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). 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: ... } // ... } 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(); } } 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: ... } // ... } 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(); } } 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 } // ... } 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(); } } // 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(); } // 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); // filename: "consoles.c" include include include 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[512]; scanf("%s", temp); *value = (strcmp(temp, "true") == 0); } // 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; } 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; 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.

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"module pattern". 1784849413. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1365718162

JSON record · API collection · citation policy