Skip to content

Latest commit

ย 

History

History
164 lines (99 loc) ยท 3.02 KB

Adapter Pattern.md

File metadata and controls

164 lines (99 loc) ยท 3.02 KB

์–ด๋Œ‘ํ„ฐ ํŒจํ„ด


  • ์šฉ๋„ : ํด๋ž˜์Šค๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Œ (๋‹ค๋ฅธ ๊ณณ์—์„œ ๊ฐœ๋ฐœํ–ˆ๋‹ค๊ฑฐ๋‚˜, ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์„ ๋•Œ) ์ค‘๊ฐ„์—์„œ ๋ณ€ํ™˜ ์—ญํ• ์„ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๊ฐ€ ํ•„์š” โ†’ ์–ด๋Œ‘ํ„ฐ ํŒจํ„ด

  • ์‚ฌ์šฉ ๋ฐฉ๋ฒ• : ์ƒ์†

  • ํ˜ธํ™˜๋˜์ง€ ์•Š์€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ ๊ทธ๋Œ€๋กœ ํ™œ์šฉ ๊ฐ€๋Šฅ

  • ํ–ฅํ›„ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋ฐ”๋€Œ๋”๋ผ๋„, ๋ณ€๊ฒฝ ๋‚ด์—ญ์€ ์–ด๋Œ‘ํ„ฐ์— ์บก์Šํ™” ๋˜๋ฏ€๋กœ ํด๋ผ์ด์–ธํŠธ ๋ฐ”๋€” ํ•„์š”X


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

img

์•„์ดํฐ์˜ ์ด์–ดํฐ์„ ์ƒ๊ฐํ•ด๋ณด์ž

๊ฐ€์žฅ ํ”ํ•œ ์ด์–ดํฐ ์žญ์„ ์•„์ดํฐ์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ์žญ ์ž์ฒด๊ฐ€ ๋งž์ง€ ์•Š๋Š”๋‹ค.

๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” ์–ด๋Œ‘ํ„ฐ๋ฅผ ๋”ฐ๋กœ ๊ตฌ๋งคํ•ด์„œ ์—ฐ๊ฒฐํ•ด์•ผ ์ด๋Ÿฐ ์ด์–ดํฐ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

์ด์ฒ˜๋Ÿผ ์–ด๋Œ‘ํ„ฐ๋Š” ํ•„์š”๋กœ ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค

img

์ด์ฒ˜๋Ÿผ ์—…์ฒด์—์„œ ์ œ๊ณตํ•œ ํด๋ž˜์Šค๊ฐ€ ๊ธฐ์กด ์‹œ์Šคํ…œ์— ๋งž์ง€ ์•Š์œผ๋ฉด?

๊ธฐ์กด ์‹œ์Šคํ…œ์„ ์ˆ˜์ •ํ•  ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์–ด๋Œ‘ํ„ฐ๋ฅผ ํ™œ์šฉํ•ด ์œ ์—ฐํ•˜๊ฒŒ ํ•ด๊ฒฐํ•˜์ž


์ฝ”๋“œ๋กœ ์–ด๋Œ‘ํ„ฐ ํŒจํ„ด ์ดํ•ดํ•˜๊ธฐ

์˜ค๋ฆฌ์™€ ์น ๋ฉด์กฐ ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ

๋งŒ์•ฝ ์˜ค๋ฆฌ ๊ฐ์ฒด๊ฐ€ ๋ถ€์กฑํ•ด์„œ ์น ๋ฉด์กฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด?

๋‘ ๊ฐ์ฒด๋Š” ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ, ๋ฐ”๋กœ ์น ๋ฉด์กฐ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•จ

๋”ฐ๋ผ์„œ ์น ๋ฉด์กฐ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ƒ์„ฑํ•ด์„œ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค

  • Duck.java
package AdapterPattern;

public interface Duck {
	public void quack();
	public void fly();
}
  • Turkey.java
package AdapterPattern;

public interface Turkey {
	public void gobble();
	public void fly();
}
  • WildTurkey.java
package AdapterPattern;

public class WildTurkey implements Turkey {

	@Override
	public void gobble() {
		System.out.println("Gobble gobble");
	}

	@Override
	public void fly() {
		System.out.println("I'm flying a short distance");
	}
}
  • TurkeyAdapter.java
package AdapterPattern;

public class TurkeyAdapter implements Duck {

	Turkey turkey;

	public TurkeyAdapter(Turkey turkey) {
		this.turkey = turkey;
	}

	@Override
	public void quack() {
		turkey.gobble();
	}

	@Override
	public void fly() {
		turkey.fly();
	}

}
  • DuckTest.java
package AdapterPattern;

public class DuckTest {

	public static void main(String[] args) {

		MallardDuck duck = new MallardDuck();
		WildTurkey turkey = new WildTurkey();
		Duck turkeyAdapter = new TurkeyAdapter(turkey);

		System.out.println("The turkey says...");
		turkey.gobble();
		turkey.fly();

		System.out.println("The Duck says...");
		testDuck(duck);

		System.out.println("The TurkeyAdapter says...");
		testDuck(turkeyAdapter);

	}

	public static void testDuck(Duck duck) {

		duck.quack();
		duck.fly();

	}
}

์•„๊นŒ ํ™•์ธํ•œ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ Target์€ ์˜ค๋ฆฌ์— ํ•ด๋‹นํ•˜๋ฉฐ, Adapter๋Š” ์น ๋ฉด์กฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.