λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Java·ο»ΏServlet·ο»ΏJSP

[JAVA λ””μžμΈ νŒ¨ν„΄] ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ νŒ¨ν„΄

by Leica 2020. 6. 26.
λ°˜μ‘ν˜•

[JAVA λ””μžμΈ νŒ¨ν„΄] ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ νŒ¨ν„΄

- ν”„λ ˆμž„μ›Œν¬μ—μ„œ ν”ν•˜κ²Œ μ‚¬μš©λ˜λŠ” 섀계 기법 쀑 ν•˜λ‚˜
- abstract, final ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ κ΅¬ν˜„
- final λ©”μ†Œλ“œμ™€ abstract λ©”μ†Œλ“œλ‘œ 둜직의 νλ¦„λ§Œμ„ μ •μ˜ν•˜κ³  ꡬ체적인 κ΅¬ν˜„μ€ ν•˜μœ„ ν΄λž˜μŠ€μ— μœ„μž„

ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ νŒ¨ν„΄μ€ ν”„λ ˆμž„μ›Œν¬μ—μ„œ ν”ν•˜κ²Œ μ‚¬μš©λ˜λŠ” 섀계 기법 쀑 ν•˜λ‚˜μž…λ‹ˆλ‹€.

μš°μ„  ν…œν”Œλ¦Ώ λ©”μ†Œλ“œλž€ ν”„λ‘œκ·Έλž¨ μ „μ²΄μ˜ 흐름(μ‹œλ‚˜λ¦¬μ˜€)λ₯Ό μ •μ˜ν•œ λ©”μ†Œλ“œμž…λ‹ˆλ‹€. final ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ overrideν•  수 μ—†κ²Œ ν•˜λŠ” 것이 νŠΉμ§•μž…λ‹ˆλ‹€.

 

μ΄λŸ¬ν•œ ν…œν”Œλ¦Ώ λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•œ ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ νŒ¨ν„΄μ€ abstract ν΄λž˜μŠ€μ—μ„œ abstract λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ 전체 흐름을 μ •μ˜ν•˜κ³  ꡬ체적인 각 λ©”μ†Œλ“œμ˜ κ΅¬ν˜„μ€ ν•˜μœ„ ν΄λž˜μŠ€μ— μœ„μž„ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

ν•˜μœ„ν΄λž˜μŠ€ λ§ˆλ‹€ μ„ΈλΆ€ κ΅¬ν˜„ λ‚΄μš©μ΄ 달라져도 둜직의 흐름은 ν…œν”Œλ¦Ώ λ©”μ†Œλ“œμ— μ •μ˜λœ νλ¦„λŒ€λ‘œ μˆ˜ν–‰λ©λ‹ˆλ‹€.

 

ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ νŒ¨ν„΄ 예제1

κ°€μž₯ κ°„λ‹¨ν•œ 예둜 λ‹€μŒκ³Ό 같은 예λ₯Ό λ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

 

public abstract class Car {

    protected abstract void startCar();     // μ‹œλ™ κ±ΈκΈ°
    protected abstract void drive();        // μš΄ν–‰
    protected abstract void stop();         // 멈좀
    protected abstract void turnOff();      // μ‹œλ™ 끄기

    // ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ
    public final void run() {
        startCar();
        drive();
        stop();
        turnOff();
    }
}

abstract 클래슀 CarλŠ” final둜 μ„ μ–Έλœ run() λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ startCar() - drive() - stop() - turnOff()의 흐름을 μ •μ˜ν•©λ‹ˆλ‹€.

이 run() λ©”μ†Œλ“œκ°€ ν…œν”Œλ¦Ώ λ©”μ†Œλ“œμž…λ‹ˆλ‹€.

abstract λ©”μ†Œλ“œμΈ startCar(), drive(), stop(), turnOff()λŠ” Carλ₯Ό μƒμ†λ°›λŠ” ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ κ΅¬ν˜„ν•˜λ©°, μ„ΈλΆ€ κ΅¬ν˜„ λ‚΄μš©μ΄ 달라도 run()μ—μ„œ μ •μ˜ν•œ 흐름은 λ™μΌν•˜κ²Œ κ°€μ Έκ°‘λ‹ˆλ‹€.

 

ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ νŒ¨ν„΄ 예제2

- κ²Œμž„μ—μ„œ Playerκ°€ κ°€μ§€λŠ” λ ˆλ²¨μ— 따라 walk(), run(), jump() μ„Έ 가지 κΈ°λŠ₯을 ν•  수 μžˆλ‹€.
- Beginner 레벨: 천천히 κ±·λŠ” walk()만 κ°€λŠ₯, run(), jump() λΆˆκ°€
- Intermediate 레벨: 보톡 μ†λ„λ‘œ κ±·λŠ” walk(), 천천히 λ‹¬λ¦¬λŠ” run() κ°€λŠ₯, jump() λΆˆκ°€
- advanced 레벨: 빨리 κ±·λŠ” walk(), 보톡 μ†λ„λ‘œ λ‹¬λ¦¬λŠ” run(), 보톡 λ†’μ΄λ‘œ μ ν”„ν•˜λŠ” jum() κ°€λŠ₯
- Player의 play()λ₯Ό 톡해 μœ„ μ„Έ 가지 κΈ°λŠ₯을 μˆ˜ν–‰ν•˜κ³  횟수λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ λ°›μ•„ 횟수 만큼 jump() ν•œλ‹€.

 

public class Player {

    private PlayerLevel level;

    public Player() {
        
        level = new BeginnerPlayerLevel();
        System.out.println("ν”Œλ ˆμ΄μ–΄κ°€ μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
        level.showLevelInfo();
    }

    public void play(int count) {
        level.go(count);
    }

    public void levelUp(PlayerLevel level) {

        this.level = level;
        System.out.println("Level Up!!");
        this.level.showLevelInfo();
    }
}

public abstract class PlayerLevel {

    protected abstract void walk();
    protected abstract void run();
    protected abstract void jump();
    protected abstract void showLevelInfo();

    public final void go(int count) {// ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ

        walk();
        run();
        for (int i=0; i<count; i++) {
            jump();
        }
    }
}

public class BeginnerPlayerLevel extends PlayerLevel {

    @Override
    protected void walk() {
        System.out.println("천천히 κ±·μŠ΅λ‹ˆλ‹€.");
    }

    @Override
    protected void run() {
        System.out.println("BeginnerPlayerλŠ” 달릴 수 μ—†μŠ΅λ‹ˆλ‹€.");
    }

    @Override
    protected void jump() {
        System.out.println("BeginnerPlayerλŠ” 점프할 수 μ—†μŠ΅λ‹ˆλ‹€.");
    }

    @Override
    protected void showLevelInfo() {
        System.out.println("===== Beginner λ ˆλ²¨μž…λ‹ˆλ‹€. =====");
    }
}

public class IntermediatePlayerLevel extends PlayerLevel {

    @Override
    protected void walk() {
        System.out.println("보톡 μ†λ„λ‘œ κ±·μŠ΅λ‹ˆλ‹€.");
    }

    @Override
    protected void run() {
        System.out.println("천천히 λ‹¬λ¦½λ‹ˆλ‹€.");
    }

    @Override
    protected void jump() {
        System.out.println("IntermediatePlayerλŠ” 점프할 수 μ—†μŠ΅λ‹ˆλ‹€.");
    }

    @Override
    protected void showLevelInfo() {
        System.out.println("===== Intermediate λ ˆλ²¨μž…λ‹ˆλ‹€. =====");
    }
}

public class AdvancedPlayerLevel extends PlayerLevel {

    @Override
    protected void walk() {
        System.out.println("λΉ λ₯΄κ²Œ κ±·μŠ΅λ‹ˆλ‹€.");
    }

    @Override
    protected void run() {
        System.out.println("보톡 μ†λ„λ‘œ λ‹¬λ¦½λ‹ˆλ‹€.");
    }

    @Override
    protected void jump() {
        System.out.println("보톡 λ†’μ΄λ‘œ μ ν”„ν•©λ‹ˆλ‹€.");
    }

    @Override
    protected void showLevelInfo() {
        System.out.println("===== Advanced λ ˆλ²¨μž…λ‹ˆλ‹€. =====");
    }
}

public class PlayerLevelTest {

    public static void main(String[] args) {

        Player player = new Player();
        player.play(1);

        player.levelUp(new IntermediatePlayerLevel());
        player.play(2);

        player.levelUp(new AdvancedPlayerLevel());
        player.play(3);
    }
}

 

πŸ–₯ μ‹€ν–‰ κ²°κ³Ό

ν”Œλ ˆμ΄μ–΄κ°€ μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
===== Beginner λ ˆλ²¨μž…λ‹ˆλ‹€. =====
천천히 κ±·μŠ΅λ‹ˆλ‹€.
BeginnerPlayerλŠ” 달릴 수 μ—†μŠ΅λ‹ˆλ‹€.
BeginnerPlayerλŠ” 점프할 수 μ—†μŠ΅λ‹ˆλ‹€.
Level Up!!
===== Intermediate λ ˆλ²¨μž…λ‹ˆλ‹€. =====
보톡 μ†λ„λ‘œ κ±·μŠ΅λ‹ˆλ‹€.
천천히 λ‹¬λ¦½λ‹ˆλ‹€.
IntermediatePlayerλŠ” 점프할 수 μ—†μŠ΅λ‹ˆλ‹€.
IntermediatePlayerλŠ” 점프할 수 μ—†μŠ΅λ‹ˆλ‹€.
Level Up!!
===== Advanced λ ˆλ²¨μž…λ‹ˆλ‹€. =====
λΉ λ₯΄κ²Œ κ±·μŠ΅λ‹ˆλ‹€.
보톡 μ†λ„λ‘œ λ‹¬λ¦½λ‹ˆλ‹€.
보톡 λ†’μ΄λ‘œ μ ν”„ν•©λ‹ˆλ‹€.
보톡 λ†’μ΄λ‘œ μ ν”„ν•©λ‹ˆλ‹€.
보톡 λ†’μ΄λ‘œ μ ν”„ν•©λ‹ˆλ‹€.

Process finished with exit code 0

 

참고둜 μžλ°”μ˜ final ν‚€μ›Œλ“œλŠ” λ³€μˆ˜, λ©”μ†Œλ“œ, ν΄λž˜μŠ€μ— 뢙일 수 있으며 λ³€μˆ˜μ— 뢙이면 졜초 ν• λ‹Ή 이후 값을 λ³€κ²½ν•  수 μ—†λŠ” μƒμˆ˜λ‘œ μ‚¬μš©λ©λ‹ˆλ‹€. λ©”μ†Œλ“œμ— 뢙이면 ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ overrideν•  수 μ—†κ²Œ ν•©λ‹ˆλ‹€. ν΄λž˜μŠ€μ— 뢙이면 상속될 수 μ—†λŠ” ν΄λž˜μŠ€κ°€ λ©λ‹ˆλ‹€. μžλ°” API 쀑 String ν΄λž˜μŠ€λ„ final 클래슀이기 λ•Œλ¬Έμ— 상속받아 μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

λ°˜μ‘ν˜•

λŒ“κΈ€