๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ ์‘์šฉํ•˜๊ธฐ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 20.
๋ฐ˜์‘ํ˜•

โ–ถ ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ ์‘์šฉํ•˜๊ธฐ

โ€‹

โ€‹

์˜ˆ์ œ ์‹œ๋‚˜๋ฆฌ์˜ค) Player๊ฐ€ ์žˆ๊ณ  ์ด Player๊ฐ€ ๊ฒŒ์ž„์„ ํ•œ๋‹ค. ๊ฒŒ์ž„์—์„œ Player๊ฐ€ ๊ฐ€์ง€๋Š” ๋ ˆ๋ฒจ์— ๋”ฐ๋ผ ํ•  ์ˆ˜ ์žˆ๋Š” ์„ธ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค. run( ), jump( ), turn( ) ์ด๋‹ค.

์ดˆ๋ณด์ž ๋ ˆ๋ฒจ : ์ฒœ์ฒœํžˆ ๋‹ฌ๋ฆด(run) ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ค‘๊ธ‰์ž ๋ ˆ๋ฒจ : ๋น ๋ฅด๊ฒŒ ๋‹ฌ๋ฆฌ๊ณ (run) ์ ํ”„ํ• (jump) ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ณ ๊ธ‰์ž ๋ ˆ๋ฒจ : ์—„์ฒญ ๋น ๋ฅด๊ฒŒ ๋‹ฌ๋ฆฌ๊ณ (run) ์ ํ”„ํ•˜๊ณ (jump) ํ„ดํ• (turn) ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  ๋ ˆ๋ฒจ์—์„œ Player๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•„์‚ด๊ธฐ์ธ go(int count) ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. go( ) ๋ฉ”์„œ๋“œ๋Š” ํ•œ ๋ฒˆ runํ•˜๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ count๋งŒํผ jumpํ•˜๊ณ , ํ•œ ๋ฒˆ turnํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋ ˆ๋ฒจ์—์„œ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ธฐ๋Šฅ์„ ์š”์ฒญํ•˜๋ฉด ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๊ฒฐ๊ณผ๊ฐ’์ด ์ด๋ ‡๊ฒŒ ๋‚˜์˜ค๊ฒŒ...

 

โ€‹

โ˜…์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ฒœ์ฒœํžˆ ์ƒ๊ฐํ•ด ๋ณด๊ณ  ์†์œผ๋กœ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์„ ๊ฐ„๋žตํ•˜๊ฒŒ ๊ทธ๋ ค๋ณด๋Š” ๊ฒƒ์ด ๊ฐ์ฒด์ง€ํ–ฅ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์ข‹์€ ์Šต๊ด€์ด๋‹ค.

 

<๊ณผ์ •>

package gamelevel;

public class Player {

	private PlayerLevel level;  // Player๊ฐ€ ๊ฐ€์ง€๋Š” level ๋ณ€์ˆ˜ ์„ ์–ธ

	public Player() {  // ๋””ํดํŠธ ์ƒ์„ฑ์ž. ์ฒ˜์Œ ์ƒ์„ฑ๋˜๋ฉด BeginnerLevel๋กœ ์‹œ์ž‘ํ•˜๋ฉฐ ๋ ˆ๋ฒจ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ
		level = new BeginnerLevel();
		level.showLevelMessage();
	}

	public PlayerLevel getLevel() {
		return level;
	}

	public void upgradeLevel(PlayerLevel level) {  // ๋ ˆ๋ฒจ ๋ณ€๊ฒฝ ๋ฉ”์„œ๋“œ. ํ˜„์žฌ ์ž์‹ ์˜ level์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ level๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ๋ ˆ๋ฒจ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ
		this.level = level;
		level.showLevelMessage();
	}

	public void play(int count) {
		level.go(count);  // PlayerLevel์˜ ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ go() ํ˜ธ์ถœ
	}
    
}

 

package gamelevel;

public abstract class PlayerLevel {

	public abstract void run();

	public abstract void jump();

	public abstract void turn();

	public abstract void showLevelMessage();

	final public void go(int count) {  // ์žฌ์ •์˜๋˜๋ฉด ์•ˆ๋˜๋ฏ€๋กœ final๋กœ ์„ ์–ธ
		run();
		for (int i = 0; i < count; i++) {
			jump();
		}
		turn();
	}
    
}

 

package gamelevel;

public class BeginnerLevel extends PlayerLevel {

	@Override
	public void run() {
		System.out.println("์ฒœ์ฒœํžˆ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");
	}

	@Override
	public void jump() {
		System.out.println("Jump ํ•  ์ค„ ๋ชจ๋ฅด์ง€๋กฑ.");
	}

	@Override
	public void turn() {
		System.out.println("Turn ํ•  ์ค„ ๋ชจ๋ฅด์ง€๋กฑ.");
	}

	@Override
	public void showLevelMessage() {
		System.out.println("***** ์ดˆ๋ณด์ž ๋ ˆ๋ฒจ ์ž…๋‹ˆ๋‹ค. *****");
	}

}

 

package gamelevel;

public class AdvancedLevel extends PlayerLevel {
	
    @Override
	public void run() {
		System.out.println("๋นจ๋ฆฌ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");
	}

	@Override
	public void jump() {
		System.out.println("๋†’์ด jump ํ•ฉ๋‹ˆ๋‹ค.");
	}

	@Override
	public void turn() {
		System.out.println("Turn ํ•  ์ค„ ๋ชจ๋ฅด์ง€๋กฑ.");
	}

	@Override
	public void showLevelMessage() {
		System.out.println("***** ์ค‘๊ธ‰์ž ๋ ˆ๋ฒจ ์ž…๋‹ˆ๋‹ค. *****");
	}
    
}

 

package gamelevel;

public class SuperLevel extends PlayerLevel {

	@Override
	public void run() {
		System.out.println("์—„์ฒญ ๋นจ๋ฆฌ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");
	}

	@Override
	public void jump() {
		System.out.println("์•„์ฃผ ๋†’์ด jump ํ•ฉ๋‹ˆ๋‹ค.");
	}

	@Override
	public void turn() {
		System.out.println("ํ•œ ๋ฐ”ํ€ด ๋•๋‹ˆ๋‹ค.");
	}

	@Override
	public void showLevelMessage() {
		System.out.println("***** ๊ณ ๊ธ‰์ž ๋ ˆ๋ฒจ ์ž…๋‹ˆ๋‹ค. *****");
	}
    
}

 

package gamelevel;

public class MainBoard {
	public static void main(String[] args) {

		Player player = new Player();  // ์ฒ˜์Œ ์ƒ์„ฑํ•˜๋ฉด BiginnerLevel
		player.play(1);

		AdvancedLevel aLevel = new AdvancedLevel();
		player.upgradeLevel(aLevel);
		player.play(2);

		SuperLevel sLevel = new SuperLevel();
		player.upgradeLevel(sLevel);
		player.play(3);

	}
}

-์ถœ๋ ฅ ํด๋ž˜์Šค์—์„œ ์™œ player.go(int count); ๋กœ ์ถœ๋ ฅ์ด ์•ˆ๋˜๊ณ  player.play(int count); ๋กœ ์ถœ๋ ฅ๋˜๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค..

๋ฐ˜์‘ํ˜•

'Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] Car ์ถ”์ƒํด๋ž˜์Šค + ์‘์šฉ  (0) 2021.02.20
[Java] final ์˜ˆ์•ฝ์–ด  (0) 2021.02.20
[Java] ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ  (0) 2021.02.20
[Java] ์ถ”์ƒ ํด๋ž˜์Šค  (0) 2021.02.20
[Java] ์‹ค์Šต  (0) 2021.02.20

๋Œ“๊ธ€