728x90

๋””์ž์ธํŒจํ„ด 2

[Design Pattern] Strategy Pattern

์ž‘์„ฑ์ผ: 2018.01.22 Strategy pattern ์ผ๋ช…, ์ „๋žตํŒจํ„ด ๋ชฉ์  ๊ฐ๊ฐ์„ ์บก์Šํ™”ํ•˜์—ฌ ๋ณ€ํ™” ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“  ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ง‘ํ•ฉ์„ ์ •์˜ํ•œ๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค์— ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๊ณ , ์ž์„ธํ•œ ๊ตฌํ˜„์€ ํŒŒ์ƒ๋œ ํด๋ž˜์Šค์— ํ•œ๋‹ค. Strategy ํŒจํ„ด์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ทธ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ์™€ ๋…๋ฆฝ์ ์œผ๋กœ ๋ณ€ํ™”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. โž” OCP OCP(Open-Closed Principle;๊ฐœ๋ฐฉ ํ์‡„ ์›์น™) ํ™•์žฅ์—๋Š” ์—ด๋ ค์žˆ๊ณ , ์ˆ˜์ •์—๋Š” ๋‹ซํ˜€ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ•ต์‹ฌ ์›์น™ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ ํด๋ผ์ด์–ธํŠธ๋Š” ๋ณ€ํ™”์™€ ๊ด€๋ จํ•ด์„œ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค. โž” ํŒŒ์ƒ๋œ ํด๋ž˜์Šค ์ˆ˜๊ฐ€ ๋ณ€ํ–ˆ์„ ๊ฒฝ์šฐ, ํŒŒ์ƒ๋œ ํด๋ž˜์Šค์˜ ๊ตฌํ˜„์ด ๋ฐ”๋€Œ์—ˆ์„ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์˜ํ–ฅ์ด ์—†์–ด์•ผ ํ•œ๋‹ค. ๊ตฌ์กฐ 2๊ฐ€์ง€ ๊ตฌํ˜„ ๋ฐฉ๋ฒ• Abstract base class..

[Design Pattern] Observer Pattern

์ž‘์„ฑ์ผ: 2017.04.26 Observer Pattern ํ•œ ๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์ด ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค. ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด(Subject) / ์ƒํƒœ์˜ ๋ณ€๊ฒฝ์„ ๊ด€์ฐฐํ•˜๋Š” ๊ฐ์ฒด(Observer Object) โžž 1:N ๊ด€๊ณ„ ์žฅ์  Loose coupling subject์™€ observer๋Š” ์„œ๋กœ์˜ ์กด์žฌ๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค. Scalability(ํ™•์žฅ์„ฑ) client-server ๊ตฌ์กฐ๋ณด๋‹ค ๋” ์ข‹์€ ํ™•์žฅ์„ฑ์„ ๊ฐ–๋Š”๋‹ค. Java Observable / Observer Observable private boolean changed = false; private Vector obs; //Observable์€ ํ•˜๋‚˜ ์ด์ƒ์˜ Observer๋ฅผ ๊ฐ–๋Š”๋‹ค. ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด Observer์—๊ฒŒ ์•Œ๋ ค์ค€๋‹ค..