Content deleted Content added
m →C |
|||
(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 [[
This pattern can be implemented in several ways depending on the host programming language, such as the [[singleton pattern|singleton design pattern]], object-oriented [[
== 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 =====
<
package consoles;
Line 123 ⟶ 124:
}
</syntaxhighlight>
===== Implementation =====
<
import consoles.*;
Line 155 ⟶ 156:
}
}
</syntaxhighlight>
==== C# (C Sharp .
[[C Sharp (programming language)|C#]], like Java, supports namespaces although the pattern remains useful in specific cases.
Line 165 ⟶ 166:
===== Definition =====
<
using System;
using System.IO;
using System.Text;
namespace Consoles
{ 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();");
this.input
this.output = new OutputStream();
this.error
}
public void Unprepare()
{
this.output = null; this.input
this.error
//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>
===== Implementation =====
<
{ 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>
=== Prototype-based programming languages ===
Line 294 ⟶ 307:
===== Definition =====
<
function ConsoleClass() {
var Input = null;
Line 351 ⟶ 364:
}
</syntaxhighlight>
===== Implementation =====
<
function ConsoleDemo() {
var Console = null;
Line 380 ⟶ 393:
}
}
</syntaxhighlight>
=== Procedural programming languages ===
Line 394 ⟶ 407:
===== Definition =====
<
<?php
{ // code that prepares a "console"
{ // code that unprepares a "console"
{ // code that
{ // code that prints parameters
{ // code that prints parameters
{ // code that prints parameters
{ // code that looks for a new line
{ // code that stores data into parameters
{ // code that stores data into parameters
{ // code that stores data into parameters
</syntaxhighlight>
===== Implementation =====
<
// filename: consoledemo.php
{
}
{
}
{
}
{
}
</syntaxhighlight>
==== C ====
Line 482 ⟶ 502:
===== Definition header module =====
<
void consoles_prepare();
void consoles_unprepare();
// ...
void consoles_printNewLine();
void consoles_printString(char* Value);
void consoles_printBoolean(bool Value);
void consoles_scanString(char* Value);
void consoles_scanInteger(int* Value);
void consoles_scanBoolean(bool* Value);
</syntaxhighlight>
===== Definition body module =====
<
void consoles_prepare() {
// code that prepares console
}
void consoles_unprepare() {
// code that unprepares console
}
// ...
printf("%s",
}
void consoles_printInteger(int Value) {
printf("%d", &Value);
}
void consoles_printBoolean(bool Value) {
printf((Value) ? ("true") : ("false"));
}
void consoles_scanNewLine() {
getch();
}
void
scanf("%s", Value);
}
void
scanf("%d", Value);
}
void consoles_scanBoolean(bool* Value) {
scanf("%s", temp);
*Value = (strcmp(Temp, "true") == 0);
}
</syntaxhighlight>
===== Implementation =====
<
return 0;
int main()
{
ErrorCode Result = 0;
consoledemo_prepare();
ErrorCode = consoledemo_execute();
consoledemo_unprepare();
return ErrorCode;
}
</syntaxhighlight>
==== Procedural Pascal ====
Line 619 ⟶ 632:
===== Definition =====
<
unit consoles;
(* filename: "consoles.pas" *)
Line 691 ⟶ 704:
end;
end;
</syntaxhighlight>
===== Implementation =====
<
program consoledemo;
// filename: "consoles.pas"
Line 725 ⟶ 738:
unprepare();
end.
</syntaxhighlight>
== Comparisons to other concepts ==
|