iT邦幫忙

0

【C#】Creational Patterns Builder Mode

c#
  • 分享至 

  • xImage
  •  

The Builder design pattern separates the construction of a complex object from its representation so that the same construction process can create different representations.


學習目標: 創建者模式的概念及實務

學習難度: ☆☆☆


namespace ConsoleApp1
{
    class GameFactory
    {
        public void Develop(Programmer programmer) 
        {
            programmer.Coding();
        }
    }

    abstract class Programmer
    {
        public abstract void Coding();
        
        public abstract Project GetProject();
    }

    class ClientProgrammer : Programmer
    {
        private Project project = new Project();

        public override void Coding()
        {
            project.Name = "AOE3 RTS";
        }

        public override Project GetProject()
        {
            return project;
        }
    }

    class ServerProgrammer : Programmer
    {
        private Project project = new Project();

        public override void Coding()
        {
            project.Name = "Socket TCP";
        }

        public override Project GetProject()
        {
            return project;
        }
    }


    class Project
    {
        public string Name { get; set; }

        public void Demo()
        {
            Console.WriteLine($"-------Project Name is--{Name} -------");
        }
    }

    public class MainProgram
    {
        public static void Main()
        {
            GameFactory gamefactory = new GameFactory();

            Programmer clientprogrammer = new ClientProgrammer();

            Programmer serverprogrammer = new ServerProgrammer();

            gamefactory.Develop(clientprogrammer);

            gamefactory.Develop(serverprogrammer);

            Project ClientProject = clientprogrammer.GetProject();

            ClientProject.Demo();

            Project ServerProject = serverprogrammer.GetProject();

            ServerProject.Demo();
        }
    }
}

參考資料:

https://www.dofactory.com/net/builder-design-pattern


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言