본문 바로가기
백엔드/Java

인터페이스 활용

by david100gom 2013. 3. 6.
728x90

인터페이스 활용 예


action() 메소드를 정의한 인터페이스 생성

1
2
3
4
5
public interface IFInfo {
 
    public void action();
 
}
cs

IFInfo 를 구현한 클래스 2개 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class CarMan implements IFInfo{
 
    @Override
 
    public void action() {
 
        System.out.print("Driving.....");
 
    }
 
}
 
 
 
public class AirMan implements IFInfo{
 
    @Override
 
    public void action() {
 
        System.out.print("Flying.....");
 
    }
 
}
cs

if-info.properties 파일에는 아래와 같이 작성 action=AirMan (또는 action=CarMan)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.FileInputStream;
 
import java.util.Iterator;
 
import java.util.Properties;
 
import java.util.Set;
 
 
 
public class MainTest {
 
    public static void main(String[] args) {
 
        FileInputStream fis = null;
 
        Properties prop = new Properties();
 
        String file = "./if-info.properties";
 
        try {
 
            fis = new FileInputStream(file);
 
            prop.load(fis);
 
            Set keyValue = prop.keySet();
 
            Iterator it = keyValue.iterator();
 
            Class c = null;
 
            while(it.hasNext()) {
 
                String key = (String)it.next();
 
                String value = prop.getProperty(key);
 
                c = Class.forName(value);
 
            }
 
 
 
            // 상기 c = Class.forName(value); 의 value 가 어느 클래스인가에 따라서 (동적 로딩)
 
            // CarMan : Driving.....
 
            // AirMan : Flying.....
 
            IFInfo iff = (IFInfo) c.newInstance();
 
            iff.action();
 
 
 
        } catch (Exception e) {
 
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 
        }
 
    }
 
}
cs


728x90

'백엔드 > Java' 카테고리의 다른 글

JUnit 4.5 에러  (0) 2019.03.13
Spring @Autowired 사용시 주의점  (0) 2019.03.11
Spring + Mybatis + Junit 단위 테스트  (0) 2017.09.06
PowerMockup  (0) 2013.10.09
jstat - JVM 통계 데이터 감시 툴  (1) 2009.08.12
자바 JVM 옵션 리스트  (1) 2009.07.01
자바 template engine 프리마커  (1) 2009.06.20
자바 윈도우 환경설정  (0) 2009.06.14

댓글