๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Today I Learned(TIL)/์„ธ์…˜

Java ์„ธ์…˜_๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„์™€ ๋‹คํ˜•์„ฑ

by carrot0911 2024. 11. 30.

๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„์™€ ๋‹คํ˜•์„ฑ

11/29 11:10 ~ 12:30 (์•ฝ 1์‹œ๊ฐ„ 20๋ถ„ ์ง„ํ–‰)

 

โญ ์ค‘์š” ํ‚ค์›Œ๋“œ โญ

  • ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„
  • ๋‹คํ˜•์„ฑ
  • DI ์˜์กด์„ฑ ์ฃผ์ž…
  • IoC ์ œ์–ด์˜ ์—ญ์ „
  • ๊ฐ์ฒด ์ง€ํ–ฅ 4๊ฐ€์ง€ ํŠน์ง•

 

1. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™œ์šฉํ•œ ๋‹คํ˜•์„ฑ

์‹œ๋‚˜๋ฆฌ์˜ค 1 - ์ž๋™์ฐจ ์ฃผํ–‰ ํ”„๋กœ๊ทธ๋žจ

์ž„๋ฌด : ์ž๋™์ฐจ๋ฅผ ์ฃผํ–‰์‹œํ‚ค๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ์„ธ์š”.

senario1.directBinding ํŒจํ‚ค์ง€์— Car, GasEngine, ElectricEngine, Main ํด๋ž˜์Šค ์ƒ์„ฑ

package senario1.directBinding;

public class Car {

    // 1. ์†์„ฑ
    private GasEngine engine;


    // 2. ์ƒ์„ฑ์ž
    public Car(){
        this.engine = new GasEngine();
    }

    public void drive() {
        System.out.println("์ž๋™์ฐจ๊ฐ€ ์ฃผํ–‰๋ฉ๋‹ˆ๋‹ค.");
        this.engine.run();
    }
}
package senario1.directBinding;

public class GasEngine {

    // 1. ์†์„ฑ


    // 2. ์ƒ์„ฑ์ž


    // 3. ๊ธฐ๋Šฅ
    public void run() {
        System.out.println("๊ฐ€์Šค ์—”์ง„์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.");
    }
}
package senario1.directBinding;

public class Main {

    public static void main(String[] args) {

        Car carA = new Car();
        carA.drive();
    }
}

 

โœ”๏ธ ๊ฐ•๊ฒฐํ•ฉ

๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด์„ฑ์ด ๋†’์„ ๋•Œ ๊ฒฐํ•ฉ๋„๊ฐ€ ๊ฐ•ํ•˜๋‹ค. 

 

์‹œ๋‚˜๋ฆฌ์˜ค 1 - ์ƒˆ๋กœ์šด ์š”๊ตฌ์‚ฌํ•ญ

์ƒˆ๋กœ์šด ์—”์ง„(ElectricEngine)์ด ๊ฐœ๋ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ž๋™์ฐจ๊ฐ€ ์ƒˆ๋กœ ๊ฐœ๋ฐœ๋œ ์—”์ง„์„ ์‚ฌ์šฉํ•˜์„ธ ํ•ด์ฃผ์„ธ์š”.

package senario1.directBinding;

public class Car {

    // 1. ์†์„ฑ
    private GasEngine engine;
    private ElectircEngine electircEngine;  // ์ˆ˜์ •


    // 2. ์ƒ์„ฑ์ž
    public Car(){
        this.engine = new GasEngine();
        this.electircEngine = new ElectircEngine();  // ์ˆ˜์ •
    }

    public void drive() {
        System.out.println("์ž๋™์ฐจ๊ฐ€ ์ฃผํ–‰๋ฉ๋‹ˆ๋‹ค.");
        this.engine.run();
    }

    public void driveWithElectricEngine() {  // ์ˆ˜์ •
        System.out.println("์ž๋™์ฐจ๊ฐ€ ์ฃผํ–‰๋ฉ๋‹ˆ๋‹ค.");
        this.electircEngine.run();
    }
}
package senario1.directBinding;

public class ElectircEngine {

    // 3. ๊ธฐ๋Šฅ
    public void run() {
        System.out.println("์ „๊ธฐ ์—”์ง„์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.");
    }
}
package senario1.directBinding;

public class Main {

    public static void main(String[] args) {

        Car carA = new Car();
        //carA.drive();
        carA.driveWithElectricEngine();
    }
}

์ฝ”๋“œ๋ฅผ ์™„์„ฑํ–ˆ์ง€๋งŒ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ๊ฐ•ํ•œ ๊ฒฐํ•ฉ๋„(๊ฐ•๊ฒฐํ•ฉ)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
์ฝ”๋“œ๊ฐ€ ๊ฐ•๊ฒฐํ•ฉ์ด๋ผ๋ฉด ์œ ์ง€๋ณด์ˆ˜์— ์ข‹์ง€ ์•Š๋‹ค..

์‹œ๋‚˜๋ฆฌ์˜ค 1 - ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™œ์šฉํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

senario1.looseCoupling ํŒจํ‚ค์ง€์— Engine ์ธํ„ฐํŽ˜์ด์Šค์™€ Car, GasEngine, ElectricEngine, Main ํด๋ž˜์Šค ์ƒ์„ฑ

package senario1.looseCoupling;

public interface Engine {

    void run();
}
package senario1.looseCoupling;

public class GasEngine implements Engine{

    @Override
    public void run() {
        System.out.println("๊ฐ€์Šค ์—”์ง„์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.");
    }
}
package senario1.looseCoupling;

public class ElectricEngine implements Engine{

    @Override
    public void run() {
        System.out.println("์ „๊ธฐ ์—”์ง„์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.");
    }
}
package senario1.looseCoupling;

public class Car {

    // ์†์„ฑ
    private Engine engine;


    // ์ƒ์„ฑ์ž
    public Car(Engine engine) {
        this.engine = engine;
    }


    // ๊ธฐ๋Šฅ
    public void drive() {
        this.engine.run();
    }
}
package senario1.looseCoupling;

public class Main {

    public static void main(String[] args) {

        GasEngine gasEngine = new GasEngine();
        ElectricEngine electricEngine = new ElectricEngine();

        Car gasCar = new Car(electricEngine);
        gasCar.drive();
    }
}

์ƒˆ๋กœ์šด ์—”์ง„ ๊ฐ์ฒด๊ฐ€ ์ƒ๊ธด๋‹ค๊ณ  ํ•ด๋„ ์ž๋™์ฐจ ๊ฐ์ฒด๋Š” ๋ณ€๊ฒฝ์—์„œ ์ž์œ ๋กœ์›Œ์ง„๋‹ค.

 

2. ๋‹คํ˜•์„ฑ

ํ•˜๋‚˜์˜ ๊ฐ์ฒด๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ˜•์ฑ„๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.
์ด๋Ÿฌํ•œ ํŠน์ง•์„ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ๊ฐ์ฒด ๊ฐ„์˜ ๋Š์Šจํ•œ ๊ฒฐํ•ฉ๋„๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

3. ์˜์กด์„ฑ์ฃผ์ž…(DI : Dependency Injection)

๊ฐ์ฒด์— ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•ด ์ฃผ๋Š” ๊ฒƒ์„ ์˜์กด์„ฑ ์ฃผ์ž…์ด๋ผ๊ณ  ํ•œ๋‹ค.

public class Main {

    public static void main(String[] args) {
        GasEngine gasEngine = new GasEngine();
        Car gasCar = new Car(gasEngine); // ์˜์กด์„ฑ ์ฃผ์ž…! Car ๊ฐ์ฒด์— Engine ํƒ€์ž…์˜ ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Engine์„ ์ธ์Šคํ„ด์Šคํ™”ํ•ด์„œ ๋„ฃ์–ด์ค€๋‹ค!
        ...
    }
}
public class Car {
    ...
    public Car(Engine engine) {
        this.engine = engine;
    }
		...
}

 

4. ์ œ์–ด์˜ ์—ญ์ „(IoC : Inversion of Control)

ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ํ๋ฆ„์˜ ์ œ์–ด๊ถŒ์„ ์ œ3์˜ ์ฃผ์ฒด(์™ธ๋ถ€)๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.
IoC๋Š” ๊ฐ์ฒด ์ƒ์„ฑ, ์˜์กด์„ฑ ๊ด€๋ฆฌ๋ฅผ ์™ธ๋ถ€์—์„œ ๊ด€๋ฆฌํ•˜๋„๋ก ํ•˜์—ฌ ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๋Š” ์„ค๊ณ„ ์›์น™์ด๋‹ค.
์Šคํ”„๋ง์—์„œ๋Š” IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ด ์—ญํ• ์„ ํ•œ๋‹ค.

  • ๊ฐ์ฒด ์ƒ์„ฑ์— ๋Œ€ํ•œ ์ฑ…์ž„์„ ์™ธ๋ถ€๋กœ ์ „๊ฐ€
  • ์–ด๋–ค ์—”์ง„์„ ๊ตฌํ˜„ํ• ์ง€ ๊ฒฐ์ •์„ ์™ธ๋ถ€๋กœ ์ „๊ฐ€
public class Main {

    public static void main(String[] args) {
        ElectricEngine electricEngine = new ElectricEngine();  // ๊ฐ์ฒด ์ƒ์„ฑ์— ๋Œ€ํ•œ ์ฑ…์ž„์„ ์™ธ๋ถ€๋กœ ์ „๊ฐ€.
        GasEngine gasEngine = new GasEngine();

        Car electricCar = new Car(electricEngine);  // ์–ด๋–ค ์—”์ง„์„ ๊ตฌํ˜„ํ•  ์ง€ ์ฑ…์ž„์„ ์™ธ๋ถ€๋กœ ์ „๊ฐ€.
        Car gasCar = new Car(gasEngine);

        electricCar.drive();
        gasCar.drive();
    }
}

 

5. ์ƒ์†์„ ํ™œ์šฉํ•œ ๋‹คํ˜•์„ฑ

์‹œ๋‚˜๋ฆฌ์˜ค 2 - ์ž๋™์ฐจ ๋ถ€ํ’ˆ ๊ฒ€์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

์ž๋™์ฐจ ๋ถ€ํ’ˆ์„ ๊ฒ€์ˆ˜ํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ์„ธ์š”.

package senario2.directBinding;

/**
 * ๋ธŒ๋ ˆ์ดํฌ ๊ฐ์ฒด
 */
public class Brake {

    private String name;  // ๋ธŒ๋ ˆ์ดํฌ ์ด๋ฆ„
    private boolean isInspected;  // ๊ฒ€์ˆ˜ ์—ฌ๋ถ€(true/false)


    public Brake(String name) {
        this.name = name;
        this.isInspected = false;
    }

    // ๊ฒŒํ„ฐ
    public  boolean getIsInspected() {
        return this.isInspected;
    }

    // ์„ธํ„ฐ
    public void setInInspected(boolean isInspected) {
        this.isInspected = isInspected;
    }
}
package senario2.directBinding;

/**
 * ๋ธŒ๋ ˆ์ดํฌ ๊ฒ€์ˆ˜ ์—ญํ•  ๋‹ด๋‹น ๊ฐ์ฒด
 */
public class InspectService {

    // ๊ธฐ๋Šฅ
    public void inspect(Brake brake) {
        System.out.println("๊ฒ€์ˆ˜๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.");
        System.out.println(brake.getIsInspected());  // false
        brake.setInInspected(true);
        System.out.println("๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค -> " + brake.getIsInspected());  // true
    }
}
package senario2.directBinding;

public class Main {

    public static void main(String[] args) {
        Brake brake = new Brake("๋””์Šคํฌ ๋ธŒ๋ ˆ์ดํฌ");
        InspectService inspectService = new InspectService();

        inspectService.inspect(brake);
    }
}

์ƒˆ๋กœ์šด ๋ถ€ํ’ˆ(Sensor)์ด ์ถ”๊ฐ€๋œ๋‹ค๋ฉด..?

public class Main {

    public static void main(String[] args) {
        Brake brakeA = new Brake("๋””์Šคํฌ ๋ธŒ๋ ˆ์ดํฌ");
        Sensor sensor = new Sensor("์ถฉ๋Œ๋ฐฉ์ง€");

        InspectService inspectService = new InspectService();
        inspectService.inspect(brakeA);
        inspectService.inspect(sensor);  // ์—๋Ÿฌ๋ฐœ์ƒ
    }
}

 

์‹œ๋‚˜๋ฆฌ์˜ค 2 - ์ƒ์†์„ ํ™œ์šฉํ•œ ํ•ด๊ฒฐ

package senario2.looseCoupling;

/**
 * ์ž๋™์ฐจ ๋ถ€ํ’ˆ(๋ถ€๋ชจํด๋ž˜์Šค)
 * ๊ฒ€์ˆ˜๋งŒ ๋ฐ›์•„์•ผ ํ•ด!
 */
public abstract class CarPart {

    // ์†์„ฑ
    private boolean isInspected;

    // ์ƒ์„ฑ์ž
    public CarPart(boolean isInspected) {
        this.isInspected = isInspected;
    }

    // ๊ธฐ๋Šฅ
    public void inspect() {
        System.out.println("๊ฒ€์ˆ˜๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.");
        this.isInspected = true;
        System.out.println(" -> " + this.isInspected );
    }
}
package senario2.looseCoupling;

public class Brake extends CarPart{

    private String name;

    public Brake(String name) {
        super(false);
        this.name = name;
    }
    public void stop() {
        System.out.println("๋ฉˆ์ถœ ์ˆ˜ ์žˆ๋‹ค.");
    }
}
package senario2.looseCoupling;

public class InspectService {

    // ๊ธฐ๋Šฅ
    public void inspect(CarPart carPart) {
        carPart.inspect();
    }
}
package senario2.looseCoupling;

public class Main {

    public static void main(String[] args) {
        Brake brake = new Brake("๋””์Šคํฌ ๋ธŒ๋ ˆ์ดํฌ");
        InspectService inspectService = new InspectService();

        inspectService.inspect(brake);
    }
}

 

์ถ”์ƒํ™”

๊ฐ์ฒด ๋ณธ์งˆ์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ณธ์งˆ ์™ธ์˜ ์ •๋ณด๋ฅผ ์ˆจ๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค.

 

์ธํ„ฐํŽ˜์ด์Šค vs ์ƒ์†

ํŠน์ง• ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์†
์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ฑ โŒ ๊ตฌํ˜„์ฒด์—์„œ ๊ฐ์ž ๊ตฌํ˜„ ํ•„์š” โœ”๏ธ ๋ถ€๋ชจํด๋ž˜์Šค ๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ
๊ตฌํ˜„ ๊ฐ•์ œ์„ฑ โœ”๏ธ ๋ชจ๋“  ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ ๊ฐ•์ œ ๋ณด์žฅ โŒ ํ•„์š”ํ•œ ๊ฒƒ๋งŒ ์žฌ์ •์˜
๊ณ„์ธต ํ‘œํ˜„ โŒ ๋ถ€์ ํ•ฉ โœ”๏ธ ์ ํ•ฉ
๋‹ค์ค‘ ๊ตฌํ˜„ ๊ฐ€๋Šฅ โœ”๏ธ implements ์—ฌ๋Ÿฌ ๊ฐœ ๊ฐ€๋Šฅ โŒ extends ์—ฌ๋Ÿฌ ๊ฐœ ๋ถˆ๊ฐ€๋Šฅ

 

6. ๊ฐ์ฒด ์ง€ํ–ฅ 4๊ฐ€์ง€ ํŠน์ง• (๋ฉด์ ‘ ๋‹จ๊ณจ ์งˆ๋ฌธ)โญโญโญโญโญ

์ถ”์ƒํ™”

๋ถˆํ•„์š”ํ•œ ๊ตฌํ˜„์„ ์ˆจ๊ธฐ๊ณ  ๊ฐ์ฒด์˜ ๋ณธ์งˆ์ ์ธ ๋ถ€๋ถ„์—๋งŒ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์„ค๊ณ„์ด๋‹ค.

  • ๋ถ€๋ชจ ํด๋ž˜์Šค(CarPart)์—์„œ ๊ฒ€์ˆ˜์™€ ๊ด€๋ จ๋œ ๊ณตํ†ต ์†์„ฑ๊ณผ ๋™์ž‘์„ ์ •์˜ํ•ด์„œ ์ž์‹ ํด๋ž˜์Šค(Brake ๋˜๋Š” ํƒ€์ด์–ด)๋Š” ๊ฒ€์ˆ˜๋ฅผ ์‹ ๊ฒฝ ์“ธ ํ•„์š” ์—†์ด ์Šค์Šค๋กœ ์—ญํ• (brake.stop( ))์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ์†

๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ์„ ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ๋ฌผ๋ ค๋ฐ›๋Š” ๊ฒƒ์ด๋‹ค.

  • ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ์ •์˜๋œ ์†์„ฑ(isInspected)๊ณผ ๋ฉ”์„œ๋“œ(inspect( ))๋ฅผ ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ฌผ๋ ค๋ฐ›์•„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์บก์Šํ™”

์™ธ๋ถ€์—์„œ ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋ณดํ˜ธํ•œ๋‹ค.

  • ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ์ œ์–ด์ž(private, public)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ์™ธ๋ถ€๋กœ ๋…ธ์ถœํ•˜๊ณ  ๋ถˆํ•„์š”ํ•œ ์ •๋ณด๋Š” ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ๊ฐ์ถ”๊ณ  ๋ณดํ˜ธํ•  ์ˆ˜ ์žˆ๋Š” ์„ค๊ณ„๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹คํ˜•์„ฑ

๊ฐ™์€ ํƒ€์ž…์˜ ์ฐธ์กฐ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ˜•ํƒœ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

  • ์ธํ„ฐํŽ˜์ด์Šค์™€ ์ƒ์†์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•จ์œผ๋กœ์จ ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๋‹ค.