鸟语天空
生成器模式(Builder)
post by:追风剑情 2016-6-17 15:50

应用场景:产品对象的构造比较复杂时使用。

using System;
using System.Collections.Generic;
using System.Text;

namespace BuilderTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteBuilder cb = new ConcreteBuilder();
            Product product = cb.BuildProduct();
            Console.WriteLine(product.name);

            Console.Read();
        }
    }

    //定义产品类
    public class Product
    {
        public string name;
    }

    //定义建造者接口
    public interface IBuilder
    {
        Product BuildProduct();
    }

    //具体建造者
    public class ConcreteBuilder : IBuilder
    {
        public Product BuildProduct()
        {
            Product product = new Product();
            product.name = "DefaultName";
            return product;
        }
    }
}

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容