변경에 유리한 코드

 

분리 하자

1. 변하는 것, 변하지 않는 것

2. 관심사 다른 것

3. 중복 코드(ex : AOP)

 

Main1.java

package com.fastcampus.ch3.diCopy1;

import java.io.FileReader;
import java.util.Properties;

class Car {}
class SportsCar extends Car {}
class Truck extends Car{}

public class Main1 {

    public static void main(String[] args) {
        Car car = new SportsCar();
        System.out.println("car = " + car);
    }
    static Car getCar() throws Exception {
        Properties p = new Properties();
        p.load(new FileReader("config.txt"));

        Class clazz = Class.forName(p.getProperty("car"));

        return (Car)clazz.newInstance();
    }
}

 

config.txt

car=com.fastcampus.ch3.diCopy1.SportsCar

 

결과

car = com.fastcampus.ch3.diCopy1.SportsCar@6fb554cc

인스턴스를 변경하고 싶다면 config.txt 에 SportsCar 부분만 변경해 주면 된다.

 

주의
config.txt 최상위 폴더 위치에 생성해야 한다. 그렇지 않으면
java.io.FileNotFoundException 에러가 발생한다.

 

 

이번에는 메서드를 사용하자

 

Main1.java

package com.fastcampus.ch3.diCopy1;

import java.io.FileReader;
import java.util.Properties;

class Car {}
class SportsCar extends Car {}
class Truck extends Car {}

public class Main1 {

    public static void main(String[] args) throws Exception{
        Car car = (Car)getObject("car");
        System.out.println("car = " + car);
    }
    static Object getObject(String key) throws Exception {
        Properties p = new Properties();
        p.load(new FileReader("config.txt"));

        Class clazz = Class.forName(p.getProperty(key));

        return (Object)clazz.newInstance();
    }

//    static Car getCar() throws Exception {
//        Properties p = new Properties();
//        p.load(new FileReader("config.txt"));
//
//        Class clazz = Class.forName(p.getProperty("car"));
//
//        return (Car)clazz.newInstance();
//    }
}

 

config 내용을 Truck

결과

car = com.fastcampus.ch3.diCopy1.Truck@6be46e8f

 

Engine 클래스 추가

package com.fastcampus.ch3.diCopy1;

import java.io.FileReader;
import java.util.Properties;

class Car {}
class SportsCar extends Car {}
class Truck extends Car {}
class Engine {}

public class Main1 {

    public static void main(String[] args) throws Exception{
        Car car = (Car)getObject("car");
        Engine engine = (Engine) getObject("engine");
        System.out.println("car = " + car);
        System.out.println("engine = " + engine);
    }
    static Object getObject(String key) throws Exception {
        Properties p = new Properties();
        p.load(new FileReader("config.txt"));

        Class clazz = Class.forName(p.getProperty(key));

        return (Object)clazz.newInstance();
    }

//    static Car getCar() throws Exception {
//        Properties p = new Properties();
//        p.load(new FileReader("config.txt"));
//
//        Class clazz = Class.forName(p.getProperty("car"));
//
//        return (Car)clazz.newInstance();
//    }
}
car=com.fastcampus.ch3.diCopy1.SportsCar
engine=com.fastcampus.ch3.diCopy1.Engine

 

결과

car = com.fastcampus.ch3.diCopy1.SportsCar@614c5515
engine = com.fastcampus.ch3.diCopy1.Engine@77b52d12

'Spring > Ch3. Spring DI, AOP' 카테고리의 다른 글

Spring - Transaction  (0) 2022.09.29
Spring DI - Spring DI 흉내내기  (0) 2022.09.25
Ch3-3_Spring DI 활용하기 - 이론  (0) 2022.02.08
Ch3-2_Spring DI 흉내내기(2)  (0) 2022.02.07
Ch3-1_Spring DI 흉내내기  (0) 2022.02.07
복사했습니다!