zoukankan      html  css  js  c++  java
  • 设计模式之建造者模式代码示例

    using System;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace BuilderFunc{
    
        public class Product{
    
            List<string> arrList = new List<string>();
            // First in First Out (FIFO) 
            Queue  Q = new Queue(); 
            
            // Last in First out (LIFO) 
            Stack S = new Stack();
            public void Add(string param)
            {
                arrList.Add(param);
            }
    
            public void AddEvent(EventArgs e)
            {
                Q.Enqueue(e);
            }
            
            public void Show() { 
                Console.WriteLine("\nProduct Param -------"); 
                foreach (string part in arrList) 
                    Console.WriteLine(part); 
                    
                foreach (EventArgs e in Q) 
                    Console.WriteLine(e); 
            }
        }
    
        public abstract class AbstractBuilder{
            public abstract void BuilderProductA();
            public abstract void BuilderProductB();
            public abstract Product GetResult();
        }
    
        public class ConcreteBuilder1 : AbstractBuilder{
    
            private Product product = new Product();
    
            public override void BuilderProductA(){
                product.Add(this.ToString() + " : BuilderProductA");
            }
            public override void BuilderProductB(){
                product.Add(this.ToString() + " : BuilderProductB");
            }
            public override Product GetResult(){
                return product;
            }
        }
    
        public class Director{
            public static void CreateBuilder(AbstractBuilder abstractBuilder)
            {
                abstractBuilder.BuilderProductA();
                abstractBuilder.BuilderProductB();
            }
        }
    
        public class EntryPoint{
            public static void Main(string[] args){
                AbstractBuilder absBuilder = new ConcreteBuilder1();
                Director.CreateBuilder(absBuilder);
                Product p1 = absBuilder.GetResult();
                p1.Show();
                Console.Read();
            }
        }
    }
  • 相关阅读:
    jqGrid api 中文说明
    jsp + js + 前端弹出框
    js中关于带数字类型参数传参丢失首位数字0问题
    java中WGS84坐标(ios)转换BD-09坐标(百度坐标)
    Java中的“浅复制”与“深复制”
    Git错误:error:failed to push some refs to 'git@gitee.com:name/project.git'
    git操作教程
    线程调度及进程调度
    同步锁Lock
    多线程案例
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2651710.html
Copyright © 2011-2022 走看看