首页 > 其他分享 >CSharp: Adapter Patterns

CSharp: Adapter Patterns

时间:2022-09-20 00:22:44浏览次数:59  
标签:return string Adapter private Patterns void CSharp swdata public

 

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

相关文章

  • java: Facade Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*门面模式FacadePatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-12......
  • java: Flyweight Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*享元模式FlyweightPatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-1......
  • Csharp: Singleton Patterns
     ///<summary>///SummarydescriptionforSpooler.///geovindu,GeovinDu,涂聚文///单例模式(SingletonPatterns)///</summary>pu......
  • Csharp: Prototype Patterns
     ///<summary>///SummarydescriptionforSwimmer///geovindu,GeovinDu,涂聚文///原型模式(PrototypePatterns)///</summary>public......
  • dependency-injection-principles-practices-and-patterns 阅读记录
    最近断断续续看了一下dependency-injection-principles-practices-and-patterns这本书,随便记录了一下思维导图,结合目前的经验还是感觉到收获不少。......
  • Csharp: Builder Patterns
     ///<summary>///SummarydescriptionforEquities.///geovindu,GeovinDu,涂聚文///生成器(建造)模式(BuilderPatterns)///</summary>......
  • 用 CSharpCompilation 进行动态编译
    项目里需要用到动态编译。网上一大片的介绍C#动态编译的,是CodeDomProvider,这个东西确实好用。但是说好了支持.net6.0 的,但运行时却说不支持当前平台。骗子!网上找的......
  • [CSharpTips]C# 将DataTable转换为类
    将DataTable转换为类众所周知,有时候我们需要将sql查询返回的DataTable转换为类。最开始是使用循环一个个给类的属性赋值,但是这样效率低并且无法复用。后来了解到利用Data......
  • [CSharpTips]C# 读写INI文件
    读写.ini文件工具可以直接使用usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Text;names......
  • Spring MVC组件之HandlerAdapter
    SpringMVC组件之HandlerAdapterHandlerAdapter概述HandlerAdapter组件是一个处理器Handler的适配器。HandlerAdapter组件的主要作用是适配特定的Handler来处理相应的请......