옵저버 패턴, 람다식, 배열과 리스트

jinsuk·2023년 9월 6일

옵저버 패턴

using System;
using System.Collections.Generic;

namespace csharp
{
    // 옵저버 패턴
    // -> 구독자들한테 특정 이벤트가 발생했을때 메세지를 뿌리는 방식
    // 이벤트를 사용할경우 구독신청은 가능하지만 외부에서 마음대로 호출은 불가능
    class InputManager
    {
        public delegate void OnInputKey();
        public event OnInputKey InputKey;
        public void Update()
        {
            if (Console.KeyAvailable == false)
                return;
            
            ConsoleKeyInfo info = Console.ReadKey();
            if (info.Key == ConsoleKey.A)
            {
                InputKey();
            }
        }
    }
}
using System;
using System.Collections.Generic;

namespace csharp
{
    class Program
    {
        static void OnInputTest()
        {
            Console.WriteLine("Input Received!");
        }
        static void Main(string[] args)
        {
            InputManager inputManager = new InputManager();
            inputManager.InputKey += OnInputTest;

            while (true)
            {
                inputManager.Update();
            }
        }
    }
}

람다식

using System;
using System.Collections.Generic;

namespace csharp
{
    enum ItemType
    {
        Weapon,
        Armor,
        Amulet,
        Ring
    }

    enum Rarity
    {
        Normal,
        Uncommon,
        Rare
    }

    class Item
    {
        public ItemType ItemType;
        public Rarity Rarity;
    }

    
    class Program
    {
        static List<Item> _items = new List<Item>();

        static Item FindItem(Func<Item, bool> selector)
        {
            foreach (Item item in _items)
            {
                if (selector(item))
                    return item;
            }
            return null;

        }
        static void Main(string[] args)
        {
            _items.Add(new Item() {ItemType = ItemType.Weapon, Rarity = Rarity.Normal});
            _items.Add(new Item() {ItemType = ItemType.Armor, Rarity = Rarity.Uncommon});
            _items.Add(new Item() {ItemType = ItemType.Ring, Rarity = Rarity.Rare});

            // delegate를 직접 선언하지 않아도, 이미 만들어진 애들이 존재한다
            // -> 반환타입이 있을 경우 Func
            // -> 반환타입이 없을 경우 Action

            // Lambda : 일회용 함수를 만드는데 사용하는 문법, 익명함수/무명함수
            Item item = FindItem((Item item) => {return item.ItemType == ItemType.Weapon;});
        }
    }
}

배열과 리스트의 차이

using System;
using System.Collections.Generic;

namespace csharp
{
    class Program
    {
        
        static void Main(string[] args)
        {	
        	//일반적인 배열
            int[] arr = new int[1000];
            arr[0] = 1;

            //List <- 동적 배열
            List<int> list = new List<int>();
            for (int i = 0; i < 5; i++)
                list.Add(i);
            // 배열에서는 제공하지 않는 삽입 삭제 전체삭제 함수 제공

            for (int i = 0; i < list.Count; i++)
                Console.WriteLine(list[i]);
            foreach (int num in list)
                Console.WriteLine(num);
        }
    }
}
profile
공부기록용

0개의 댓글