首页 > 其他分享 >Csharp: Builder Patterns

Csharp: Builder Patterns

时间:2022-09-18 11:24:11浏览次数:68  
标签:Builder list Patterns Add ArrayList Csharp new array public

 

/// <summary>
    /// Summary description for Equities.
    /// geovindu,Geovin Du,涂聚文
    /// 生成器(建造)模式(Builder Patterns)
    /// </summary>
    public abstract class Equities
    {
        protected ArrayList array;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public abstract string ToString();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ArrayList getNames()
        {
            return array;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
	
        public int count()
        {
            return array.Count;
        }
    }

  

  /// <summary>
    /// Summary description for Stocks.
    /// geovindu,Geovin Du,涂聚文
    /// 生成器(建造)模式(Builder Patterns)
    /// </summary>
    public class Stocks : Equities
    {

        /// <summary>
        /// 
        /// </summary>
        public Stocks()
        {
            array = new ArrayList();
            array.Add("Cisco");
            array.Add("Coca Cola");
            array.Add("GE");
            array.Add("Harley Davidson");
            array.Add("IBM");
            array.Add("Microsoft");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "Stocks";
        }
    }

  

/// <summary>
    /// Summary description for Bonds.
    /// geovindu,Geovin Du,涂聚文
    /// 生成器(建造)模式(Builder Patterns)
    /// </summary>
    public class Bonds : Equities
    {

        /// <summary>
        /// 
        /// </summary>
        public Bonds()
        {
            array = new ArrayList();
            array.Add("CT GO 2005");
            array.Add("NY GO 2012");
            array.Add("GE Corp Bonds");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "Bonds";
        }
    }

  

/// <summary>
    /// Summary description for Mutuals.
    /// geovindu,Geovin Du,涂聚文
    /// 生成器(建造)模式(Builder Patterns)
    /// </summary>
    public class Mutuals : Equities
    {

        /// <summary>
        /// 
        /// </summary>
        public Mutuals()
        {
            array = new ArrayList();
            array.Add("Fidelity Magellan");
            array.Add("Lindner");
            array.Add("T Rowe Price");
            array.Add("Vanguard Primecap");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "Mutuals";
        }
    }

  

 /// <summary>
    /// Summary description for MultiChoice.
    /// geovindu,Geovin Du,涂聚文
    /// 生成器(建造)模式(Builder Patterns)
    /// </summary>
    public interface MultiChoice
    {
        ArrayList getSelected();
        void clear();
        Panel getWindow();
    }

  

/// <summary>
    /// 
    /// geovindu,Geovin Du,涂聚文
    /// 生成器(建造)模式(Builder Patterns)
    /// </summary>
    public class CheckChoice : MultiChoice
    {
        private ArrayList stocks;
        private Panel panel;
        private ArrayList boxes;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stks"></param>
        public CheckChoice(Equities stks)
        {
            stocks = stks.getNames();
            panel = new Panel();
            boxes = new ArrayList();
            //add the check boxes to the panel
            for (int i = 0; i < stocks.Count; i++)
            {
                CheckBox ck = new CheckBox();
                //position them
                ck.Location = new Point(8, 16 + i * 32);
                string stk = (string)stocks[i];
                ck.Text = stk;
                ck.Size = new Size(112, 24);
                ck.TabIndex = 0;
                ck.TextAlign = ContentAlignment.MiddleLeft;
                boxes.Add(ck);
                panel.Controls.Add(ck);
            }
        }
   
        /// <summary>
        /// uncheck all check boxes
        /// </summary>
        public void clear()
        {
            for (int i = 0; i < boxes.Count; i++)
            {
                CheckBox ck = (CheckBox)boxes[i];
                ck.Checked = false;
            }
        }
    
        /// <summary>
        /// return list of checked items
        /// </summary>
        /// <returns></returns>
        public ArrayList getSelected()
        {
            ArrayList sels = new ArrayList();
            for (int i = 0; i < boxes.Count; i++)
            {
                CheckBox ck = (CheckBox)boxes[i];
                if (ck.Checked)
                {
                    sels.Add(ck.Text);
                }
            }
            return sels;
        }
    
        /// <summary>
        /// return panel of checkboxes
        /// </summary>
        /// <returns></returns>
        public Panel getWindow()
        {
            return panel;
        }
    }

  

 /// <summary>
    /// Summary description for ListChoice.
    /// geovindu,Geovin Du,涂聚文
    /// 生成器(建造)模式(Builder Patterns)
    /// </summary>
    /// creates a Panel containing a list box
    public class ListChoice : MultiChoice
    {
        private ArrayList stocks;
        private Panel panel;
        private ListBox list;
    

        /// <summary>
        /// constructor creates and loads the list box
        /// </summary>
        /// <param name="stks"></param>
        public ListChoice(Equities stks)
        {
            stocks = stks.getNames();
            panel = new Panel();
            list = new ListBox();
            list.Location = new Point(16, 0);
            list.Size = new Size(120, 160);
            list.SelectionMode = SelectionMode.MultiExtended;
            list.TabIndex = 0;
            panel.Controls.Add(list);
            for (int i = 0; i < stocks.Count; i++)
            {
                list.Items.Add(stocks[i]);
            }
        }
        
        /// <summary>
        /// returns the Panel
        /// </summary>
        /// <returns></returns>
        public Panel getWindow()
        {
            return panel;
        }
      
        /// <summary>
        /// returns an array of selected elements
        /// </summary>
        /// <returns></returns>
        public ArrayList getSelected()
        {
            ArrayList sels = new ArrayList();
            ListBox.SelectedObjectCollection coll = list.SelectedItems;
            for (int i = 0; i < coll.Count; i++)
            {
                string item = (string)coll[i];
                sels.Add(item);
            }
            return sels;
        }
     
        /// <summary>
        /// clear selected elements
        /// </summary>
        public void clear()
        {
            list.Items.Clear();
        }
    }

  

 /// <summary>
    /// Summary description for StockFactory.
    /// </summary>
    public class StockFactory
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stocks"></param>
        /// <returns></returns>
        public static MultiChoice getBuilder(Equities stocks)
        {
            if (stocks.count() <= 3)
            {
                return new CheckChoice(stocks);
            }
            else
            {
                return new ListChoice(stocks);
            }
        }
    }

  

窗体调用测试:

    /// <summary>
    /// 建造者模式(Builder Pattern) 
    /// Geovin Du 涂聚文
    /// </summary>
    public partial class BuilderPatternsForm : Form
    {
        private MultiChoice mchoice;


        /// <summary>
        /// 
        /// </summary>
        private void init()
        {
            lsEquities.Items.Add(new Stocks());
            lsEquities.Items.Add(new Bonds());
            lsEquities.Items.Add(new Mutuals());
        }
        /// <summary>
        /// 
        /// </summary>
        public BuilderPatternsForm()
        {
            InitializeComponent();
            init();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BuilderPatternsForm_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        private void setPanel()
        {
            pnl.Location = new Point(152, 24);
            pnl.Size = new Size(128, 168);
            pnl.TabIndex = 1;
            Controls.Add(pnl);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lsEquities_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = lsEquities.SelectedIndex;
            Equities eq = (Equities)lsEquities.Items[i];
            mchoice = StockFactory.getBuilder(eq);
            this.Controls.Remove(pnl);
            pnl = mchoice.getWindow();
            setPanel();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btPlot_Click(object sender, EventArgs e)
        {
            //display the selected items in a message box
            if (mchoice != null)
            {
                ArrayList ar = mchoice.getSelected();
                string ans = "";
                for (int i = 0; i < ar.Count; i++)
                {
                    ans += (string)ar[i] + " ";
                }
                MessageBox.Show(null, ans, "Selected equities", MessageBoxButtons.OK);
            }
        }
    }

  

输出:

 

标签:Builder,list,Patterns,Add,ArrayList,Csharp,new,array,public
From: https://www.cnblogs.com/geovindu/p/16704451.html

相关文章

  • The following method did not exist: com.google.gson.GsonBuilder.setLenient()
    报错完整信息  原因:因为引入的jar包版本不对,里面没有com.google.gson.GsonBuilder.setLenient()这个方法。解决方案:1、引入版本对应的jar包2、排除springBoot中......
  • CBuilder中的第七个案例
    myEXDC1:逻辑控制框图myEXDC1是包含**static、RAM_FUNCTIONS:、RAM****CODE**四部分代码的封装模式VERSION:3.001include"myEXDC1.h"STATIC:doubledt,washoutG,......
  • 2022最全Hbuilder打包成苹果IOS-App的详解
     本文相关主要记录一下使用Hbuilder打包成苹果IOS-App的详细步骤。介绍一下个人开发者账号:再说下什么是免费的苹果开发者账号,就是你没交688年费的就是免费账号,如果你......
  • alibaba的csb使用HttpParameters.Builder 发送请求时通过NGINX时,一些参数消失
    问题描述当使用csb的HttpParameters.Builder的requestURL方法调用第三方api时,本地运行成功调用,但是当部署在服务器A并通过另一台服务器B的ng转发调用失败。代码......
  • 用 CSharpCompilation 进行动态编译
    项目里需要用到动态编译。网上一大片的介绍C#动态编译的,是CodeDomProvider,这个东西确实好用。但是说好了支持.net6.0 的,但运行时却说不支持当前平台。骗子!网上找的......
  • HBuilder包装iOS APP上App Store的详细过程
    以前看过很多教程,然后。ITUNE管理APP功能被删除,IPA安装失败。Appuploader已有1个月的期限等问题。请参见此教程重新安装。因此,放出。很多新生开发,不知道如何包装。iosAP......
  • java Builder模式
    Builder 模式也叫建造者模式,builder模式的作用将一个复杂对象的构建与他的表示分离,一步一步创建一个复杂对象的创建型模式。在不知道内部建造细节的情况下,可以更精细的控......
  • 06 | JAVA的StringBuilder高效拼接字符串
    StringBuilder如果用+来拼接字符串速度满。我们可以提前用StringBuilder来申请一大块的内存。把他想象成一个容器。为了能高效拼接字符串,Java标准库提供了StringBuil......
  • [CSharpTips]C# 将DataTable转换为类
    将DataTable转换为类众所周知,有时候我们需要将sql查询返回的DataTable转换为类。最开始是使用循环一个个给类的属性赋值,但是这样效率低并且无法复用。后来了解到利用Data......
  • [CSharpTips]C# 读写INI文件
    读写.ini文件工具可以直接使用usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Text;names......