/// <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