/// <summary> /// A simple file handlng class /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public class csFile { private string fileName; StreamReader ts; StreamWriter ws; private bool opened, writeOpened; /// <summary> /// /// </summary> public csFile() { init(); } /// <summary> /// /// </summary> private void init() { opened = false; writeOpened = false; } /// <summary> /// /// </summary> /// <param name="file_name"></param> public csFile(string file_name) { fileName = file_name; init(); } /// <summary> /// /// </summary> /// <param name="file_name"></param> /// <returns></returns> public bool OpenForRead(string file_name) { fileName = file_name; try { ts = new StreamReader(fileName); opened = true; } catch (FileNotFoundException) { return false; } return true; } /// <summary> /// /// </summary> /// <returns></returns> public bool OpenForRead() { return OpenForRead(fileName); } /// <summary> /// /// </summary> /// <returns></returns> public string readLine() { return ts.ReadLine(); } /// <summary> /// /// </summary> /// <param name="s"></param> public void writeLine(string s) { ws.WriteLine(s); } /// <summary> /// /// </summary> public void close() { if (opened) ts.Close(); if (writeOpened) ws.Close(); } /// <summary> /// /// </summary> /// <returns></returns> public bool OpenForWrite() { return OpenForWrite(fileName); } /// <summary> /// /// </summary> /// <param name="file_name"></param> /// <returns></returns> public bool OpenForWrite(string file_name) { try { ws = new StreamWriter(file_name); fileName = file_name; writeOpened = true; return true; } catch (FileNotFoundException) { return false; } } }
/// <summary> /// Summary description for LstAdapter. /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public interface LstAdapter { /// <summary> /// /// </summary> /// <param name="sw"></param> void Add(Swimmer sw); /// <summary> /// /// </summary> /// <returns></returns> int SelectedIndex(); /// <summary> /// /// </summary> void Clear(); /// <summary> /// /// </summary> void clearSelection(); }
/// <summary> /// Summary description for Swimmer. /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public class Swimmer : IComparable { private string name; //name private string lname, frname;//split names private int age; //age private string club; //club initials private float time; //time achieved private bool female; //sex /// <summary> /// /// </summary> /// <param name="line"></param> public Swimmer(string line) { StringTokenizer tok = new StringTokenizer(line, ","); splitName(tok); age = Convert.ToInt32(tok.nextToken()); club = tok.nextToken(); time = Convert.ToSingle(tok.nextToken()); string sx = tok.nextToken().ToUpper(); female = sx.Equals("F"); } /// <summary> /// /// </summary> /// <param name="tok"></param> private void splitName(StringTokenizer tok) { name = tok.nextToken(); int i = name.IndexOf(" "); if (i > 0) { frname = name.Substring(0, i); lname = name.Substring(i + 1).Trim(); } } /// <summary> /// /// </summary> /// <param name="swo"></param> /// <returns></returns> public int CompareTo(object swo) { Swimmer sw = (Swimmer)swo; return lname.CompareTo(sw.getLName()); } /// <summary> /// /// </summary> /// <returns></returns> public bool isFemale() { return female; } /// <summary> /// /// </summary> /// <returns></returns> public int getAge() { return age; } /// <summary> /// /// </summary> /// <returns></returns> public float getTime() { return time; } /// <summary> /// /// </summary> /// <returns></returns> public string getName() { return name; } /// <summary> /// /// </summary> /// <returns></returns> public string getClub() { return club; } /// <summary> /// /// </summary> /// <returns></returns> public string getLName() { return lname; } /// <summary> /// /// </summary> /// <returns></returns> public string getFrname() { return frname; } }
/// <summary> /// String Tokenizer class /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public class StringTokenizer { private string data, delimiter; private string[] tokens; private int index; /// <summary> /// /// </summary> /// <param name="dataLine"></param> public StringTokenizer(string dataLine) { init(dataLine, " "); } /// <summary> /// /// </summary> /// <param name="dataLine"></param> /// <param name="delim"></param> private void init(String dataLine, string delim) { delimiter = delim; data = dataLine; tokens = data.Split(delimiter.ToCharArray()); index = 0; } /// <summary> /// /// </summary> /// <param name="dataLine"></param> /// <param name="delim"></param> public StringTokenizer(string dataLine, string delim) { init(dataLine, delim); } /// <summary> /// /// </summary> /// <returns></returns> public bool hasMoreElements() { return (index < (tokens.Length)); } /// <summary> /// /// </summary> /// <returns></returns> public string nextToken() { return nextElement(); } /// <summary> /// /// </summary> /// <returns></returns> public string nextElement() { string s = tokens[index++]; while ((s.Length <= 0) && (index < tokens.Length)) s = tokens[index++]; return s; } }
/// <summary> /// Summary description for SwimData. /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public class SwimData : ICloneable { protected ArrayList swdata; private int index; /// <summary> /// /// </summary> public SwimData() { swdata = new ArrayList(); } /// <summary> /// /// </summary> /// <param name="swd"></param> public SwimData(ArrayList swd) { swdata = swd; index = 0; } /// <summary> /// /// </summary> /// <returns></returns> public int count() { return swdata.Count; } /// <summary> /// /// </summary> /// <param name="filename"></param> public SwimData(string filename) { swdata = new ArrayList(); csFile fl = new csFile(filename); fl.OpenForRead(); string s = fl.readLine(); while (s != null) { Swimmer sw = new Swimmer(s); swdata.Add(sw); s = fl.readLine(); } fl.close(); } /// <summary> /// /// </summary> /// <returns></returns> public object Clone() { SwimData newsd = new SwimData(swdata); return newsd; } /// <summary> /// /// </summary> public void moveFirst() { index = 0; } /// <summary> /// /// </summary> /// <returns></returns> public bool hasMoreElements() { return (index < swdata.Count - 1); } /// <summary> /// /// </summary> public void sort() { //sort using IComparable interface of Swimmer swdata.Sort(0, swdata.Count, null); } /// <summary> /// /// </summary> /// <param name="index"></param> /// <returns></returns> public Swimmer getSwimmer(int index) { if (index < swdata.Count && index >= 0) return (Swimmer)swdata[index++]; else return null; } /// <summary> /// /// </summary> /// <returns></returns> public Swimmer getSwimmer() { if (index < swdata.Count) return (Swimmer)swdata[index++]; else return null; } }
/// <summary> /// Summary description for GridAdapter. /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public class GridAdapter : LstAdapter { private DataGrid grid; private DataTable dTable; /// <summary> /// /// </summary> /// <param name="grd"></param> public GridAdapter(DataGrid grd) { grid = grd; dTable = (DataTable)grid.DataSource; } /// <summary> /// /// </summary> /// <param name="sw"></param> public void Add(Swimmer sw) { DataRow row = dTable.NewRow(); row["Frname"] = sw.getFrname(); row[1] = sw.getLName(); row[2] = sw.getAge(); //This one is an integer dTable.Rows.Add(row); dTable.AcceptChanges(); } /// <summary> /// /// </summary> /// <returns></returns> public int SelectedIndex() { return 0; } /// <summary> /// /// </summary> public void Clear() { } /// <summary> /// /// </summary> public void clearSelection() { } }
/// <summary> /// Summary description for ListAdapter. /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public class ListAdapter : LstAdapter { private ListBox listbox; /// <summary> /// /// </summary> /// <param name="lb"></param> public ListAdapter(ListBox lb) { listbox = lb; } /// <summary> /// /// </summary> /// <param name="s"></param> public void Add(string s) { listbox.Items.Add(s); } /// <summary> /// /// </summary> /// <param name="sw"></param> public void Add(Swimmer sw) { listbox.Items.Add(sw.getName() + "\t" + sw.getTime()); } /// <summary> /// /// </summary> /// <returns></returns> public int SelectedIndex() { return listbox.SelectedIndex; } /// <summary> /// /// </summary> public void Clear() { listbox.Items.Clear(); } /// <summary> /// /// </summary> public void clearSelection() { int i = SelectedIndex(); if (i >= 0) { listbox.SelectedIndex = -1; } } }
/// <summary> /// Summary description for TreeAdapter. /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public class TreeAdapter : LstAdapter { private TreeView tree; /// <summary> /// /// </summary> /// <param name="tr"></param> public TreeAdapter(TreeView tr) { tree = tr; } /// <summary> /// /// </summary> /// <param name="sw"></param> public void Add(Swimmer sw) { TreeNode nod; //add a root node nod = tree.Nodes.Add(sw.getName()); //add a child node to it nod.Nodes.Add(sw.getTime().ToString()); tree.ExpandAll(); } /// <summary> /// /// </summary> /// <returns></returns> public int SelectedIndex() { return tree.SelectedNode.Index; } /// <summary> /// /// </summary> public void Clear() { TreeNode nod; for (int i = 0; i < tree.Nodes.Count; i++) { nod = tree.Nodes[i]; nod.Remove(); } } /// <summary> /// /// </summary> public void clearSelection() { } }
窗体调用测试:
/// <summary> /// geovindu,Geovin Du,涂聚文 /// 适配器模式(Adapter Patterns) /// </summary> public partial class AdapterPatternsForm : Form { private SwimData swdata; private ListAdapter lskids; private TreeAdapter lsNewKids; private DataTable dTable; /// <summary> /// /// </summary> private void init() { swdata = new SwimData("swimmers.txt"); lskids = new ListAdapter(lsKids); lsNewKids = new TreeAdapter(Tree); reload(); } /// <summary> /// /// </summary> private void reload() { lskids.Clear(); swdata.moveFirst(); while (swdata.hasMoreElements()) { Swimmer sw = swdata.getSwimmer(); lskids.Add(sw.getName()); } } /// <summary> /// /// </summary> public AdapterPatternsForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AdapterPatternsForm_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btClone_Click(object sender, EventArgs e) { int i = lskids.SelectedIndex(); if (i >= 0) { Swimmer sw = swdata.getSwimmer(i); lsNewKids.Add(sw); lskids.clearSelection(); } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btBack_Click(object sender, EventArgs e) { reload(); } }
标签:return,string,Adapter,private,Patterns,void,CSharp,swdata,public From: https://www.cnblogs.com/geovindu/p/16709649.html