팩토리 패턴
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public abstract class Enemy { // Common enemy behavior and properties } public class Zombie : Enemy { // Zombie-specific behavior and properties } public class Ghost : Enemy { // Ghost-specific behavior and properties } public static class EnemyFactory { public static Enemy CreateEnemy(string type) { switch (type) { case "Zombie": return new Zombie(); case "Ghost": return new Ghost(); default: throw new ArgumentException("Invalid enemy type"); } } } public class EnemySpawner : MonoBehaviour { private void SpawnEnemy(string type) { Enemy enemy = EnemyFactory.CreateEnemy(type); // Spawn and initialize the enemy } } | cs |
데코레이터 패턴
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | public abstract class Weapon { public abstract int GetDamage(); } public class Sword : Weapon { public override int GetDamage() { return 10; } } public abstract class WeaponDecorator : Weapon { protected Weapon _weapon; public WeaponDecorator(Weapon weapon) { _weapon = weapon; } public override int GetDamage() { return _weapon.GetDamage(); } } public class FireEnchantment : WeaponDecorator { public FireEnchantment(Weapon weapon) : base(weapon) { } public override int GetDamage() { return base.GetDamage() + 5; } } | cs |
스테이트 패턴
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | public interface IState { void Enter(); void Execute(); void Exit(); } public class IdleState : IState { public void Enter() { /* Enter idle state logic */ } public void Execute() { /* Execute idle state logic */ } public void Exit() { /* Exit idle state logic */ } } public class MovingState : IState { public void Enter() { /* Enter moving state logic */ } public void Execute() { /* Execute moving state logic */ } public void Exit() { /* Exit moving state logic */ } } public class StateMachine { private IState _currentState; public void ChangeState(IState newState) { _currentState?.Exit(); _currentState = newState; _currentState.Enter(); } public void Update() { _currentState.Execute(); } } | cs |
글 하나에 쓰지 왜 도배함? 그리고 스니펫 다 타이핑할텐데 왜 올리냐 글 삭제 해라
글자제한때문에, 스니펫 올려달라고한 사람 있어서 그런데 문제된다 싶으면 자삭하겠음
잘 보고 있는데 왜 지랄이냐
정신병 있는 새끼인가
데코레이터 패턴은 처음듣네
패턴 한번 봐야지하면서 아직 안보고 있었는데, 이렇게 정리된거 보니까 결국 패턴이라는게 클래스를 상속하는 형태로 분류하네.