본문 바로가기
C#프로그래밍

ItemFactory

by 노재두내 2023. 7. 27.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace LearnDotnet
{
    public class App
    {

        //생성자
        public App()
        {

            ItemFactory factory = new ItemFactory();

            factory.CreateItem((item) =>
            {
                Console.WriteLine("{0}이 생성되었습니다",item.name );
            });
        }
       
    }

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    public class ItemFactory
    {
        
        public ItemFactory()
        {
            
        }
        public void CreateItem(Action<Item>callback)
        {
            
            callback(new Item("장검"));
            
        }
    }
}

Item 타입을 써야하고 객체가 없으니까 new 해서 던진다?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    
    public class Item
    {
        public string name;
        public Item(string name)
        {
            this.name = name;
        }
    }
}

만약에 

public void CreateItem(Action<string>callback)
string 으로 쓰고싶으면 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace LearnDotnet
{
    public class App
    {

        //생성자
        public App()
        {

            ItemFactory factory = new ItemFactory();

            factory.CreateItem((itemName) =>
            {
                Console.WriteLine("{0}이 생성되었습니다",itemName );
            });
        }
       
    }

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    public class ItemFactory
    {
        
        public ItemFactory()
        {
            
        }
        public void CreateItem(Action<string>callback)
        {
            
            callback("장검");
            
        }
    }
}

 

'C#프로그래밍' 카테고리의 다른 글

27일 복습  (0) 2023.07.27
직렬화 역직렬화 연습  (0) 2023.07.27
매개변수 있는 대리자(Action 연습  (0) 2023.07.27
대리자(Action) 연습  (0) 2023.07.27
디자인패턴/싱글톤 패턴/딕셔너리  (0) 2023.07.27