"์•ˆ๋…•ํ•˜์„ธ์š”, ์•„๋ฏธ๊ณ ! ์ €์˜ˆ์š”. ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๋˜ ๋‹ค๋ฅธ ๊ด€์ ์„ ์ œ์‹œํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ํด๋ž˜์Šค๋Š” ํŠน์ • ๊ฐ์ฒด์˜ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋Œ€์กฐ์ ์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฐ์ฒด์˜ ๋Šฅ๋ ฅ ๋˜๋Š” ๊ฐ์ฒด์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ฒด ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ์—ญํ• ์ž…๋‹ˆ๋‹ค."

์˜ˆ๋ฅผ ๋“ค์–ด ์ž๋™์ฐจ, ์ž์ „๊ฑฐ, ์˜คํ† ๋ฐ”์ด, ๋ฐ”ํ€ด์™€ ๊ฐ™์€ ์‚ฌ๋ฌผ์€ ํด๋ž˜์Šค์™€ ๊ฐœ์ฒด๋กœ ๊ฐ€์žฅ ์ž˜ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ยซI can moveยป, ยซI can carry peopleยป, ยซI can parkยป๊ณผ ๊ฐ™์€ ๋Šฅ๋ ฅ์€ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋” ์ž˜ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ์ด ์˜ˆ๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.

์ž๋ฐ” ์ฝ”๋“œ ์„ค๋ช…
interface Moveable
{
void move(String newAddress);
}
์ด๋™ ๋Šฅ๋ ฅ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
interface Driveable
{
void drive(Driver driver);
}
์ด๋™ ๋Šฅ๋ ฅ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
interface Transport
{
void addStuff(Object stuff);
Object removeStuff();
}
ํ™”๋ฌผ์„ ์šด๋ฐ˜ํ•˜๋Š” ๋Šฅ๋ ฅ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
class Wheel implements Moveable
{
...
}
ยซํœ ยป ํด๋ž˜์Šค. ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
class Car implements Moveable, Drivable, Transport
{
...
}
ยซ์ž๋™์ฐจยป ํด๋ž˜์Šค. ์›€์ง์ด๊ณ , ์‚ฌ๋žŒ์ด ์šด์ „ํ•˜๊ณ , ํ™”๋ฌผ์„ ์šด๋ฐ˜ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
class Skateboard implements Moveable, Driveable
{
...
}
ยซ์Šค์ผ€์ดํŠธ๋ณด๋“œยป ์ˆ˜์—…. ์›€์ง์ผ ์ˆ˜ ์žˆ๊ณ  ์‚ฌ๋žŒ์ด ์กฐ์ข…ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์‚ถ์„ ํฌ๊ฒŒ ๋‹จ์ˆœํ™”ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์—๋Š” ์ข…์ข… ์ˆ˜์ฒœ ๊ฐœ์˜ ๊ฐœ์ฒด, ์ˆ˜๋ฐฑ ๊ฐœ์˜ ํด๋ž˜์Šค ๋ฐ ์ˆ˜์‹ญ ๊ฐœ์˜ ์ธํ„ฐํŽ˜์ด์Šค(์—ญํ• )๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ญํ• ์€ ๊ฑฐ์˜ ์—†์ง€๋งŒ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•(ํด๋ž˜์Šค)์œผ๋กœ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์š”์ ์€ ๋‹ค๋ฅธ ๋ชจ๋“  ํด๋ž˜์Šค์™€์˜ ์ƒํ˜ธ ์ž‘์šฉ์„ ์ •์˜ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค)๊ณผ ์ƒํ˜ธ ์ž‘์šฉํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋‹น์‹ ์ด ๋กœ๋ด‡ ๋นŒ๋”๋ผ๊ณ  ์ƒ์ƒํ•ด๋ณด์‹ญ์‹œ์˜ค. ์ˆ˜์‹ญ ๊ฐœ์˜ ํ•˜์œ„ ๋กœ๋ด‡์ด ์žˆ์œผ๋ฉฐ ๊ฐ๊ฐ ์—ฌ๋Ÿฌ ๊ธฐ์ˆ ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธ‰ํžˆ ๋ฒฝ ๊ฑด์„ค์„ ์™„๋ฃŒํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. "๋นŒ๋“œ" ๋Šฅ๋ ฅ์ด ์žˆ๋Š” ๋ชจ๋“  ๋กœ๋ด‡์„ ์„ ํƒํ•˜๊ณ  ๋ฒฝ์„ ๋งŒ๋“ค๋ผ๊ณ  ์ง€์‹œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ์–ด๋–ค ๋กœ๋ด‡์ด ๊ทธ๊ฒƒ์„ ํ•˜๋Š”์ง€ ์ •๋ง๋กœ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋กœ๋ด‡์‹ ๋ฌผ๋ฟŒ๋ฆฌ๊ฐœ๊ฐ€ ๋˜๊ฒŒ ํ•ด์ฃผ์„ธ์š”. ๊ตฌ์ถ• ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ๊ตฌ์ถ•ํ•˜๋„๋ก ํ•˜์‹ญ์‹œ์˜ค.

์ฝ”๋“œ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

์ž๋ฐ” ์ฝ”๋“œ ์„ค๋ช…
static interface WallBuilder
{
void buildWall();
}
ยซ๋ฒฝ์„ ์Œ“๋Š”ยป ๋Šฅ๋ ฅ. ยซ๋ฒฝ์„ ์Œ“๋‹คยป ๋ช…๋ น์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค(์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Œ).
static class WorkerRobot implements WallBuilder
{
void buildWall()
 {
โ€ฆ
 }
}
static class GuardRobot implements WallBuilder
{
void buildWall()
 {
โ€ฆ
 }
}
static class WateringCan
{
โ€ฆ
}
์ด ๋Šฅ๋ ฅ/๊ธฐ์ˆ ์„ ๊ฐ€์ง„ ๋กœ๋ด‡.

๋ฌผ ๋ฟŒ๋ฆฌ๊ฐœ๋Š” ๋ฒฝ์„ ๋งŒ๋“ค ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค(WallBuilder ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์Œ).

public static void main(String[] args)
{
 //add all robots to a list
 ArrayList robots = new ArrayList();
 robots.add(new WorkerRobot());
 robots.add(new GuardRobot());
 robots.add(new WateringCan());

 //build a wall if you can
 for (Object robot: robots)
 {
  if (robot instanceof WallBuilder)
  {
   WallBuilder builder = (WallBuilder) robot;
   builder.buildWall();
   }
  }
 }
}
๋ฒฝ์„ ์„ธ์šฐ๋ผ๋Š” ๋ช…๋ น์„ ์–ด๋–ป๊ฒŒ ๋‚ด๋ฆฝ๋‹ˆ๊นŒ?

"๋†€๋ผ์šธ ์ •๋„๋กœ ํฅ๋ฏธ๋กญ๊ตฐ์š”. ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ด๋ ‡๊ฒŒ ํฅ๋ฏธ๋กœ์šด ์ฃผ์ œ๊ฐ€ ๋  ์ค„์€ ๊ฟˆ์—๋„ ๋ชฐ๋ž์Šต๋‹ˆ๋‹ค."

"๊ทธ๋ฆฌ๊ณ  ์ผ๋ถ€! ๋‹คํ˜•์„ฑ๊ณผ ํ•จ๊ป˜, ๊ทธ๊ฒƒ์€ ์™„์ „ํžˆ ๋†€๋ผ์šด ๊ฒƒ์ž…๋‹ˆ๋‹ค."