CSharp: Adapter Patterns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | /// <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 ; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /// <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(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | /// <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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | /// <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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | /// <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 ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | /// <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() { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /// <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; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | /// <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() { } } |
窗体调用测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /// <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(); } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!