mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 12:59:13 +00:00
FirstCut++
FirstCut++
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* The Data Mapper (DM) is a layer of software that separates the in-memory
|
||||
* objects from the database. Its responsibility is to transfer data between the
|
||||
* two and also to isolate them from each other. With Data Mapper the in-memory
|
||||
* objects needn't know even that there's a database present; they need no SQL
|
||||
* interface code, and certainly no knowledge of the database schema. (The
|
||||
* database schema is always ignorant of the objects that use it.) Since it's a
|
||||
* form of Mapper , Data Mapper itself is even unknown to the domain layer.
|
||||
* <p>
|
||||
* The below example demonstrates basic CRUD operations: Create, Read, Update,
|
||||
* and Delete.
|
||||
*
|
||||
*/
|
||||
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 void prepare() throws FileNotFoundException {
|
||||
filePrinterModule = FilePrinterModule.getSingleton();
|
||||
|
||||
filePrinterModule.prepare(OUTPUT_FILE, ERROR_FILE);
|
||||
}
|
||||
|
||||
public static void unprepare() {
|
||||
filePrinterModule.unprepare();
|
||||
}
|
||||
|
||||
public static final void execute(final String... args) {
|
||||
filePrinterModule.printString("Hello World");
|
||||
}
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args
|
||||
* command line args.
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public static final void main(final String... args)
|
||||
throws FileNotFoundException {
|
||||
prepare();
|
||||
execute(args);
|
||||
unprepare();
|
||||
}
|
||||
|
||||
private App() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
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
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
public final class FilePrinterModule {
|
||||
|
||||
private static final Logger logger = Logger
|
||||
.getLogger(FilePrinterModule.class);
|
||||
|
||||
private static FilePrinterModule singleton = null;
|
||||
|
||||
public PrintStream output = null;
|
||||
public PrintStream error = null;
|
||||
|
||||
private FilePrinterModule() {
|
||||
}
|
||||
|
||||
public static final FilePrinterModule getSingleton() {
|
||||
|
||||
if (FilePrinterModule.singleton == null) {
|
||||
FilePrinterModule.singleton = new FilePrinterModule();
|
||||
}
|
||||
|
||||
return FilePrinterModule.singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public final void prepare(final String outputFile, final String errorFile)
|
||||
throws FileNotFoundException {
|
||||
|
||||
logger.debug("MainModule::prepare();");
|
||||
|
||||
this.output = new PrintStream(new FileOutputStream(outputFile));
|
||||
this.error = new PrintStream(new FileOutputStream(errorFile));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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("MainModule::unprepare();");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public final void printString(final String value) {
|
||||
this.output.print(value);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public final void printErrorString(final String value) {
|
||||
this.error.print(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user