⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 builder.cs

📁 C#设计模式源码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace DesignDll
{
    /************************************************************************
     * 建造者模式;(Builder Method) 将一个复杂的对象的构建与它的表示分离,使
     * 得同样的构建,可以创建不同的表示
     * **********************************************************************/
    /// <summary>
    /// 产品类
    /// </summary>
    public class Product
    {
        IList<string> lsParts = new List<string>();

        /// <summary>
        /// 添加产品部件 
        /// </summary>
        /// <param name="part"></param>
        public void Add(string part)
        {
            lsParts.Add(part);
        }

        /// <summary>
        /// 显示产品信息
        /// </summary>
        public IList<string> ShowPart()
        {
            return lsParts;
            //foreach (String s in lsParts)
            //{
            //    //显示信息
            //}
        }
    }


    /// <summary>
    /// 创建一个建造者的抽象类
    /// </summary>
    public abstract class Builder
    {
        public abstract void BuilderPartA();
        public abstract void BuilderPartB();
        public abstract Product GetResult();
    }

    /// <summary>
    /// 创建者A
    /// </summary>
    public class BuilderA : Builder
    {
        private Product productA = new Product();

        /// <summary>
        /// 部件X
        /// </summary>
        public override void BuilderPartA()
        {
            productA.Add("部件X");
            //throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// 部件Y
        /// </summary>
        public override void BuilderPartB()
        {
            productA.Add("部件Y");
            //throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// 获取类型 
        /// </summary>
        /// <returns></returns>
        public override Product GetResult()
        {
            return productA;
            //throw new Exception("The method or operation is not implemented.");
        }
    }

    /// <summary>
    /// 创建者B
    /// </summary>
    public class BuilderB : Builder
    {
        private Product productB = new Product();

        /// <summary>
        /// 部件A
        /// </summary>
        public override void BuilderPartA()
        {
            productB.Add("部件A");
            //throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// 部件B
        /// </summary>
        public override void BuilderPartB()
        {
            productB.Add("部件B");
            //throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// 得到结果
        /// </summary>
        public override Product GetResult()
        {
            return productB;
            //throw new Exception("The method or operation is not implemented.");
        }
    }

    /// <summary>
    /// 为了整合前几种方式
    /// </summary>
    public class Director
    {
        public void ConectProduct(Builder builder)
        {
            builder.BuilderPartA();
            builder.BuilderPartB();
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -