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