Java 동적 로딩

코딩/JAVA 2014. 9. 12. 22:25

서비스 객체의 메소드를 등록, 검색 객체가 부르는 방식

등록, 검색 객체가 서비스 객체의 주소값을 가지고 있음


상속, 구현 구조:

foodstoreservice-->foodStoreServiceUI

task-->abstractUI-->searchNearStoreUI


등록, 검색 객체는 MakeInstance 객체를 통해 서비스 객체의 태스크맵에 저장됨

MakeInstance객체는 등록 객체 등을 만든 후 불러오기 위해 txt 파일이 필요



package org.thinker.fs;

import java.util.Map;

import java.util.Scanner;

public class FoodStoreUI {

private FoodStoreService service;

private Scanner scanner;

private Map<String, Task> taskMap;

public FoodStoreUI(FoodStoreService service)throws Exception {

super();

this.service = service;

this.scanner = new Scanner(System.in);

this.taskMap = MakeInstance.loadTask("C:\\zzz\\store.txt", service);

}

public void execute(){

System.out.println("TaskName ");

String taskName = scanner.nextLine();

Task task = taskMap.get(taskName);

try {

task.doJob();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

execute();

}

}


---------------------------------------------------------------------------------------------


package org.thinker.fs;

import java.io.FileInputStream;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Properties;

public class MakeInstance {

public static Map<String, Task> loadTask(String fileName, FoodStoreService service) throws Exception {

Map<String, Task> taskMap = new HashMap<String, Task>();

Properties prop = new Properties();

prop.load(new FileInputStream(fileName));

Iterator names = prop.keySet().iterator();

while(names.hasNext()){

String key = (String)names.next();

String value = (String)prop.get(key);

System.out.println(key +":" + value);

Task obj = (Task)Class.forName(value).newInstance();

obj.setService(service);

taskMap.put(key, obj);

}

return taskMap;

}

}



---------------------------------------------------------------------------------------------


package org.thinker.fs;

import java.util.List;

public interface FoodStoreService {

public void regist(FoodStoreVO vo)throws Exception;

public FoodStoreVO findByName(String name)throws Exception;

public List<FoodStoreVO> findNearStores(Location currentPos)throws Exception;

}



package org.thinker.fs;

import java.util.List;

public class FoodStoreServiceImpl implements FoodStoreService {

private FoodStoreDAO dao;

@Override

public void regist(FoodStoreVO vo) throws Exception {

// TODO Auto-generated method stub

dao.instance.saveData(null);

}

@Override

public FoodStoreVO findByName(String name) throws Exception {

// TODO Auto-generated method stub

return null;

}

@Override

public List<FoodStoreVO> findNearStores(Location currentPos)

throws Exception {

// TODO Auto-generated method stub

return null;

}

}


---------------------------------------------------------------------------------------------


package org.thinker.fs;

public interface Task {

public void setService(FoodStoreService service);

public void doJob()throws Exception;

}



package org.thinker.fs;

import java.util.Scanner;

public abstract class AbstractUI implements Task{

protected FoodStoreService service;

protected Scanner scanner;

public AbstractUI() {

super();

this.scanner = new Scanner(System.in);

}

@Override

public void setService(FoodStoreService service) {

this.service = service;

}

}





package org.thinker.fs;

public class RegistStoreUI extends AbstractUI {

@Override

public void doJob() throws Exception {

System.out.println("가게이름 ");

String name = scanner.nextLine();

}

}



package org.thinker.fs;

public class SearchNearStoreUI extends AbstractUI {

@Override

public void doJob() throws Exception {

System.out.println("Search.....");

}

}



---------------------------------------------------------------------------------------------


store.txt

NEAR=org.thinker.fs.SearchNearStoreUI

REGIST=org.thinker.fs.RegistStoreUI

Posted by 타다키치

댓글을 달아 주세요