当前位置:首页 > 开发 > 编程语言 > 正则表达式 > 正文

Loan pattern in Java

发表于: 2013-08-07   作者:cywhoyi   来源:转载   浏览次数:
摘要: 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 w

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 pattern in Java

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Decorator之前 在Java程序中,我们常会在IO的时候用到类似于如下的代码: InputStream in = new Buf
1. The story for Factory pattern Factory design pattern is used for creating an object based
Prototype design pattern is used when very similar objects frequently are required. Prototype
Adapter pattern is frequently used in modern Java frameworks. It comes into place when you wa
3.状态模式(state pattern) 状态模式实现的代码不复杂,不过难的是不知道什么时候用上 书上写得很
4.策略模式(Strategy pattern) 策略模式我个人觉得自己平时用得很多,怎么说呢,其实策略模式也就
尝试用简单的语言,讲述复杂的事情。 如发现文中的错误,或内容需要补充,忘不吝指教! CHALLENGE 2
Command Pattern 餐厅中的命令模式: 客人给服务员下菜单,服务员记录下来,给厨师,让厨师做出美丽
Abstract Factory pattern adds another layer of abstraction for Factory pattern. If we compare
先从起源说起。在设计界面时,大家可以注意到这样的一种情况,同样的菜单控件,在不同的应用环境中
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号