SecondCut++
SecondCut++
This commit is contained in:
@ -35,23 +35,35 @@ import java.io.FileNotFoundException;
|
||||
*/
|
||||
public final class App {
|
||||
|
||||
private static final String OUTPUT_FILE = "output.txt";
|
||||
private static final String ERROR_FILE = "error.txt";
|
||||
|
||||
public static FilePrinterModule filePrinterModule = null;
|
||||
public static FileLoggerModule fileLoggerModule = null;
|
||||
public static ConsoleLoggerModule consoleLoggerModule = null;
|
||||
|
||||
public static void prepare() throws FileNotFoundException {
|
||||
filePrinterModule = FilePrinterModule.getSingleton();
|
||||
|
||||
fileLoggerModule = FileLoggerModule.getSingleton();
|
||||
consoleLoggerModule = ConsoleLoggerModule.getSingleton();
|
||||
|
||||
filePrinterModule.prepare(OUTPUT_FILE, ERROR_FILE);
|
||||
/* Prepare modules */
|
||||
fileLoggerModule.prepare();
|
||||
consoleLoggerModule.prepare();
|
||||
}
|
||||
|
||||
public static void unprepare() {
|
||||
filePrinterModule.unprepare();
|
||||
|
||||
/* Close all resources */
|
||||
fileLoggerModule.unprepare();
|
||||
consoleLoggerModule.unprepare();
|
||||
}
|
||||
|
||||
public static final void execute(final String... args) {
|
||||
filePrinterModule.printString("Hello World");
|
||||
|
||||
/* Send logs on file system */
|
||||
fileLoggerModule.printString("Message");
|
||||
fileLoggerModule.printErrorString("Error");
|
||||
|
||||
/* Send logs on console */
|
||||
consoleLoggerModule.printString("Message");
|
||||
consoleLoggerModule.printErrorString("Error");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,106 @@
|
||||
/**
|
||||
* The MIT License Copyright (c) 2016 Amit Dixit
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.module;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* 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. 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.
|
||||
* <p>
|
||||
* The below example demonstrates a Console logger module, which can print
|
||||
* simple and error messages in two designated formats
|
||||
*/
|
||||
public class ConsoleLoggerModule {
|
||||
|
||||
private static final Logger logger = Logger
|
||||
.getLogger(ConsoleLoggerModule.class);
|
||||
|
||||
private static ConsoleLoggerModule singleton = null;
|
||||
|
||||
public PrintStream output = null;
|
||||
public PrintStream error = null;
|
||||
|
||||
private ConsoleLoggerModule() {
|
||||
}
|
||||
|
||||
public static final ConsoleLoggerModule getSingleton() {
|
||||
|
||||
if (ConsoleLoggerModule.singleton == null) {
|
||||
ConsoleLoggerModule.singleton = new ConsoleLoggerModule();
|
||||
}
|
||||
|
||||
return ConsoleLoggerModule.singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public final void prepare() {
|
||||
|
||||
logger.debug("ConsoleLoggerModule::prepare();");
|
||||
|
||||
this.output = new PrintStream(System.out);
|
||||
this.error = new PrintStream(System.err);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public final void unprepare() {
|
||||
|
||||
if (this.output != null) {
|
||||
|
||||
this.output.flush();
|
||||
this.output.close();
|
||||
}
|
||||
|
||||
if (this.error != null) {
|
||||
|
||||
this.error.flush();
|
||||
this.error.close();
|
||||
}
|
||||
|
||||
logger.debug("ConsoleLoggerModule::unprepare();");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public final void printString(final String value) {
|
||||
this.output.println(value);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public final void printErrorString(final String value) {
|
||||
this.error.println(value);
|
||||
}
|
||||
}
|
@ -1,11 +1,3 @@
|
||||
package com.iluwatar.module;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* The MIT License Copyright (c) 2016 Amit Dixit
|
||||
*
|
||||
@ -27,39 +19,60 @@ import org.apache.log4j.Logger;
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
public final class FilePrinterModule {
|
||||
package com.iluwatar.module;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* 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. 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.
|
||||
* <p>
|
||||
* The below example demonstrates a File logger module, which can print simple
|
||||
* and error messages in two designated files
|
||||
*/
|
||||
public final class FileLoggerModule {
|
||||
|
||||
private static final Logger logger = Logger
|
||||
.getLogger(FilePrinterModule.class);
|
||||
.getLogger(FileLoggerModule.class);
|
||||
|
||||
private static FilePrinterModule singleton = null;
|
||||
private static FileLoggerModule singleton = null;
|
||||
|
||||
private static final String OUTPUT_FILE = "output.txt";
|
||||
private static final String ERROR_FILE = "error.txt";
|
||||
|
||||
public PrintStream output = null;
|
||||
public PrintStream error = null;
|
||||
|
||||
private FilePrinterModule() {
|
||||
private FileLoggerModule() {
|
||||
}
|
||||
|
||||
public static final FilePrinterModule getSingleton() {
|
||||
public static final FileLoggerModule getSingleton() {
|
||||
|
||||
if (FilePrinterModule.singleton == null) {
|
||||
FilePrinterModule.singleton = new FilePrinterModule();
|
||||
if (FileLoggerModule.singleton == null) {
|
||||
FileLoggerModule.singleton = new FileLoggerModule();
|
||||
}
|
||||
|
||||
return FilePrinterModule.singleton;
|
||||
return FileLoggerModule.singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public final void prepare(final String outputFile, final String errorFile)
|
||||
throws FileNotFoundException {
|
||||
public final void prepare() throws FileNotFoundException {
|
||||
|
||||
logger.debug("MainModule::prepare();");
|
||||
logger.debug("FileLoggerModule::prepare();");
|
||||
|
||||
this.output = new PrintStream(new FileOutputStream(outputFile));
|
||||
this.error = new PrintStream(new FileOutputStream(errorFile));
|
||||
this.output = new PrintStream(new FileOutputStream(OUTPUT_FILE));
|
||||
this.error = new PrintStream(new FileOutputStream(ERROR_FILE));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,7 +92,7 @@ public final class FilePrinterModule {
|
||||
this.error.close();
|
||||
}
|
||||
|
||||
logger.debug("MainModule::unprepare();");
|
||||
logger.debug("FileLoggerModule::unprepare();");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -87,7 +100,7 @@ public final class FilePrinterModule {
|
||||
* @param value
|
||||
*/
|
||||
public final void printString(final String value) {
|
||||
this.output.print(value);
|
||||
this.output.println(value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,6 +108,6 @@ public final class FilePrinterModule {
|
||||
* @param value
|
||||
*/
|
||||
public final void printErrorString(final String value) {
|
||||
this.error.print(value);
|
||||
this.error.println(value);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user