Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc94c8a3

Browse files
authored
Merge pull requestiluwatar#512 from inbravo/master
Module patterniluwatar#62
2 parentsefc2c88 +5d88068 commitc94c8a3

File tree

12 files changed

+755
-0
lines changed

12 files changed

+755
-0
lines changed

‎module/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
layout:pattern
3+
title:Module
4+
folder:module
5+
permalink:/patterns/module/
6+
pumlid:JShB3OGm303HLg20nFVjnYGM1CN6ycTfVtFSsnjfzY5jPgUqkLqHwXy0mxUU8wuyqidQ8q4IjJqCO-QBWGOtVh5qyd5AKOmW4mT6Nu2-ZiAekapH_hkcSTNa-GC0
7+
categories:Creational Pattern
8+
tags:
9+
-Java
10+
-Difficulty-Beginner
11+
---
12+
13+
##Intent
14+
Module pattern is used to implement the concept of software modules, defined by modular programming, in a programming language with incomplete direct support for the concept.
15+
16+
![alt text](./etc/module.png"Module")
17+
18+
##Applicability
19+
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.
20+
21+
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.
22+
23+
##Credits
24+
25+
*[Module](https://en.wikipedia.org/wiki/Module_pattern)

‎module/etc/module.png

17.6 KB
Loading

‎module/etc/module.ucls

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagramversion="1.1.9"icons="true"always-add-relationships="false"generalizations="true"realizations="true"
3+
associations="true"dependencies="false"nesting-relationships="true"router="FAN">
4+
<classid="1"language="java"name="com.iluwatar.module.FileLoggerModule"project="module"
5+
file="/module/src/main/java/com/iluwatar/module/FileLoggerModule.java"binary="false"corner="BOTTOM_RIGHT">
6+
<positionheight="-1"width="-1"x="401"y="190"/>
7+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
8+
sort-features="false"accessors="true"visibility="true">
9+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
10+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
11+
</display>
12+
</class>
13+
<classid="2"language="java"name="com.iluwatar.module.ConsoleLoggerModule"project="module"
14+
file="/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java"binary="false"corner="BOTTOM_RIGHT">
15+
<positionheight="-1"width="-1"x="686"y="176"/>
16+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
17+
sort-features="false"accessors="true"visibility="true">
18+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
19+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
20+
</display>
21+
</class>
22+
<classid="3"language="java"name="com.iluwatar.module.App"project="module"
23+
file="/module/src/main/java/com/iluwatar/module/App.java"binary="false"corner="BOTTOM_RIGHT">
24+
<positionheight="-1"width="-1"x="558"y="501"/>
25+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
26+
sort-features="false"accessors="true"visibility="true">
27+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
28+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
29+
</display>
30+
</class>
31+
<associationid="4">
32+
<endtype="SOURCE"refId="1"navigable="false">
33+
<attributeid="5"name="singleton"/>
34+
<multiplicityid="6"minimum="0"maximum="1"/>
35+
</end>
36+
<endtype="TARGET"refId="1"navigable="true"/>
37+
<displaylabels="true"multiplicity="true"/>
38+
</association>
39+
<associationid="7">
40+
<endtype="SOURCE"refId="2"navigable="false">
41+
<attributeid="8"name="singleton"/>
42+
<multiplicityid="9"minimum="0"maximum="1"/>
43+
</end>
44+
<endtype="TARGET"refId="2"navigable="true"/>
45+
<displaylabels="true"multiplicity="true"/>
46+
</association>
47+
<associationid="10">
48+
<endtype="SOURCE"refId="3"navigable="false">
49+
<attributeid="11"name="consoleLoggerModule"/>
50+
<multiplicityid="12"minimum="0"maximum="1"/>
51+
</end>
52+
<endtype="TARGET"refId="2"navigable="true"/>
53+
<displaylabels="true"multiplicity="true"/>
54+
</association>
55+
<associationid="13">
56+
<endtype="SOURCE"refId="3"navigable="false">
57+
<attributeid="14"name="fileLoggerModule"/>
58+
<multiplicityid="15"minimum="0"maximum="1"/>
59+
</end>
60+
<endtype="TARGET"refId="1"navigable="true"/>
61+
<displaylabels="true"multiplicity="true"/>
62+
</association>
63+
<classifier-displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
64+
sort-features="false"accessors="true"visibility="true">
65+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
66+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
67+
</classifier-display>
68+
<association-displaylabels="true"multiplicity="true"/>
69+
</class-diagram>

‎module/etc/module.urm.puml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@startuml
2+
packagecom.iluwatar.module {
3+
classApp {
4+
+consoleLoggerModule : ConsoleLoggerModule {static}
5+
+fileLoggerModule : FileLoggerModule {static}
6+
-App()
7+
+execute(args : String[]) {static}
8+
+main(args : String[]) {static}
9+
+prepare() {static}
10+
+unprepare() {static}
11+
}
12+
classConsoleLoggerModule {
13+
-LOGGER : Logger {static}
14+
+error :PrintStream
15+
+output :PrintStream
16+
-singleton : ConsoleLoggerModule {static}
17+
-ConsoleLoggerModule()
18+
+getSingleton() : ConsoleLoggerModule {static}
19+
+prepare()
20+
+printErrorString(value : String)
21+
+printString(value : String)
22+
+unprepare()
23+
}
24+
classFileLoggerModule {
25+
-ERROR_FILE : String {static}
26+
-LOGGER : Logger {static}
27+
-OUTPUT_FILE : String {static}
28+
+error :PrintStream
29+
+output :PrintStream
30+
-singleton : FileLoggerModule {static}
31+
-FileLoggerModule()
32+
+getSingleton() : FileLoggerModule {static}
33+
+prepare()
34+
+printErrorString(value : String)
35+
+printString(value : String)
36+
+unprepare()
37+
}
38+
}
39+
FileLoggerModule--> "-singleton"FileLoggerModule
40+
App--> "-consoleLoggerModule"ConsoleLoggerModule
41+
ConsoleLoggerModule--> "-singleton"ConsoleLoggerModule
42+
App--> "-fileLoggerModule"FileLoggerModule
43+
@enduml

‎module/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2016 Amit Dixit
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
27+
<modelVersion>4.0.0</modelVersion>
28+
<parent>
29+
<groupId>com.iluwatar</groupId>
30+
<artifactId>java-design-patterns</artifactId>
31+
<version>1.14.0-SNAPSHOT</version>
32+
</parent>
33+
<artifactId>module</artifactId>
34+
<dependencies>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>log4j</groupId>
42+
<artifactId>log4j</artifactId>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* The MIT License Copyright (c) 2014 Ilkka Seppälä
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
packagecom.iluwatar.module;
20+
21+
importjava.io.FileNotFoundException;
22+
23+
/**
24+
* The Module pattern can be considered a Creational pattern and a Structural pattern. It manages
25+
* the creation and organization of other elements, and groups them as the structural pattern does.
26+
* An object that applies this pattern can provide the equivalent of a namespace, providing the
27+
* initialization and finalization process of a static class or a class with static members with
28+
* cleaner, more concise syntax and semantics.
29+
* <p>
30+
* The below example demonstrates a use case for testing two different modules: File Logger and
31+
* Console Logger
32+
*
33+
*/
34+
publicfinalclassApp {
35+
36+
publicstaticFileLoggerModulefileLoggerModule;
37+
publicstaticConsoleLoggerModuleconsoleLoggerModule;
38+
39+
/**
40+
* Following method performs the initialization
41+
*
42+
* @throws FileNotFoundException if program is not able to find log files (output.txt and
43+
* error.txt)
44+
*/
45+
publicstaticvoidprepare()throwsFileNotFoundException {
46+
47+
/* Create new singleton objects and prepare their modules */
48+
fileLoggerModule =FileLoggerModule.getSingleton().prepare();
49+
consoleLoggerModule =ConsoleLoggerModule.getSingleton().prepare();
50+
}
51+
52+
/**
53+
* Following method performs the finalization
54+
*/
55+
publicstaticvoidunprepare() {
56+
57+
/* Close all resources */
58+
fileLoggerModule.unprepare();
59+
consoleLoggerModule.unprepare();
60+
}
61+
62+
/**
63+
* Following method is main executor
64+
*
65+
* @param args for providing default program arguments
66+
*/
67+
publicstaticvoidexecute(finalString...args) {
68+
69+
/* Send logs on file system */
70+
fileLoggerModule.printString("Message");
71+
fileLoggerModule.printErrorString("Error");
72+
73+
/* Send logs on console */
74+
consoleLoggerModule.printString("Message");
75+
consoleLoggerModule.printErrorString("Error");
76+
}
77+
78+
/**
79+
* Program entry point.
80+
*
81+
* @param args command line args.
82+
* @throws FileNotFoundException if program is not able to find log files (output.txt and
83+
* error.txt)
84+
*/
85+
publicstaticvoidmain(finalString...args)throwsFileNotFoundException {
86+
prepare();
87+
execute(args);
88+
unprepare();
89+
}
90+
91+
privateApp() {}
92+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* The MIT License Copyright (c) 2014 Ilkka Seppälä
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
packagecom.iluwatar.module;
20+
21+
importjava.io.PrintStream;
22+
23+
importorg.apache.log4j.Logger;
24+
25+
/**
26+
* The ConsoleLoggerModule is responsible for showing logs on System Console
27+
* <p>
28+
* The below example demonstrates a Console logger module, which can print simple and error messages
29+
* in two designated formats
30+
*/
31+
publicfinalclassConsoleLoggerModule {
32+
33+
privatestaticfinalLoggerLOGGER =Logger.getLogger(ConsoleLoggerModule.class);
34+
35+
privatestaticConsoleLoggerModulesingleton =null;
36+
37+
publicPrintStreamoutput =null;
38+
publicPrintStreamerror =null;
39+
40+
privateConsoleLoggerModule() {}
41+
42+
/**
43+
* Static method to get single instance of class
44+
*
45+
* @return singleton instance of ConsoleLoggerModule
46+
*/
47+
publicstaticConsoleLoggerModulegetSingleton() {
48+
49+
if (ConsoleLoggerModule.singleton ==null) {
50+
ConsoleLoggerModule.singleton =newConsoleLoggerModule();
51+
}
52+
53+
returnConsoleLoggerModule.singleton;
54+
}
55+
56+
/**
57+
* Following method performs the initialization
58+
*/
59+
publicConsoleLoggerModuleprepare() {
60+
61+
LOGGER.debug("ConsoleLoggerModule::prepare();");
62+
63+
this.output =newPrintStream(System.out);
64+
this.error =newPrintStream(System.err);
65+
66+
returnthis;
67+
}
68+
69+
/**
70+
* Following method performs the finalization
71+
*/
72+
publicvoidunprepare() {
73+
74+
if (this.output !=null) {
75+
76+
this.output.flush();
77+
this.output.close();
78+
}
79+
80+
if (this.error !=null) {
81+
82+
this.error.flush();
83+
this.error.close();
84+
}
85+
86+
LOGGER.debug("ConsoleLoggerModule::unprepare();");
87+
}
88+
89+
/**
90+
* Used to print a message
91+
*
92+
* @param value will be printed on console
93+
*/
94+
publicvoidprintString(finalStringvalue) {
95+
this.output.println(value);
96+
}
97+
98+
/**
99+
* Used to print a error message
100+
*
101+
* @param value will be printed on error console
102+
*/
103+
publicvoidprintErrorString(finalStringvalue) {
104+
this.error.println(value);
105+
}
106+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp