Skip to content

Commit 7015e95

Browse files
committed
SecondCut++
SecondCut++
1 parent 2a77ac2 commit 7015e95

7 files changed

Lines changed: 397 additions & 66 deletions

File tree

module/README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,22 @@ title: Module
44
folder: module
55
permalink: /patterns/module/
66
pumlid: JShB3OGm303HLg20nFVjnYGM1CN6ycTfVtFSsnjfzY5jPgUqkLqHwXy0mxUU8wuyqidQ8q4IjJqCO-QBWGOtVh5qyd5AKOmW4mT6Nu2-ZiAekapH_hkcSTNa-GC0
7-
categories: Persistence Tier
7+
categories: Creational Pattern
88
tags:
99
- Java
1010
- Difficulty-Beginner
1111
---
1212

1313
## Intent
14-
A layer of mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself
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.
1515

1616
![alt text](./etc/module.png "Module")
1717

1818
## Applicability
19-
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.
20-
2119
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.
2220

2321
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.
2422

25-
It supports specific cases where a class or object can be considered structured, procedural data. And, vice versa, migrate structured, procedural data, and considered as object-oriented.
26-
2723
## Credits
2824

2925
* [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/src/main/java/com/iluwatar/module/App.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,35 @@
3535
*/
3636
public final class App {
3737

38-
private static final String OUTPUT_FILE = "output.txt";
39-
private static final String ERROR_FILE = "error.txt";
40-
41-
public static FilePrinterModule filePrinterModule = null;
38+
public static FileLoggerModule fileLoggerModule = null;
39+
public static ConsoleLoggerModule consoleLoggerModule = null;
4240

4341
public static void prepare() throws FileNotFoundException {
44-
filePrinterModule = FilePrinterModule.getSingleton();
42+
43+
fileLoggerModule = FileLoggerModule.getSingleton();
44+
consoleLoggerModule = ConsoleLoggerModule.getSingleton();
4545

46-
filePrinterModule.prepare(OUTPUT_FILE, ERROR_FILE);
46+
/* Prepare modules */
47+
fileLoggerModule.prepare();
48+
consoleLoggerModule.prepare();
4749
}
4850

4951
public static void unprepare() {
50-
filePrinterModule.unprepare();
52+
53+
/* Close all resources */
54+
fileLoggerModule.unprepare();
55+
consoleLoggerModule.unprepare();
5156
}
5257

5358
public static final void execute(final String... args) {
54-
filePrinterModule.printString("Hello World");
59+
60+
/* Send logs on file system */
61+
fileLoggerModule.printString("Message");
62+
fileLoggerModule.printErrorString("Error");
63+
64+
/* Send logs on console */
65+
consoleLoggerModule.printString("Message");
66+
consoleLoggerModule.printErrorString("Error");
5567
}
5668

5769
/**
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) 2016 Amit Dixit
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+
import java.io.PrintStream;
23+
24+
import org.apache.log4j.Logger;
25+
26+
/**
27+
* The Module pattern can be considered a Creational pattern and a Structural
28+
* pattern. It manages the creation and organization of other elements, and
29+
* groups them as the structural pattern does. An object that applies this
30+
* pattern can provide the equivalent of a namespace, providing the
31+
* initialization and finalization process of a static class or a class with
32+
* static members with cleaner, more concise syntax and semantics.
33+
* <p>
34+
* The below example demonstrates a Console logger module, which can print
35+
* simple and error messages in two designated formats
36+
*/
37+
public class ConsoleLoggerModule {
38+
39+
private static final Logger logger = Logger
40+
.getLogger(ConsoleLoggerModule.class);
41+
42+
private static ConsoleLoggerModule singleton = null;
43+
44+
public PrintStream output = null;
45+
public PrintStream error = null;
46+
47+
private ConsoleLoggerModule() {
48+
}
49+
50+
public static final ConsoleLoggerModule getSingleton() {
51+
52+
if (ConsoleLoggerModule.singleton == null) {
53+
ConsoleLoggerModule.singleton = new ConsoleLoggerModule();
54+
}
55+
56+
return ConsoleLoggerModule.singleton;
57+
}
58+
59+
/**
60+
*
61+
* @throws FileNotFoundException
62+
*/
63+
public final void prepare() {
64+
65+
logger.debug("ConsoleLoggerModule::prepare();");
66+
67+
this.output = new PrintStream(System.out);
68+
this.error = new PrintStream(System.err);
69+
}
70+
71+
/**
72+
*
73+
*/
74+
public final void unprepare() {
75+
76+
if (this.output != null) {
77+
78+
this.output.flush();
79+
this.output.close();
80+
}
81+
82+
if (this.error != null) {
83+
84+
this.error.flush();
85+
this.error.close();
86+
}
87+
88+
logger.debug("ConsoleLoggerModule::unprepare();");
89+
}
90+
91+
/**
92+
*
93+
* @param value
94+
*/
95+
public final void printString(final String value) {
96+
this.output.println(value);
97+
}
98+
99+
/**
100+
*
101+
* @param value
102+
*/
103+
public final void printErrorString(final String value) {
104+
this.error.println(value);
105+
}
106+
}

module/src/main/java/com/iluwatar/module/FilePrinterModule.java renamed to module/src/main/java/com/iluwatar/module/FileLoggerModule.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
package com.iluwatar.module;
2-
3-
import java.io.FileNotFoundException;
4-
import java.io.FileOutputStream;
5-
import java.io.PrintStream;
6-
7-
import org.apache.log4j.Logger;
8-
91
/**
102
* The MIT License Copyright (c) 2016 Amit Dixit
113
*
@@ -27,39 +19,60 @@
2719
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2820
* SOFTWARE.
2921
*/
30-
public final class FilePrinterModule {
22+
package com.iluwatar.module;
23+
24+
import java.io.FileNotFoundException;
25+
import java.io.FileOutputStream;
26+
import java.io.PrintStream;
27+
28+
import org.apache.log4j.Logger;
29+
30+
/**
31+
* The Module pattern can be considered a Creational pattern and a Structural
32+
* pattern. It manages the creation and organization of other elements, and
33+
* groups them as the structural pattern does. An object that applies this
34+
* pattern can provide the equivalent of a namespace, providing the
35+
* initialization and finalization process of a static class or a class with
36+
* static members with cleaner, more concise syntax and semantics.
37+
* <p>
38+
* The below example demonstrates a File logger module, which can print simple
39+
* and error messages in two designated files
40+
*/
41+
public final class FileLoggerModule {
3142

3243
private static final Logger logger = Logger
33-
.getLogger(FilePrinterModule.class);
44+
.getLogger(FileLoggerModule.class);
45+
46+
private static FileLoggerModule singleton = null;
3447

35-
private static FilePrinterModule singleton = null;
48+
private static final String OUTPUT_FILE = "output.txt";
49+
private static final String ERROR_FILE = "error.txt";
3650

3751
public PrintStream output = null;
3852
public PrintStream error = null;
3953

40-
private FilePrinterModule() {
54+
private FileLoggerModule() {
4155
}
4256

43-
public static final FilePrinterModule getSingleton() {
57+
public static final FileLoggerModule getSingleton() {
4458

45-
if (FilePrinterModule.singleton == null) {
46-
FilePrinterModule.singleton = new FilePrinterModule();
59+
if (FileLoggerModule.singleton == null) {
60+
FileLoggerModule.singleton = new FileLoggerModule();
4761
}
4862

49-
return FilePrinterModule.singleton;
63+
return FileLoggerModule.singleton;
5064
}
5165

5266
/**
5367
*
5468
* @throws FileNotFoundException
5569
*/
56-
public final void prepare(final String outputFile, final String errorFile)
57-
throws FileNotFoundException {
70+
public final void prepare() throws FileNotFoundException {
5871

59-
logger.debug("MainModule::prepare();");
72+
logger.debug("FileLoggerModule::prepare();");
6073

61-
this.output = new PrintStream(new FileOutputStream(outputFile));
62-
this.error = new PrintStream(new FileOutputStream(errorFile));
74+
this.output = new PrintStream(new FileOutputStream(OUTPUT_FILE));
75+
this.error = new PrintStream(new FileOutputStream(ERROR_FILE));
6376
}
6477

6578
/**
@@ -79,22 +92,22 @@ public final void unprepare() {
7992
this.error.close();
8093
}
8194

82-
logger.debug("MainModule::unprepare();");
95+
logger.debug("FileLoggerModule::unprepare();");
8396
}
8497

8598
/**
8699
*
87100
* @param value
88101
*/
89102
public final void printString(final String value) {
90-
this.output.print(value);
103+
this.output.println(value);
91104
}
92105

93106
/**
94107
*
95108
* @param value
96109
*/
97110
public final void printErrorString(final String value) {
98-
this.error.print(value);
111+
this.error.println(value);
99112
}
100113
}

0 commit comments

Comments
 (0)