본문 바로가기
C# 디자인패턴

04# Factory Method 패턴1 Creational Patterns

by NaHyungMin 2019. 5. 23.

static void Main(string[] args)

{

    //BuilderExample();

    //FactoryExample();

    FactoryMethodExample();

 

    //Console.WriteLine("Hello World!");

    Console.ReadKey();

}

 

static void FactoryMethodExample()

{

    Document[] documents = new Document[2];

    documents[0= new Resume();

    documents[1= new Report();

 

    foreach(Document document in documents)

    {

        Console.WriteLine(string.Format("{0}--", document.GetType().Name));

 

        foreach(IPage page in document.Pages)

        {

            Console.WriteLine(string.Format("Page : {0}", page.GetType().Name));

        }

    }

}

 

main.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Patterns.FactoryMethod

{

    public interface IPage

    {

    }

}

 

IPage.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Patterns.FactoryMethod.Pages

{

    public class BibliographyPage : IPage

    {

    }

}

 

BibliographyPage.cs

BibliographyPage를 포함한 Page들은 구조 동일

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Patterns.FactoryMethod

{

    public abstract class Document

    {

        private List<IPage> pages = new List<IPage>();

        public abstract void CreatePages();

 

        public List<IPage> Pages => pages;

        

        public Document()

        {

            this.CreatePages();

        }

    }

}

 

Document.cs

 

using System;

using System.Collections.Generic;

using System.Text;

using DesignPatterns.Patterns.FactoryMethod.Pages;

 

namespace DesignPatterns.Patterns.FactoryMethod

{

    public class Report : Document

    {

        public override void CreatePages()

        {

            Pages.Add(new IntroductionPage());

            Pages.Add(new ResultsPage());

            Pages.Add(new ConclusionPage());

            Pages.Add(new SummaryPage());

            Pages.Add(new BibliographyPage());

        }

    }

}

 

Report.cs

 

using System;

using System.Collections.Generic;

using System.Text;

using DesignPatterns.Patterns.FactoryMethod.Pages;

 

namespace DesignPatterns.Patterns.FactoryMethod

{

    public class Resume : Document

    {

        public override void CreatePages()

        {

            Pages.Add(new SkillsPage());

            Pages.Add(new EducationPage());

            Pages.Add(new ExperiencePage());

        }

    }

}

 

Resume.cs

 

참고 사이트

https://www.dofactory.com/net/factory-method-design-pattern

 

Factory Method .NET Design Pattern in C# and VB - dofactory.com

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.     The classes and objects participating in this pattern are: This structural code demonstrate

www.dofactory.com