`
cywhoyi
  • 浏览: 421072 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Loan pattern in Java

 
阅读更多

se Case

Implement separation between the code that holds resource from that of accessing it such that the accessing code doesn’t need to manage the resources. The use case mentioned holds true when we write code to read/write to a file or querying SQL / NOSQL dbs. There are certainly API’s handled this with the help of AOP. But I thought if a pattern based approach could help us to deal with these kind of use case, that’s where I came to know about Loan Pattern (a.k.a lender lendee pattern).
 

What it does

Loan pattern takes a “lending approach” i.e the code which keep hold of the resources “lends” if to the calling code. The lender (a.k.a code which holds resources) manages the resources once the lendee (code accessing the resource) has used it (with no interest ). Lets get in to lender code:

package org.dxy.pattern.loan;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * This class is an illustration of using loan pattern(a.k.a lender-lendee
 * pattern)
 * 
 * @author ibyoung
 */
public class IOResourceLender {

	/**
	 * Interface to write data to the buffer. Clients using this class should
	 * provide impl of this interface
	 * 
	 * @author sysadmin
	 * 
	 */
	public interface WriteBlock {
		void call(BufferedWriter writer) throws IOException;
	}

	/**
	 * Interface to read data from the buffer. Clients using this class should
	 * provide impl of this interface
	 * 
	 * @author sysadmin
	 * 
	 */
	public interface ReadBlock {
		void call(BufferedReader reader) throws IOException;
	}

	/**
	 * method which loans / lends the resource. Here {@link FileWriter} is the
	 * resource lent. The resource is managed for the given impl of
	 * {@link WriteBlock}
	 * 
	 * @param fileName
	 * @param block
	 * @throws IOException
	 */
	public static void writeUsing(String fileName, WriteBlock block)
			throws IOException {
		File csvFile = new File(fileName);
		if (!csvFile.exists()) {
			csvFile.createNewFile();
		}
		FileWriter fw = new FileWriter(csvFile.getAbsoluteFile(), true);
		BufferedWriter bufferedWriter = new BufferedWriter(fw);
		block.call(bufferedWriter);
		bufferedWriter.close();
	}

	/**
	 * method which loans / lends the resource. Here {@link FileReader} is the
	 * resource lent. The resource is managed for the given impl of
	 * {@link ReadBlock}
	 * 
	 * @param fileName
	 * @param block
	 * @throws IOException
	 */
	public static void readUsing(String fileName, ReadBlock block)
			throws IOException {
		File inputFile = new File(fileName);
		FileReader fileReader = new FileReader(inputFile.getAbsoluteFile());
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		block.call(bufferedReader);
		bufferedReader.close();
	}

	// client code

	public void writeColumnNameToMetaFile(final String attrName,
			String fileName, final String[] colNames) throws IOException {
		IOResourceLender.writeUsing(fileName,
				new IOResourceLender.WriteBlock() {
					public void call(BufferedWriter out) throws IOException {
						StringBuilder buffer = new StringBuilder();
						for (String string : colNames) {
							buffer.append(string);
							buffer.append(",");
						}
						out.append(attrName + " = " + buffer.toString());
						out.newLine();
					}
				});
	}
}

 The example uses the loan pattern for a simple file IO operation. However this code could be further improved by providing abstract lenders and lendee.The code for this post is shared in the following gist https://gist.github.com/4481190 I welcome your comments and suggestions !!

 

分享到:
评论

相关推荐

    LOAN面向对向类程序

    面向对象类的一个列子 Loan::Loan(double r,int y,double a){ annualInterestRate=r; numberOfYears=y; loanAmount=a; } Loan::Loan(const Loan& loan){ annualInterestRate = loan.annualInterestRate; number...

    China’s mortgage loan growth improved in October.pdf

    China’s mortgage loan growth improved in October.pdf

    What Determine Loan Rate and Default Status in Financial Technology

    【贷款利率与违约状态在金融科技中的决定因素:印尼在线直接借贷的证据】 本文基于2014年至2018年期间三个印尼领先的在线点对点(P2P)借贷平台的大规模数据集,深入研究了平台利率和借款人违约状态的决定因素。...

    loan类 的修改

    loan类 的修改

    collectionJava源码-Loan-Collection-and-Recovery-Services:它是Java中的Loan收集和恢

    《Java源码解析:Loan Collection and Recovery Services》 在Java编程领域,理解并掌握系统级的贷款收集和恢复服务的源码是至关重要的。这涉及到金融应用开发中的核心功能,如贷款管理、逾期处理和债务回收等。在...

    predict-loan-defaulters+

    "predict-loan-defaulters+"数据集为此提供了丰富的资源,包含8个不同的数据包,旨在帮助我们进行基础建模工作。下面,我们将逐一解析这些数据包,并探讨它们在构建预测模型中的作用。 1. **trans.txt**:这个文件...

    深入理解Java中的final关键字_动力节点Java学院整理

    Java 中的 `final` 关键字是一个至关重要的概念,它被用来声明类、方法和变量,以确保它们在程序执行过程中不可更改。深入理解 `final` 关键字可以帮助开发者写出更加安全、高效的代码。 首先,`final` 关键字可以...

    购车贷款程序设计JAVA

    在JAVA中,我们可以创建一个`Loan`类来封装这些信息。`Loan`类包含贷款金额`loanAmount`、年利率`annualInterestRate`、贷款年限`loanTermYears`以及还款方式`repaymentMethod`等属性。此外,还需要定义相应的getter...

    Loan Application Builder

    根据提供的文件信息,“Loan Application Builder”似乎是一款与贷款申请相关的工具或服务,主要涉及工业企业的财务援助申请。从描述来看,“Calculation for load generation related to building”表明此工具还...

    LOAN AMORTIZATION(表格模板、XLS格式).xls

    LOAN AMORTIZATION(表格模板、XLS格式).xls

    JAVA英语单词.pdf

    Java英语单词大全 本资源是一个 Java英语...* loan:借出、借给 * operator:操作员 * protect:保卫、护卫 * private:私人的 这些单词都是Java开发中常用的计算机概念单词,了解这些单词对于编写Java程序非常重要。

    A bank loan type project that lets the user see by answering

    A bank loan type project that lets the user see by answering a few simple questions if the applicant is eligible for a bank loan or if the applicant would be a risk factor.

    Loan Prediction 数据集(贷款预测)

    Loan Prediction 数据集是保险领域最常引用的一个数据集。利用这个数据集,你可以充分体验到如何处理保险公司的数据,包括会遇到哪些挑战、需要什么策略、哪些变量会影响结果等。这是一个分类问题,数据集包含614行...

    after_loan.xlsx

    机器学习模型

    U10316025_HW8_14_4:如果输入的值小于或等于 0,则让 Loan 类抛出 IllegalArgumentException

    在Java编程语言中,异常处理是一项关键特性,用于处理程序执行期间可能出现的错误或异常情况。在给定的标题和描述中,我们关注的是`Loan`类如何在接收到小于或等于0的输入值时抛出`IllegalArgumentException`。`...

    java贷款计算器

    其次,`Loan.java`文件可能包含了贷款计算的主要逻辑。在这个类中,可能会定义一些属性如贷款金额、年利率、贷款期限等,并提供计算每月还款额的方法。等额本金法是指每期还款金额包括一部分本金和剩余本金产生的...

    Golub_G.H.__van_Loan_C.F._Matrix_computations__3ed

    完整版 Golub_G.H.__van_Loan_C.F._Matrix_computations__3ed.pdf

    Loan amortization.xls

    Loan amortization.xls

Global site tag (gtag.js) - Google Analytics