Net Core教程

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;
    }
}

  

这篇关于c# 建造者模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!