๐Ÿ“š TIL 8์ผ์ฐจ

temprmnยท2023๋…„ 5์›” 24์ผ
0
post-thumbnail

์˜ค๋Š˜ ์ฃผ์š” ์‚ฌํ•ญ (5/24, ์ˆ˜์š”์ผ)

  1. Git ํŠน๊ฐ•
  2. Java ๋ฌธ๋ฒ• ํ•™์Šต
    • ๊ฐ์ฒด์ง€ํ–ฅ (SOLID)
    • ์ถ”์ƒ ํด๋ž˜์Šค vs ์ธํ„ฐํŽ˜์ด์Šค

[Git ํŠน๊ฐ•] Pull request ์—ฐ์Šต

์•ž์œผ๋กœ๋„ ๋งŽ์€ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์•„์„œ ๊ธ€์„ ๋”ฐ๋กœ ์ •๋ฆฌํ•ด๋‘์—ˆ๋‹ค.
๐Ÿฑ [Git] Pull request ์—ฐ์Šต

์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๋‚ด์šฉ ๋ณต์Šต : ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„

Step 1

์•„๋ž˜์˜ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ๊ณผ ์กฐ๊ฑด์„ ์ฐธ๊ณ ํ•˜์—ฌ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด๋ณด์ž.

[ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ]

ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ 1

[์กฐ๊ฑด]
  1. ๊ณ„์‚ฐ๊ธฐ๋Š” ๋ง์…ˆ, ๋บ„์…ˆ, ๋‚˜๋ˆ—์…ˆ, ๊ณฑ์…ˆ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  2. calculate() ๋ฉ”์„œ๋“œ๋Š” String ํƒ€์ž…์˜ operator ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์—ฐ์‚ฐ์ž ๋งค๊ฐœ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.
  3. int ํƒ€์ž…์˜ firstNumber, secondNumber ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ํ”ผ์—ฐ์‚ฐ์ž ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.
  4. ์ „๋‹ฌ๋ฐ›์€ ํ”ผ์—ฐ์‚ฐ์ž, ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
๐Ÿ“ Calculate.java
public class Calculator {

	/* ๊ณ„์‚ฐ ๋ฉ”์„œ๋“œ */
    double calculate(String operator, int firstNumber, int secondNumber) {

        double result = 0;

		// operator๋ฅผ ํ†ตํ•ด ๋„˜์–ด์˜ค๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ๋น„๊ตํ•ด
        // ๊ทธ์— ๋งž๋Š” ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•œ๋‹ค.
        switch(operator) {
            case "+":
                result = firstNumber + secondNumber;
                break;
            case "-":
                result = firstNumber - secondNumber;
                break;
            case "/":
                result = firstNumber / secondNumber;
                break;
            case "*":
                result = firstNumber * secondNumber;
                break;
        }

        return result;
    }
}
๐Ÿ“ Main.java
public class Main {
    public static void main(String[] args) {

        Calculator cal = new Calculator();

		// cal.calculate(String, int, int)
        System.out.println("add: " + cal.calculate("+", 10, 10));
        System.out.println("sub: " + cal.calculate("-", 7, 10));
        System.out.println("div: " + cal.calculate("/", 5, 2));
        System.out.println("mul: " + cal.calculate("*", 7, 9));
    }
}
โ–ถ๏ธ ์‹คํ–‰ ๊ฒฐ๊ณผ
add: 20.0
sub: -3.0
div: 2.0
mul: 63.0

Step 2

[ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ]

ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ 2

[์กฐ๊ฑด]
  1. ๋ง์…ˆ, ๋บ„์…ˆ, ๋‚˜๋ˆ—์…ˆ, ๊ณฑ์…ˆ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ํด๋ž˜์Šค๋“ค์„ ๋งŒ๋“ ๋‹ค.
  2. ๋งŒ๋“  ํ›„ Calculator ํด๋ž˜์Šค์™€ ๊ด€๊ณ„๋ฅผ ๋งบ๋Š”๋‹ค. (Is-a, Has-a ๋“ฑ)
  3. ํ•„์š”ํ•˜๋‹ค๋ฉด Calculator ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.
๐Ÿ“ ~~~Operation.java : ์—ฐ์‚ฐ ํด๋ž˜์Šค

๋ฉ”์„œ๋“œ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž๋งŒ ๋ฐ”๊ฟ” ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์—ฐ์‚ฐ์ž๋งŒ ๋‹ค๋ฅด๊ณ , ์ž‘์—…์€ ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„ํšจ์œจ์ ์œผ๋กœ ๋ณด์ธ๋‹ค.

/* AddOperation.java */
public double operate(int firstNumber, int secondNumber) {

        return firstNumber + secondNumber;
}

/* SubstractOperation.java */
public double operate(int firstNumber, int secondNumber) {

        return firstNumber - secondNumber;
}

/* DivideOperation.java */
public double operate(int firstNumber, int secondNumber) {

        return firstNumber / secondNumber;
}

/* MultiplyOperation.java */
public double operate(int firstNumber, int secondNumber) {

        return firstNumber * secondNumber;
}
๐Ÿ“ Calculate.java
public class Calculator {

    /* ๋ง์…ˆ */
    public AddOperation addOperation = new AddOperation();
    /* ๋บ„์…ˆ */
    public SubstractOperation substractOperation = new SubstractOperation();
    /* ๋‚˜๋ˆ—์…ˆ */
    public DivideOperation divideOperation = new DivideOperation();
    /* ๊ณฑ์…ˆ */
    public MultiplyOperation multiplyOperation = new MultiplyOperation();

	/* ๊ณ„์‚ฐ ๋ฉ”์„œ๋“œ */
    double calculate(String operator, int firstNumber, int secondNumber) {

        double result = 0;

		// operator๋ฅผ ํ†ตํ•ด ๋„˜์–ด์˜ค๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ๋น„๊ตํ•ด
        // ๊ทธ์— ๋งž๋Š” ์—ฐ์‚ฐ ํด๋ž˜์Šค์˜ operate() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
        switch(operator) {
            case "+":
            	// result = firstNumber + secondNumber;
                result = addOperation.operate(firstNumber, secondNumber);
                break;
            case "-":
            	// result = firstNumber - secondNumber;
                result = substractOperation.operate(firstNumber, secondNumber);
                break;
            case "/":
            	// result = firstNumber / secondNumber;
                result = divideOperation.operate(firstNumber, secondNumber);
                break;
            case "*":
            	// result = firstNumber * secondNumber;
                result = multiplyOperation.operate(firstNumber, secondNumber);
                break;
        }

        return result;
    }
}
๐Ÿ“ Main.java
public class Main {
    public static void main(String[] args) {

		// Calculator ๊ฐ์ฒด ์ƒ์„ฑ
        Calculator cal = new Calculator();

		// cal.calculate(String, int, int)
        System.out.println("add: " + cal.calculate("+", 10, 10));
        System.out.println("sub: " + cal.calculate("-", 7, 10));
        System.out.println("div: " + cal.calculate("/", 5, 2));
        System.out.println("mul: " + cal.calculate("*", 7, 9));
    }
}
โ–ถ๏ธ ์‹คํ–‰ ๊ฒฐ๊ณผ
add: 20.0
sub: -3.0
div: 2.0
mul: 63.0

๐Ÿค”โ“ Step 1๊ณผ ๋น„๊ตํ•˜์—ฌ ์–ด๋– ํ•œ ์ ์ด ๊ฐœ์„  ๋˜์—ˆ๋Š”์ง€ ์Šค์Šค๋กœ ์ƒ๊ฐํ•ด ๋ด…๋‹ˆ๋‹ค.

๐Ÿ’ก hint. ํด๋ž˜์Šค์˜ ์ฑ…์ž„ (๋‹จ์ผ์ฑ…์ž„์›์น™)

[A] ๊ธฐ์กด 'Step 1'์—์„œ๋Š” Calculator ํด๋ž˜์Šค์˜ calculate() ๋ฉ”์„œ๋“œ๊ฐ€ ์—ฐ์‚ฐ์ž ๋น„๊ต์™€ ์—ฐ์‚ฐ ์ž‘์—…์„ ๋ชจ๋‘ ๋งก์•˜๋‹ค. ์ด๋Š” ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜์˜ ์ฑ…์ž„๋งŒ์„ ์ ธ์•ผํ•œ๋‹ค๋Š” ๊ฐ์ฒด์ง€ํ–ฅ ๋‹จ์ผ์ฑ…์ž„์›์น™์— ์œ„๋ฐฐ๋œ๋‹ค.

'Step 2'๋กœ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ๋Š” ๊ฐ ์—ฐ์‚ฐ ํด๋ž˜์Šค๋“ค์ด ๋ง์…ˆ, ๋บ„์…ˆ, ๋‚˜๋ˆ—์…ˆ, ๊ณฑ์…ˆ ์ค‘ ํ•œ๊ฐ€์ง€ ์”ฉ ์ˆ˜ํ–‰์„ ํ•˜๊ณ  ์žˆ๊ณ , calculate() ๋ฉ”์„œ๋“œ๋Š” ์—ฐ์‚ฐ์ž ๋น„๊ต๋งŒ์„ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋‹ค.

Step 3

[ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ]

ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ 3

[์กฐ๊ฑด]
  1. Step 2์—์„œ ๋งŒ๋“  ์—ฐ์‚ฐ ํด๋ž˜์Šค๋“ค์„ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”์ƒํ™”ํ•œ๋‹ค.
  2. ์ถ”์ƒํ™”ํ•œ ํ›„, Calculator ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.
  3. Calculator์˜ calculate() ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ํ™•์ธ.
๐Ÿ“ AbstractOperation.java : ์ถ”์ƒ ํด๋ž˜์Šค

์ˆœ๊ฐ„ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋ผ๊ณ  ์•Œ์•„๋“ค์–ด์„œ ^^;; ๋„์ค‘์— ์ดํ•ด๊ฐ€ ๊ผฌ์ด๋Š” ๋ฐ”๋žŒ์— ์ด์ƒํ•œ ๊ณณ์—์„œ ์‹œ๊ฐ„์„ ๋ฒ„๋ ธ๋‹ค. abstract์™€ interface๋ฅผ ์•ž์œผ๋กœ๋Š” ํ—ท๊ฐˆ๋ฆฌ์ง€ ๋ง์ž! ๋ฌผ๋ก  interface๋ฅผ ์‚ฌ์šฉํ–ˆ์–ด๋„ ๋ฌธ์ œ์—†์ด ๊ตฌํ˜„๋์„ ๊ฒƒ์ด๋‹ค.

public abstract class AbstractOperation { 
	
    // ๋‚ด์šฉ์€ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ๋‹ค.
	public abstract double operate(int firstNumber, int secondNumber);

}
๐Ÿ“ ~~~Operation.java : ์—ฐ์‚ฐ ํด๋ž˜์Šค

์œ„์˜ AbstractOperation ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›๋Š”๋‹ค. ๊ฐ ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ๋ฉ”์„œ๋“œ๋Š” Step 2์™€ ๋™์ผํ•˜๋‹ค.

/* AddOperation.java */
public class AddOperation extends AbstractOperation { }

/* SubstractOperation.java */
public class SubstractOperation extends AbstractOperation { }

/* DivideOperation.java */
public class DivideOperation extends AbstractOperation { }

/* MultiplyOperation.java */
public class MultiplyOperation extends AbstractOperation { }
๐Ÿ“ Calculate.java
	// ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•ด ํ•˜์œ„ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ์˜ ํ˜•๋ณ€ํ™˜์„ ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค.
    AbstractOperation operation;

	// setter()๋ฅผ ์‚ฌ์šฉํ•ด ์—ฐ์‚ฐ์ž๋ฅผ ์„ค์ •ํ•œ๋‹ค.
    public void setOperation(AbstractOperation operation) {
        this.operation = operation;
    }

	// calculate() ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ
    // ์—ฐ์‚ฐ์ž๋ฅผ ๋ฐ›์•„์˜ค๋Š” String ๋ณ€์ˆ˜๊ฐ€ ์‚ฌ๋ผ์ ธ
    // ์ด ๋ฉ”์„œ๋“œ๋Š” ์—ฐ์‚ฐ์„ ํ˜ธ์ถœํ•˜๋Š” ์ผ๋งŒ์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.
    double calculate(int firstNumber, int secondNumber) {

        return operation.operate(firstNumber, secondNumber);
    }
๐Ÿ“ Main.java
public static void main(String[] args) {

	// ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•  ์—ฐ์‚ฐ์ž (๋ง์…ˆ)
	AbstractOperation addOperation = new AddOperation();
	// Calculator ๊ฐ์ฒด ์ƒ์„ฑ
	Calculator cal = new Calculator();

	// Calculator์˜ ์—ฐ์‚ฐ์ž ์„ค์ •
	cal.setOperation(addOperation);
	// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
	System.out.println("add: " + cal.calculate(10, 10));

	// ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•  ์—ฐ์‚ฐ์ž (๊ณฑ์…ˆ)
	AbstractOperation multiflyOperation = new MultiplyOperation();
	
    // Calculator์˜ ์—ฐ์‚ฐ์ž ์„ค์ •
	cal.setOperation(multiflyOperation);
	// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
	System.out.println("mul: " + cal.calculate(7, 9));
}
โ–ถ๏ธ ์‹คํ–‰ ๊ฒฐ๊ณผ
add: 20.0
mul: 63.0

๐Ÿค”โ“ Step 2์™€ ๋น„๊ตํ•ด์„œ ์–ด๋– ํ•œ ์ ์ด ๊ฐœ์„  ๋˜์—ˆ๋Š”์ง€ ์Šค์Šค๋กœ ์ƒ๊ฐํ•ด ๋ด…๋‹ˆ๋‹ค.

๐Ÿ’ก hint. ํด๋ž˜์Šค ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„, ์˜์กด์„ฑ (์˜์กด์„ฑ์—ญ์ „์›์น™)

[A] ๊ธฐ์กด 'Step 2'์—์„œ๋Š” ๊ฐ ํด๋ž˜์Šค๊ฐ€ ๊ฐ€์ง€๋Š” ์ฑ…์ž„(์ˆ˜ํ–‰)์„ ๋ถ„๋‹ดํ•˜๋Š” ๊ฒƒ์— ์„ฑ๊ณตํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ Step 2๋ฅผ ๋‹ค์‹œ ๋ณด๋ฉด, Calculate ํด๋ž˜์Šค์™€ ์—ฐ์‚ฐ ํด๋ž˜์Šค ๊ฐ„ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

'Step 3'์—์„œ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์ธ AbstractOperation์„ ๋งŒ๋“ค์–ด, ๋‹ค๋ฅธ ์—ฐ์‚ฐ ํด๋ž˜์Šค๋“ค์ด ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ Calculate ํด๋ž˜์Šค์™€ AbstractOperation ํด๋ž˜์Šค๊ฐ€ ํฌํ•จ ๊ด€๊ณ„๋ฅผ ๋งบ์œผ๋ฉด ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„์ง„๋‹ค. (์ถ”์ƒ ํด๋ž˜์Šค๋Š” ํ•˜์œ„ ํด๋ž˜์Šค๋กœ์˜ ํ˜•๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค)

๊ทธ๋ฆฌ๊ณ  Caculate ํด๋ž˜์Šค๋Š” ๊ตฌ์ฒดํ™”๋œ(=๊ตฌํ˜„๋ถ€) ์—ฐ์‚ฐ ํด๋ž˜์Šค์— ๊ธฐ๋Šฅ์„ ์˜์กดํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ์ด๋Š” ๊ฐ์ฒด์ง€ํ–ฅ ์›์น™ ์ค‘, '๊ตฌ์ฒด์ ์ธ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ถ”์ƒํ™”๋œ ๊ฒƒ์— ์˜์กดํ•˜๋ผ'๋Š” ์˜์กด์„ฑ ์—ญ์ „์˜ ์›์น™์˜ ์œ„๋ฐฐ๋œ๋‹ค.

์˜์กด์„ฑ ์—ญ์ „์˜ ์›์น™(DIP)๋ฅผ ์ง€ํ‚ค๋Š” ์ด์œ ๋Š”, ์ˆ˜์ •์— ์šฉ์ดํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ๊ตฌํ˜„๋ถ€ ํด๋ž˜์Šค(๊ตฌ์ฒดํ™”๋œ ํด๋ž˜์Šค)๋Š” ์ฝ”๋“œ ๋ณ€ํ™”์— ์˜ํ–ฅ์„ ํฌ๊ฒŒ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ํด๋ž˜์Šค์— ์˜์กดํ•˜๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋„ ์˜ํ–ฅ์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ๊ตฌํ˜„๋ถ€์˜ ์ƒ์œ„์— ์žˆ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ์˜ํ–ฅ์„ ๋ฐ›์„ ์ผ์ด ์ ๊ธฐ ๋•Œ๋ฌธ์—, ์ถ”์ƒ ํด๋ž˜์Šค์— ์˜์กดํ•˜๋Š” ๊ฒƒ์ด ๊ตฌ์กฐ์ƒ ๋ฐ”๋žŒ์งํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๊ธฐ์กด ์—ฐ์‚ฐ ํด๋ž˜์Šค๋“ค ๋Œ€์‹  ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ํฌํ•จํ•˜๊ณ , ์—ฐ์‚ฐ์ž๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹Œ setter (setOperation() ๋ฉ”์„œ๋“œ)๋ฅผ ํ†ตํ•ด ์„ค์ •ํ•˜๋„๋ก ํ–ˆ๋‹ค. 'Step 3'์—์„œ๋Š” Main์—์„œ ์‚ฌ์šฉํ•  ์—ฐ์‚ฐ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ณ , Calculator ํด๋ž˜์Šค์˜ setter๋ฅผ ํ†ตํ•ด ์—ฐ์‚ฐ์ž๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ๋Š” ์—ฐ์‚ฐ์ด ํ•„์š”ํ•œ firstNumber์™€ secondNumber๋งŒ Calculator.operate() ๋ฉ”์„œ๋“œ๋กœ ์ „๋‹ฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

ํ›„์ผ๋‹ด

Step 1โ†’2๋กœ๋ถ€ํ„ฐ์˜ ๋ณ€ํ™”์™€, Step 2โ†’3๋กœ๋ถ€ํ„ฐ์˜ ๋ณ€ํ™”๋ฅผ SOLID์™€ ์—ฎ์–ด์„œ ์„ค๋ช…ํ•˜๋ ค๋ฉด ๋”ฐ๋กœ ๊ฐ์ฒด์ง€ํ–ฅ์›์น™์— ๋Œ€ํ•œ ํ•™์Šต์ด ํ•„์š”ํ•ด์„œ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

ํ•˜์ง€๋งŒ ๋ฏธ๋ฃจ๋ฉด ๋‹ค์Œ์— ๋‚ด๊ฐ€ ํ•ด๋‘๋ฆฌ๋ž€ ๋ณด์žฅ๋„ ์—†์„ ๊ฒƒ ๊ฐ™๊ณ ... ๊ผญ ํ•˜๊ณ  ์ž ๋“ค๊ณ  ์‹ถ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๊ณ  ๋‚˜๋‹ˆ ๊ธฐ๋ถ„์€ ์ข‹๋‹ค! (^โ”€^)

์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค

์ถ”๊ฐ€๋กœ 'Step 3'์˜ ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ -> ์ธํ„ฐํŽ˜์ด์Šค ํ˜•ํƒœ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ ์–ด๋ ต์ง€ ์•Š๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ๋งŒ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋!

public interface AbstractOperation {

    public double operate(int firstNumber, int secondNumber);
}
// ๋‹ค๋ฅธ ์—ฐ์‚ฐ ํด๋ž˜์Šค๋“ค๋„ ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
public class AddOperation implements AbstractOperation {  /* ๋ฉ”์„œ๋“œ๋Š” ๊ทธ๋Œ€๋กœ */ }

๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ๋ณด๋ฉด ์ฐจ์ด๊ฐ€ ์—†๋Š”๋ฐ, ๋‘˜์€ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ•ด์„œ ์“ฐ๋Š” ๊ฑธ๊นŒ? ๋˜, ์™œ ์ด ๊ณ„์‚ฐ๊ธฐ ๋ฌธ์ œ์—์„œ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ–ˆ์„๊นŒ? ๊ฐ™์€ ์˜๋ฌธ์ ์ด ์ƒ๊ธด๋‹ค...

์ถœ์ฒ˜: ์ž๋ฐ”์˜ ์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค

Q. ๋‘˜์€ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ•ด์„œ ์“ฐ๋Š” ๊ฑธ๊นŒ?

์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์กด์žฌ ๋ชฉ์ ์ด ๋‹ค๋ฅด๋‹ค.

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

์ฆ‰, ์ƒ์†์„ ๋ฐ›์•„์„œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ  ํ™•์žฅ ์‹œํ‚ค๋ ค๋ฉด ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ, ๋ช…์‹œํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋ผ๋ฉด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

Q. ์ด ๊ณ„์‚ฐ๊ธฐ ๋ฌธ์ œ์—์„œ๋Š” ์™œ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ–ˆ์„๊นŒ?

๋ณด๋Š” ์‹œ๊ฐ์— ๋”ฐ๋ผ ๊ตฌํ˜„ ์š”๊ตฌ์‚ฌํ•ญ์— ์ฐจ์ด๊ฐ€ ์ƒ๊ธด ๊ฒƒ ๊ฐ™๋‹ค. (์ถ”์ธก) ์œ— ๋ฌธ๋‹จ์— ๋”ฐ๋ฅด๋ฉด ์•„๋งˆ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ์— ์ข€ ๋” ์ง‘์ค‘ํ•œ ๊ฒƒ์€ ์•„๋‹๊นŒ? ๋‚˜๋Š” [์กฐ๊ฑด]์„ ์ฒ˜์Œ ์ฝ์—ˆ์„ ๋•Œ, '๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ๋‹ค'์— ์ดˆ์ ์„ ๋‘๊ณ  ์ฝ์–ด์„œ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์˜ค๋…ํ–ˆ์—ˆ๋‹ค.

Java๋Š” ๋‹ค์ค‘ ์ƒ์†์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ƒ์œ„ ํด๋ž˜์Šค๊ฐ€ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š”, ์‹คํ–‰ ์‹œ์— ์‹ค์ œ๋กœ ์–ด๋–ค ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋ ์ง€ ๋ชจํ˜ธ์„ฑ์ด ์ƒ๊ฒจ๋ฒ„๋ฆฐ๋‹ค. Java๋Š” ์ด ๋ชจํ˜ธ์„ฑ์„ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์ค‘ ์ƒ์†์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ฐ›์•„์™€์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. (์ด๋Š” ์ƒ์†์ด ์•„๋‹ˆ๋‹ค!!)

class Car implements vehicle, engine {}
profile
`ISFJ` T 49% F 51% /

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 5์›” 25์ผ

์ •๋ฆฌ๋ฅผ ์—„์ฒญ ์™„๋ฒฝํ•˜๊ฒŒ ํ•ด์ฃผ์…จ๋„ค์š”..
์ฝ๋Š” ์ž…์žฅ์—์„œ๋„ ๋„ˆ๋ฌด ์ˆ ์ˆ  ์ž˜ ์ฝํžˆ๊ณ  ์–ด๋–ค ๊ฑธ ๋ฐฐ์šฐ์…จ๋Š”์ง€ ํ•œ๋ฒˆ์— ์•Œ์ˆ˜์žˆ์–ด์š” !
์•ž์œผ๋กœ๋„ ํ™”์ดํŒ…๋น ์…์ž…๋‹ˆ๋‹ค !!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ