Skip to content

Commit c94c8a3

Browse files
authored
Merge pull request iluwatar#512 from inbravo/master
Module pattern iluwatar#62
2 parents efc2c88 + 5d88068 commit c94c8a3

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-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="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+
<position height="-1" width="-1" x="401" y="190"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<class id="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+
<position height="-1" width="-1" x="686" y="176"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<class id="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+
<position height="-1" width="-1" x="558" y="501"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</class>
31+
<association id="4">
32+
<end type="SOURCE" refId="1" navigable="false">
33+
<attribute id="5" name="singleton"/>
34+
<multiplicity id="6" minimum="0" maximum="1"/>
35+
</end>
36+
<end type="TARGET" refId="1" navigable="true"/>
37+
<display labels="true" multiplicity="true"/>
38+
</association>
39+
<association id="7">
40+
<end type="SOURCE" refId="2" navigable="false">
41+
<attribute id="8" name="singleton"/>
42+
<multiplicity id="9" minimum="0" maximum="1"/>
43+
</end>
44+
<end type="TARGET" refId="2" navigable="true"/>
45+
<display labels="true" multiplicity="true"/>
46+
</association>
47+
<association id="10">
48+
<end type="SOURCE" refId="3" navigable="false">
49+
<attribute id="11" name="consoleLoggerModule"/>
50+
<multiplicity id="12" minimum="0" maximum="1"/>
51+
</end>
52+
<end type="TARGET" refId="2" navigable="true"/>
53+
<display labels="true" multiplicity="true"/>
54+
</association>
55+
<association id="13">
56+
<end type="SOURCE" refId="3" navigable="false">
57+
<attribute id="14" name="fileLoggerModule"/>
58+
<multiplicity id="15" minimum="0" maximum="1"/>
59+
</end>
60+
<end type="TARGET" refId="1" navigable="true"/>
61+
<display labels="true" multiplicity="true"/>
62+
</association>
63+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
64+
sort-features="false" accessors="true" visibility="true">
65+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
66+
<operations public="true" package="true" protected="true" private="true" static="true"/>
67+
</classifier-display>
68+
<association-display labels="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+
package com.iluwatar.module {
3+
class App {
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+
class ConsoleLoggerModule {
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+
class FileLoggerModule {
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+
<project xsi: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+
package com.iluwatar.module;
20+
21+
import java.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+
public final class App {
35+
36+
public static FileLoggerModule fileLoggerModule;
37+
public static ConsoleLoggerModule consoleLoggerModule;
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+
public static void prepare() throws FileNotFoundException {
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+
public static void unprepare() {
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+
public static void execute(final String... 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+
public static void main(final String... args) throws FileNotFoundException {
86+
prepare();
87+
execute(args);
88+
unprepare();
89+
}
90+
91+
private App() {}
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+
package com.iluwatar.module;
20+
21+
import java.io.PrintStream;
22+
23+
import org.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+
public final class ConsoleLoggerModule {
32+
33+
private static final Logger LOGGER = Logger.getLogger(ConsoleLoggerModule.class);
34+
35+
private static ConsoleLoggerModule singleton = null;
36+
37+
public PrintStream output = null;
38+
public PrintStream error = null;
39+
40+
private ConsoleLoggerModule() {}
41+
42+
/**
43+
* Static method to get single instance of class
44+
*
45+
* @return singleton instance of ConsoleLoggerModule
46+
*/
47+
public static ConsoleLoggerModule getSingleton() {
48+
49+
if (ConsoleLoggerModule.singleton == null) {
50+
ConsoleLoggerModule.singleton = new ConsoleLoggerModule();
51+
}
52+
53+
return ConsoleLoggerModule.singleton;
54+
}
55+
56+
/**
57+
* Following method performs the initialization
58+
*/
59+
public ConsoleLoggerModule prepare() {
60+
61+
LOGGER.debug("ConsoleLoggerModule::prepare();");
62+
63+
this.output = new PrintStream(System.out);
64+
this.error = new PrintStream(System.err);
65+
66+
return this;
67+
}
68+
69+
/**
70+
* Following method performs the finalization
71+
*/
72+
public void unprepare() {
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+
public void printString(final String value) {
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+
public void printErrorString(final String value) {
104+
this.error.println(value);
105+
}
106+
}

0 commit comments

Comments
 (0)