Skip to content

Latest commit

ย 

History

History
68 lines (41 loc) ยท 2.26 KB

Strategy Pattern.md

File metadata and controls

68 lines (41 loc) ยท 2.26 KB

์ŠคํŠธ๋ ˆํ‹ฐ์ง€ ํŒจํ„ด(Strategy Pattern)

์–ด๋–ค ๋™์ž‘์„ ํ•˜๋Š” ๋กœ์ง์„ ์ •์˜ํ•˜๊ณ , ์ด๊ฒƒ๋“ค์„ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด(์บก์Šํ™”) ๊ด€๋ฆฌํ•˜๋Š” ํŒจํ„ด

์ƒˆ๋กœ์šด ๋กœ์ง์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ๋•Œ, ํ•œ๋ฒˆ์— ํšจ์œจ์ ์œผ๋กœ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.


[ ์ŠˆํŒ… ๊ฒŒ์ž„์„ ์„ค๊ณ„ํ•˜์‹œ์˜ค ]
์œ ๋‹› ์ข…๋ฅ˜ : ์ „ํˆฌ๊ธฐ, ํ—ฌ๋ฆฌ์ฝฅํ„ฐ
์œ ๋‹›๋“ค์€ ๋ฏธ์‚ฌ์ผ์„ ๋ฐœ์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
์ „ํˆฌ๊ธฐ๋Š” ์ง์„  ๋ฏธ์‚ฌ์ผ์„, ํ—ฌ๋ฆฌ์ฝฅํ„ฐ๋Š” ์œ ๋„ ๋ฏธ์‚ฌ์ผ์„ ๋ฐœ์‚ฌํ•œ๋‹ค.
ํ•„์‚ด๊ธฐ๋กœ๋Š” ํญํƒ„์ด ์žˆ๋Š”๋ฐ, ์ „ํˆฌ๊ธฐ์—๋Š” ์žˆ๊ณ  ํ—ฌ๋ฆฌ์ฝฅํ„ฐ์—๋Š” ์—†๋‹ค.

Strategy pattern์„ ์ ์šฉํ•œ ์„ค๊ณ„๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ƒ์†์€ ๋ฌด๋ถ„๋ณ„ํ•œ ์†Œ์Šค ์ค‘๋ณต์ด ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ปดํฌ์ง€์…˜์„ ํ™œ์šฉํ•œ๋‹ค. (์ธํ„ฐํŽ˜์ด์Šค์™€ ๋กœ์ง์˜ ํด๋ž˜์Šค์™€์˜ ๊ด€๊ณ„๋ฅผ ์ปดํฌ์ง€์…˜ํ•˜๊ณ , ์œ ๋‹›์—์„œ ์ƒํ™ฉ์— ๋งž๋Š” ๋กœ์ง์„ ์“ฐ๊ฒŒ๋” ์œ ๋„ํ•˜๋Š” ๊ฒƒ)


  • ๋ฏธ์‚ฌ์ผ์„ ์˜๋Š” ๊ฒƒ๊ณผ ํญํƒ„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์บก์Šํ™”ํ•˜์ž

    ShootAction๊ณผ BombAction์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ฐ์ž ํ•„์š”ํ•œ ๋กœ์ง์„ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด implementํ•œ๋‹ค.

  • ์ „ํˆฌ๊ธฐ์™€ ํ—ฌ๋ฆฌ์ฝฅํ„ฐ๋ฅผ ๋ฌถ์„ Unit ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ž

    Unit์—๋Š” ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์ด ๋“ค์–ด์žˆ๊ณ , ๋ฏธ์‚ฌ์ผ๊ณผ ํญํƒ„์„ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•ด variable๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋“ค์„ ์„ ์–ธํ•œ๋‹ค.


์ „ํˆฌ๊ธฐ์™€ ํ—ฌ๋ฆฌ์ฝฅํ„ฐ๋Š” Unit ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ , ์ƒ์„ฑ์ž์— ๋งž๋Š” ๋กœ์ง์„ ์ •์˜ํ•ด์ฃผ๋ฉด ๋๋‚œ๋‹ค.

์ „ํˆฌ๊ธฐ ์˜ˆ์‹œ
class Fighter extends Unit {
    private ShootAction shootAction;
    private BombAction bombAction;
    
    public Fighter() {
        shootAction = new OneWayMissle();
        bombAction = new SpreadBomb();
    }
}

Fighter.doAttack()์„ ํ˜ธ์ถœํ•˜๋ฉด, OneWayMissle์˜ attack()์ด ํ˜ธ์ถœ๋  ๊ฒƒ์ด๋‹ค.


์ •๋ฆฌ

์ด์ฒ˜๋Ÿผ Strategy Pattern์„ ํ™œ์šฉํ•˜๋ฉด ๋กœ์ง์„ ๋…๋ฆฝ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํŽธํ•ด์ง„๋‹ค. ๋กœ์ง์— ๋“ค์–ด๊ฐ€๋Š” 'ํ–‰๋™'์„ ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•˜๊ณ , ์ธํ„ฐํŽ˜์ด์Šค์™€ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ!



[์ฐธ๊ณ ]

๋งํฌ