编程语言
首页 > 编程语言> > c# 建造者模式

c# 建造者模式

作者:互联网

// See https://aka.ms/new-console-template for more information
/*
 个人理解
获取要组装的东西
叫人去组装
获取组装好的东西展示
 
 
 */
Console.WriteLine("Hello, World!");
XiaoMi xiaoMi = new XiaoMi();//
Builder builder1 = new ConcreteBuilder1();
xiaoMi.ConstructPhone(builder1);
Phone phone = builder1.GetBuilder();
phone.Show();



public class XiaoMi
{
    public void ConstructPhone(Builder builder)
    {
        builder.BuildPartCamera();
        builder.BuildPartScreen();
    }

}
public class Phone
{
    private List<string> list = new();
    public void Add(string part)
    {
        list.Add(part);
    }
    public void Show()
    {
        Console.WriteLine("手机开始在组装.......");
        foreach (string part in list)
        {
            Console.WriteLine("组件" + part + "已装好");
        }

        Console.WriteLine("手机组装好了");
    }
}

public abstract class Builder 
{
    /// <summary>
    /// 摄像头
    /// </summary>
    public abstract void BuildPartCamera();
    /// <summary>
    /// 屏幕
    /// </summary>
    public abstract void BuildPartScreen();

    public abstract Phone GetBuilder();

}

public class ConcreteBuilder1 : Builder
{
    Phone phone = new Phone();
    public override void BuildPartCamera()
    {
        phone.Add("装相机");
    }

    public override void BuildPartScreen()
    {
        phone.Add("装屏幕");
    }

    public override Phone GetBuilder()
    {
        return phone;
    }
}

  

标签:Console,phone,c#,void,建造,模式,Phone,new,public
来源: https://www.cnblogs.com/liujian1368928/p/16670970.html