• 设计模式:建造者模式


    建造者模式(Builder):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

    namespace Builder
    {
        public class Product
        {
            IList<string> parts = new List<string>();
            public void Add(string part)
            {
                parts.Add(part);
            }
            public void Show()
            {
                Console.WriteLine("
    产品 创建 ---");
                foreach(string part in parts)
                {
                    Console.WriteLine(part);
                }
            }
        }
        public abstract class Builder
        {
            public abstract void BuildPartA();
            public abstract void BuildPartB();
            public abstract Product GetResult();
        }
        public class ConcreteBuilder1 : Builder
        {
            private Product product = new Product();
            public override void BuildPartA()
            {
                product.Add("部件A");
            }
    
            public override void BuildPartB()
            {
                product.Add("部件B");
            }
    
            public override Product GetResult()
            {
                return product;
            }
        }
        public class ConcreteBuilder2 : Builder
        {
            private Product product = new Product();
            public override void BuildPartA()
            {
                product.Add("部件X");
            }
    
            public override void BuildPartB()
            {
                product.Add("部件Y");
            }
    
            public override Product GetResult()
            {
                return product;
            }
        }
        public class Director
        {
            public void Construct(Builder builder)
            {
                builder.BuildPartA();
                builder.BuildPartB();
            }
        }
    }
    View Code

    测试代码:

                Builder.Director director = new Builder.Director();
                Builder.Builder b1 = new Builder.ConcreteBuilder1();
                Builder.Builder b2 = new Builder.ConcreteBuilder2();
                director.Construct(b1);
                Builder.Product p1 = b1.GetResult();
                p1.Show();
                director.Construct(b2);
                Builder.Product p2 = b2.GetResult();
                p2.Show();
    View Code
  • 相关阅读:
    A1132 Cut Integer (20分)
    A1131 Subway Map (30分)
    A1130 Infix Expression (25分)
    A1129 Recommendation System (25分)
    A1128 N Queens Puzzle (20分)
    arm指令集
    韦东山视频地址
    汇编知识
    emacs 使用教程
    ip
  • 原文地址:https://www.cnblogs.com/uptothesky/p/5257524.html
Copyright © 2020-2023  润新知