Back-end/Spring

[Spring] ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๊ธฐ (feat. ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ๊ด€์ )

๐Ÿ’ก ๋ฆฌํŒฉํ† ๋ง์ด๋ž€?

 

๋ฆฌํŒฉํ„ฐ๋ง(refactoring)์€ ์†Œํ”„ํŠธ์›จ์–ด ๊ณตํ•™์—์„œ '๊ฒฐ๊ณผ์˜ ๋ณ€๊ฒฝ ์—†์ด ์ฝ”๋“œ์˜ ๊ตฌ์กฐ๋ฅผ ์žฌ์กฐ์ •ํ•จ'์„ ๋œปํ•œ๋‹ค. ์ฃผ๋กœ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  ์œ ์ง€๋ณด์ˆ˜๋ฅผ ํŽธํ•˜๊ฒŒ ํ•œ๋‹ค. ๋ฒ„๊ทธ๋ฅผ ์—†์• ๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋Š” ํ–‰์œ„๋Š” ์•„๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋Š” ์™ธ๋ถ€ ํ™”๋ฉด์€ ๊ทธ๋Œ€๋กœ ๋‘๋ฉด์„œ ๋‚ด๋ถ€ ๋…ผ๋ฆฌ๋‚˜ ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊พธ๊ณ  ๊ฐœ์„ ํ•˜๋Š” ์œ ์ง€๋ณด์ˆ˜ ํ–‰์œ„์ด๋‹ค.

 

 

โœ๏ธ ๋ฆฌํŒฉํ† ๋ง ์ „ ์ฝ”๋“œ ์ ๊ฒ€ ํ•ญ๋ชฉ

  • ์ฝ”๋“œ์— ์ค‘๋ณต๋œ ๋ถ€๋ถ„์€ ์—†๋Š”๊ฐ€?
  • ์ฝ”๋“œ๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š” ๊ฒƒ์ธ์ง€ ์ดํ•ดํ•˜๊ธฐ ๋ถˆํŽธํ•˜์ง€ ์•Š์€๊ฐ€?
  • ์ฝ”๋“œ๊ฐ€ ์ž์‹ ์ด ์žˆ์–ด์•ผ ํ•  ์ž๋ฆฌ์— ์žˆ๋Š”๊ฐ€?
  • ์•ž์œผ๋กœ ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚œ๋‹ค๋ฉด ์–ด๋–ค ๊ฒƒ์ด ์žˆ์„ ์ˆ˜ ์žˆ๊ณ , ๊ทธ ๋ณ€ํ™”์— ์‰ฝ๊ฒŒ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š”๊ฐ€?

โœ๏ธ UserService๋ฅผ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด์ž

 

๊ฐ User๋งˆ๋‹ค ๊ฐ–๊ณ  ์žˆ๋Š” level์„ login, recommend๋ฅผ ๊ฒ€์‚ฌํ•ด ๊ธฐ์ค€์— ๋งŒ์กฑํ•˜๋ฉด

Level์„ ์—…๊ทธ๋ ˆ์ด๋“œ ์‹œ์ผœ์ฃผ๋Š” ๋ ˆ๋ฒจ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด๋ดค๋‹ค.

 

๋ ˆ๋ฒจ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์˜ ์‚ฌ์šฉ์ž ๊ด€๋ฆฌ ๋กœ์ง์€ UserDaoJdbc์— ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š์•„

UserService๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์–ด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋‹ด๋‹น์‹œ์ผœ์ฃผ์—ˆ๋‹ค.

 

UserService.class

public class UserService {
	UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void upgradeLevels() {
		List<User> users = userDao.getAll();
		for(User user : users) {
			Boolean changed = null;
			if(user.getLevel() == Level.BASIC && user.getLogin() >= 50) {
				user.setLevel(Level.SILVER);
				changed = true;
			}
			else if(user.getLevel() == Level.SILVER && user.getRecommend() >= 30) {
				user.setLevel(Level.GOLD);
				changed = true;
			}
			else if(user.getLevel() == Level.GOLD) { changed = false; }
			else { changed = false; }

			if(changed) { userDao.update(user); }
		}
	}
    
    public void add(User user) {
		if(user.getLevel() == null) user.setLevel(Level.BASIC);
		userDao.add(user);
	}
}

 

upgradeLevel()๋ฅผ ๋ณด๋ฉด ๋งŽ์€ if/elseif/else ๋ธ”๋ก๋“ค์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋งŒ์•ฝ Level์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๊ณ , ์ด์กฑํ•ด์•ผํ•˜๋Š” ์กฐ๊ฑด๋“ค์ด ๋งŽ์•„์ง€๋ฉด ์ด ๋ธ”๋ก์˜ ์ˆ˜๋Š” ๋”์šฑ ๋งŽ์•„์ ธ,

๊ฐˆ์ˆ˜๋ก ์ดํ•ดํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ํž˜๋“ค์–ด์งˆ ๋ฟ ์•„๋‹ˆ๋ผ, ์ง€์ €๋ถ„ํ•ด์ง€๋Š” ์ฝ”๋“œ ํƒ“์— ๋ฒ„๊ทธ๊ฐ€ ์ˆจ์–ด๋“ค์–ด ๊ฐˆ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์งˆ ๊ฒƒ์ด๋‹ค.

 

๋”ฐ๋ผ์„œ if/elseif/else๋กœ ์ด๋ค„์ง„ ๋ณต์žกํ•œ ์กฐ๊ฑด์‹๋“ค์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฆฌํŒฉํ† ๋งํ•ด๋ณด์ž

 

canUpgradeLevel(), upgradeLevel()์„ ๋งŒ๋“ค์–ด์คŒ์œผ๋กœ์„œ ์กฐ๊ฑด๋ฌธ์„ ์ตœ๋Œ€ํ•œ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ”— canUpgradeLevel : user๊ฐ€ ๊ฐ€์ง„ ์ •๋ณด๋กœ ๋ ˆ๋ฒจ์ด ์—…๊ทธ๋ ˆ์ด๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š”์ง€ ํŒ๋ณ„ํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ

private boolean canUpgradeLevel(User user) {
	Level currentLevel = user.getLevel();
	switch(currentLevel) {
	case BASIC : return (user.getLogin() >= MIN_LOGCOUNT_FOR_SILVER);
	case SILVER : return (user.getRecommend() >= MIN_RECCOMEND_FOR_GOLD);
	case GOLD : return false;
	default : throw new IllegalArgumentException("Unknown Level : " + currentLevel);
	}
}

 

๐Ÿ”— upgradeLevel : user๊ฐ€ ๊ฐ€์ง„ ๋ ˆ๋ฒจ์„ ์—…๊ทธ๋ ˆ์ด๋“œ ํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ

private void upgradeLevel(User user) {
		if(user.getLevel() == Level.BASIC) user.setLevel(Level.SILVER);
		else if (user.getLevel() == Level.SILVER) user.setLevel(Level.GOLD);
		userDao.update(user);
	}

์ด๋ ‡๊ฒŒ ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋ณต์žกํ•œ if/else if/else๋ฌธ์„ ๋‹จ์ˆœํ™”์‹œ์ผœ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ upgradeLevel ๋ฉ”์†Œ๋“œ์—๋„ ๋ฌธ์ œ๊ฐ€ ๋ณด์ผ ๊ฒƒ์ด๋‹ค.

Level ๋งˆ๋‹ค ์กฐ๊ฑด์‹์„ ๋งŒ๋“ค์–ด์ค€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ , ๋ ˆ๋ฒจ์ด ๋งŽ์•„์ง€๋ฉด ์œ„์™€ ๊ฐ™์ด ๋˜ ์กฐ๊ฑด์‹์ด ์ˆ˜๋„ ์—†์ด ๋งŽ์•„์งˆ ๊ฒƒ์ด๋‹ค.

 

 

๐Ÿ’ก ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ๊ด€์ ์„ ํ†ตํ•ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋ณด์ž

 

์šฐ๋ฆฌ๋Š” JAVA๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค. 

 

๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์ฝ”๋“œ๋ž€ ๋‹ค๋ฅธ ์˜ค๋ธŒ์ ํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๋‹ค๋ฅธ ์˜ค๋ธŒ์ ํŠธ์—๊ฒŒ

์ž‘์—…์„ ํ•ด๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ฆ‰, ์˜ค๋ธŒ์ ํŠธ์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ž‘์—…์„ ์š”์ฒญํ•˜๋Š” ๊ฒƒ์ด ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ ์›๋ฆฌ์ด๋‹ค.

 

๊ฐ์ฒด์ง€ํ–ฅ์  ์ฝ”๋“œ ๊ด€์ ์„ ์ƒ๊ฐํ•˜๋ฉฐ upgradeLevel๋ฅผ ๋ฆฌํŒฉํ† ๋ง

 

์šฐ์„  if/else ๋ฌธ์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ ๋œ ํ›„์˜ Level์„ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์„ Level Enum์—๊ฒŒ ์ž‘์—…์„ ์š”๊ตฌํ•˜๋„๋ก ํ•˜์ž.

๊ธฐ์กด์˜ Level Enum์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ณ ์ณ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

public enum Level {
	GOLD(3, null),  SILVER(2, GOLD), BASIC(1, SILVER);
	
	private final int value;
	private final Level next;
	
	Level(int value, Level next){
		this.value = value;
		this.next = next;
	}
	
	public int intValue() {
		return value;
	}
	
	public Level nextLevel() {
		return this.next;
	}
	
	public static Level valueOf(int value) {
		switch(value) {
		case 1 : return BASIC;
		case 2 : return SILVER;
		case 3 : return GOLD;
		default : throw new AssertionError("Unknown value : " + value);
		}
	}
}

 

์œ„์™€ ๊ฐ™์ด ๋‹ค์Œ๋ ˆ๋ฒจ์„ ํŒ๋ณ„ํ•ด์ฃผ๋Š” ์ž‘์—…์€ UserService์—์„œ์˜ ์ž‘์—…์ด ์•„๋‹Œ, Level ๊ฐ์ฒด์—๊ฒŒ ์ž‘์—… ์š”์ฒญ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋˜ํ•œ ํŒ๋ณ„ํ•ด์ค€ nextLevel๋กœ ๋ ˆ๋ฒจ์„ ๋‹ค์‹œ ์„ค์ •ํ•ด์ฃผ๋Š” ์ž‘์—…์€ UserService๊ฐ€ ์•„๋‹Œ,

์•„๋ž˜์™€ ๊ฐ™์ด User ํด๋ž˜์Šค์—์„œ upgradeLevel ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด Level์„ ๊ฐ–๊ณ  ์žˆ๋Š” User์—๊ฒŒ ์š”์ฒญ ํ•  ์ˆ˜  ์žˆ๋‹ค.

public class User {
	//...์ƒ๋žต
    public void upgradeLevel() {
		Level nextLevel = this.level.nextLevel();
		if(nextLevel == null) {
			throw new IllegalStateException( this.level + "cannot be upgraded");
		}
		else {
			this.level = nextLevel;
		}
	}
}

์ด๋ ‡๊ฒŒ ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์ฝ”๋“œ์˜ ๊ด€์ ์„ ๊ฐ–๊ณ  ๋ฆฌํŒฉํ† ๋ง ํ•ด์ฃผ๋ฉด ๋”์šฑ ์ฝ”๋“œ๋Š” ๊ฐ„๋‹จํ•ด ์งˆ ๊ฒƒ์ด๋‹ค.

 

๊ฐ„๋‹จํ•ด์ง„ upgradeLevel ๋ฉ”์†Œ๋“œ

private void upgradeLevel(User user) {
		user.upgradeLevel();
		userDao.update(user);
}

๐Ÿ“ƒ ์ •๋ฆฌ

  • ์œ„์™€ ๊ฐ™์ด ์ ๊ฒ€์„ ํ•ด์ค€ ํ›„ ๋ฆฌํŒฉํ† ๋ง์„ ํ•ด์ฃผ์ž!

  • ์ž๋ฐ”๋ฅผ ์“ฐ๋Š” ์ด์ƒ ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์ž!

์ฐธ๊ณ  ๋ฌธ์„œ : 

ํ† ๋น„์˜ ์Šคํ”„๋ง3 (์ด์ผ๋ฏผ)

๋ฐ˜์‘ํ˜•