0
1

More than 1 year has passed since last update.

【Java】FactoryMethodを使ったファイル読込み機能の実装

Last updated at Posted at 2022-12-13

メモ

自分用のメモ

クラス図

クラス図0.png

実装

InterfaceFileReader.java
package factoryMethodPattern;

import java.io.IOException;

public interface InterfaceFileReader {

	//共通して使うメソッド1を定義する(ファイルを読み込むメソッド)
	public int readFile() throws IOException;

	//共通して使うメソッド2を定義する(ファイルを閉じるメソッド)
	public void closeFile() throws IOException;
}


FileReaderFactory.java
package factoryMethodPattern;

import java.io.File;
import java.io.FileNotFoundException;

public class FileReaderFactory {

	public static InterfaceFileReader create(String fileType , File filePath) throws FileNotFoundException{

		//CSVファイル
		if(fileType.equals("CSV")){
			return new CSVFileReader(filePath);
		}

		//固定長ファイル
		if(fileType.equals("FixedLength")){
			return new FixedLengthFileReader(filePath);
		}

		//XMLファイル
		if(fileType.equals("XML")){
			return new XMLFileReader(filePath);
		}

		return null;
	}
}

CSVFileReader.java
package factoryMethodPattern;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CSVFileReader implements InterfaceFileReader{
	//フィールド
	private FileInputStream fileInputStream;

	//コンストラクタ
	public  CSVFileReader(File csvFileReader) throws FileNotFoundException {
		fileInputStream = new FileInputStream(csvFileReader);
	}

	//ファイルを読込むメソッド
	@Override 
	public int readFile() throws IOException {
		System.out.println("CSVファイル特有の処理をします");
		int byteData = 0;

		if(fileInputStream != null){
			byteData = fileInputStream.read();
		}
		return byteData;
	}

	//ファイルをクローズするメソッド
	@Override 
	public void closeFile() throws IOException {
		if(fileInputStream != null){
			fileInputStream.close();
		}
	}
}


FixedLengthFileReader.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FixedLengthFileReader implements InterfaceFileReader{
	//フィールド
	private FileInputStream fileInputStream;

	//コンストラクタ
	public  FixedLengthFileReader(File fixedLengthFile) throws FileNotFoundException {
		fileInputStream = new FileInputStream(fixedLengthFile);
	}

	//ファイルを読込むメソッド
	@Override 
	public int readFile() throws IOException {
		System.out.println("固定長ファイル特有の処理をします");
		int byteSize = 0;

		if(fileInputStream != null){
			byteSize = fileInputStream.read();
		}
		return byteSize;
	}

	//ファイルをクローズするメソッド
	@Override
	public void closeFile() throws IOException {
		if(fileInputStream != null){
			fileInputStream.close();
		}
	}
}

XMLFileReader.java

package factoryMethodPattern;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class XMLFileReader implements InterfaceFileReader{
	//フィールド
	private FileInputStream fileInputStream;

	//コンストラクタ
	public  XMLFileReader(File xmlFileReader) throws FileNotFoundException {
		fileInputStream = new FileInputStream(xmlFileReader);
	}

	//ファイルを読込むメソッド
	@Override
	public int readFile() throws IOException {
		System.out.println("XMLファイル特有の処理をします");
		int byteSize = 0;

		if(fileInputStream != null){
			byteSize = fileInputStream.read();
		}
		return byteSize;
	}

	//ファイルをクローズするメソッド
	@Override
	public void closeFile() throws IOException {
		if(fileInputStream != null){
			fileInputStream.close();
		}
	}
}
Main.java
package factoryMethodPattern;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {
	private static InterfaceFileReader interfaceFileReader;


	public static void main(String[] args) {
		System.out.println("処理を開始します。");

		// fileType filePathを変更することで処理を切り替えることができる。
		String fileType = "CSV";
		File   filePath = new File("test.csv");

		try {
			//FileReaderFactoryのstaticメソッドを呼び出し。
			interfaceFileReader = FileReaderFactory.create(fileType, filePath);
			if(interfaceFileReader == null){
				System.out.println("処理を異常終了します。");
				System.exit(1);
			}

			//ファイル読込み数の取得
			int readData = interfaceFileReader.readFile();

			//int型で表示//char型で表示
			System.out.println("int型 : " + readData);
			System.out.println("char型: " + (char)readData  );

			System.out.println("処理を終了します。");

		} catch (FileNotFoundException e) {
			System.out.println("読込みファイルが見つかりません。");
			e.printStackTrace();

		} catch (IOException e) {
			System.out.println("入出力例外発生");
			e.printStackTrace();
		}
	}
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1