728x90

๐Ÿ“š Computer Science/Design Pattern 5

[Unity][๋””์ž์ธ ํŒจํ„ด] Simple Factory Pattern(์‹ฌํ”Œ ํŒฉํ† ๋ฆฌ ํŒจํ„ด)

*Factory: ๊ฐ์ฒด ์ƒ์„ฑ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ํŒฉํ† ๋ฆฌ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. *Simple Factory: ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ผ์„ ์ „๋‹ดํ•˜๋Š” ํด๋ž˜์Šค. Simple Factory Pattern ์—๋Š” ์ฃผ์–ด์ง„ ์ž…๋ ฅ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค๋ฅธ ์œ ํ˜•์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋Š” ํŒฉํ† ๋ฆฌ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค. Simple Factory ๋Š” ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ ํ•ญ์ƒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ผ ํŒจํ„ด์œผ๋กœ ์ทจ๊ธ‰ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๋‹ค๋งŒ, ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ํŒจํ„ด์ด๋‚˜ ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์˜ ๊ธฐ๋ณธ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•Œ์•„๋‘์ž. using UnityEngine; public abstract class Unit { public abstract void move(); } public class Marine : Unit { public Marine() { Debug.Log("M..

[Unity][๋””์ž์ธ ํŒจํ„ด] Component Pattern

*Component Pattern: ๋กœ์ง์„ ๊ธฐ๋Šฅ๋ณ„๋กœ ์ปดํฌ๋„ŒํŠธ ํ•˜๋Š” ๊ฒƒ. ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  Decoupling(๋…์ž์ ์ธ ํ๋ฆ„์„ ๋ณด์ด๋Š” ๊ฒƒ) ์‹œํ‚ค๋Š” ๊ฒƒ์€ ํ˜‘์—…ํ•˜์—ฌ ๊ฐœ๋ฐœ์„ ๋งŽ์ด ํ•ด๋ณธ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ๋Š” ์ž์—ฐํžˆ ๋…น์•„ ์žˆ๋Š” ์Šต๊ด€์ด๋‹ค. ์ด๋Ÿฐ ๊ตฌ์กฐ์ ์ธ ๊ฐœ์„  ์ž‘์—…์„ ์„ ๋ฐฐ ๊ฐœ๋ฐœ์ž๋“ค์ด ์ด๋ฏธ ์ปดํฌ๋„ŒํŠธ ํŒจํ„ด์œผ๋กœ ๋ช…๋ช…ํ•ด ๋‘์—ˆ๋‹ค. ํŠนํžˆ Unity ์˜ GameObject ๋Š” Component ํŒจํ„ด์ด ์ž˜ ๋ฐ˜์˜๋˜์–ด ์„ค๊ณ„๋œ ํด๋ž˜์Šค์ด๋‹ค. GetComponet Method ๋กœ ๊ฐ๊ฐ์˜ Decoupling ๋œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋ถˆ๋Ÿฌ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ ๋‹ˆํ‹ฐ๋Š” ์ƒˆ๋กœ์šด ํ–‰๋™์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ปดํฌ์ง€์…˜(composition: ํ•ฉ์„ฑ) ๊ตฌ์กฐ๋ฅผ ์ฑ„์šฉํ•œ๋‹ค. ์ด ์ ์€ ์ƒ์†(Inheritance)๋ฅผ ํ†ตํ•ด ํ™•์žฅ๋˜๋Š” ์˜ค๋ธŒ์ ํŠธ ์ง€ํ–ฅ ํด๋ž˜์Šค ๊ณ„์ธต๊ณผ๋Š” ์œ„์ƒ์„ ๋‹ฌ๋ฆฌ..

[C#][Unity][๋””์ž์ธ ํŒจํ„ด] Strategy Pattern(์ „๋žต ํŒจํ„ด)

*Strategy Pattern: ์—ฌ๋Ÿฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•˜๋‚˜์˜ ์ถ”์ƒ์ ์ธ ์ ‘๊ทผ์ (์ธํ„ฐํŽ˜์ด์Šค)์„ ๋งŒ๋“ค์–ด์„œ ์ ‘๊ทผ์ ์—์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์„œ๋กœ ๊ตํ™˜ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๋Š” ํŒจํ„ด *์‚ฌ์šฉ ์˜ˆ: ์›Œ๋“œ ๋ฌธ์„œ์—์„œ ํ”„๋ฆฐํ„ฐ, ํฐํŠธ ์‚ฌ์šฉ. ์–ด๋–ค ๊ธฐ์ข…์˜ ํ”„๋ฆฐํŠธ๋”๋ผ๋„ 'Print' ๋ฒ„ํŠผ๋งŒ ๋ˆ„๋ฅด๋ฉด ๋˜‘๊ฐ™์ด ๋™์ž‘ํ•จ. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์˜ ๋ฌด๊ธฐ(๊ต์ฒด ํ›„) ์‚ฌ์šฉ. ์–ด๋–ค ๋ฌด๊ธฐ๋ผ๋„ '๊ณต๊ฒฉ' ๋ฒ„ํŠผ๋งŒ ๋ˆ„๋ฅด๋ฉด ๊ณต๊ฒฉ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•จ. public class MyWeapon { //์ ‘๊ทผ์  private IWeapon weapon; //๋ฌด๊ธฐ ์ฒด์ธ์ง€ public void SetWeapon(IWeapon weapon) { this.weapon = weapon; } //๋ฌด๊ธฐ์‚ฌ์šฉ public void Shoot() { weapon.Shoot(); } } IWeapon ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜..

[C#][Unity][๋””์ž์ธ ํŒจํ„ด] Command ํŒจํ„ด

* Commnad ํŒจํ„ด: ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ์‹ค์ฒดํ™”, ์ฆ‰ ๊ฐ์ฒด๋กœ ๊ฐ์‹ผ ๊ฒƒ. : ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๊ฐ์ฒด๋กœ ๋งŒ๋“  ์ด์œ ๋Š” ๋””์ปคํ”Œ๋ง์œผ๋กœ ์ฝ”๋“œ๊ฐ€ ์œ ์—ฐํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ. ex) ์ž…๋ ฅํ‚ค ๋ณ€๊ฒฝ, ์‹คํ–‰์ทจ์†Œ/์žฌ์‹คํ–‰ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ. 1. Command ํŒจํ„ด์—†์ด ๋งŒ๋“  GetKey ๊ธฐ๋Šฅ using System.Collections; using UnityEngine; public class Player : MonoBehaviour { [SerializeField] private GameObject shield; [SerializeField] private GameObject cannon; [SerializeField] private Transform firePos; void Update() { if (Input.GetKeyDown(..

[Unity][๋””์ž์ธ ํŒจํ„ด] Observer Pattern

ํ•œ ๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ทธ ๊ฐ์ฒด์— ์˜์กดํ•˜๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์—๊ฒŒ ์—ฐ๋ฝ์ด ๊ฐ€๊ณ  ์ž๋™์œผ๋กœ ๋‚ด์šฉ์ด ๊ฐฑ์‹ ๋˜๋Š” ๋ฐฉ์‹. ์ฃผ์ œ์™€ ์˜ต์ €๋ฒ„๊ฐ€ ๋Š์Šจํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜์–ด ์žˆ๋Š” ๊ฐ์ฒด ๋””์ž์ธ. Subject๊ฐ€ Observer์— ๋Œ€ํ•ด์„œ ์•„๋Š” ๊ฒƒ์€ Observer๊ฐ€ ํŠน์ • ์ธํ„ฐํŽ˜์ด์Šค(Observer ์ธํ„ฐํŽ˜์ด์Šค)๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค๋Š” ๊ฒƒ ๋ฟ. Observer๋Š” ์–ธ์ œ๋“ ์ง€ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ. ์ƒˆ๋กœ์šด ํ˜•ํƒœ์˜ Observer๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค ํ•ด๋„ Subject๋ฅผ ์ „ํ˜€ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†์Œ. ์ƒˆ๋กœ์šด ํด๋ž˜์Šค์—์„œ Observer ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋จ. Subject๋‚˜ Observer๊ฐ€ ๋ฐ”๋€Œ๋”๋ผ๋„ ์„œ๋กœ์—๊ฒŒ ์ „ํ˜€ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Œ. ๊ทธ๋ž˜์„œ Subject์™€ Observer๋Š” ์„œ๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. 1. ๊ธฐ๋ณธ ํ˜ธ์ถœ //์˜ต์ €๋ฒ„ ์ถ”์ƒํด๋ž˜์Šค //: ์˜ต์ €๋ฒ„๋“ค์ด ๊ตฌํ˜„..

728x90