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

Csharp: Prototype Patterns

时间:2022-09-18 14:33:58浏览次数:86  
标签:return string Swimmer private Patterns Csharp swdata Prototype public

 

 /// <summary>
    /// Summary description for Swimmer
    /// geovindu,Geovin Du,涂聚文
    /// 原型模式(Prototype 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>
        public Swimmer() { }
        /// <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().Trim();
            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>
    /// A simple file handlng class
    /// geovindu,Geovin Du,涂聚文
    /// 原型模式(Prototype 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 e)
            {
                Console.WriteLine(e.Message);
                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>
    /// String Tokenizer class
    /// geovindu,Geovin Du,涂聚文
    /// 原型模式(Prototype 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,涂聚文
    /// 原型模式(Prototype Patterns)
    /// </summary>
    public class SwimData
    {
        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="swdat"></param>
        public virtual void cloneMe(SwimData swdat)
        {
            swdata = new ArrayList();
            ArrayList swd = swdat.getData();
            //copy in swimmer objects
            for (int i = 0; i < swd.Count; i++)
                swdata.Add(swd[i]);

        }
        /// <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 ArrayList getData()
        {
            return swdata;
        }
        /// <summary>
        /// 
        /// </summary>
        public void moveFirst()
        {
            index = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool hasMoreElements()
        {
            return (index < swdata.Count);
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual void sort()
        {
            //sort using IComparable interface of Swimmer
            swdata.Sort(0, swdata.Count, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Swimmer getSwimmer()
        {
            if (index < swdata.Count)
                return (Swimmer)swdata[index++];
            else
                return null;
        }
    }

  

  /// <summary>
    /// Summary description for SexSwimData.
    /// geovindu,Geovin Du,涂聚文
    /// 原型模式(Prototype Patterns)
    /// </summary>
    public class AgeSwimData : SwimData
    {
        ArrayList swd;
        public AgeSwimData()
        {
            swdata = new ArrayList();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        public AgeSwimData(string filename) : base(filename) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ssd"></param>
        public AgeSwimData(ArrayList ssd) : base(ssd) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="swdat"></param>
        public override void cloneMe(SwimData swdat)
        {
            swd = swdat.getData();
        }
        /// <summary>
        /// 
        /// </summary>
        public override void sort()
        {
            Swimmer[] sws = new Swimmer[swd.Count];
            //copy in swimmer objects
            for (int i = 0; i < swd.Count; i++)
            {
                sws[i] = (Swimmer)swd[i];
            }
            //sort into increasing order
            for (int i = 0; i < sws.Length; i++)
            {
                for (int j = i; j < sws.Length; j++)
                {
                    if (sws[i].getAge() > sws[j].getAge())
                    {
                        Swimmer sw = sws[i];
                        sws[i] = sws[j];
                        sws[j] = sw;
                    }
                }
            }
            int age = sws[0].getAge();
            int agecount = 0;
            int k = 0;
            swdata = new ArrayList();
            bool quit = false;

            while (k < sws.Length && !quit)
            {
                while (sws[k].getAge() == age && !quit)
                {
                    agecount++;
                    if (k < sws.Length - 1)
                        k++;
                    else
                        quit = true;
                }
                //create a new Swimmer with a series of X's for a name
                //for each new age
                string name = "";
                for (int j = 0; j < agecount; j++)
                    name += "X";
                Swimmer sw = new Swimmer(age.ToString() + " " +
                    name + "," + age.ToString() + ",club,0,F");
                swdata.Add(sw);
                agecount = 0;
                if (quit)
                    age = 0;
                else
                    age = sws[k].getAge();
            }

        }
    }

  

窗体调用:

    /// <summary>
    /// geovindu,Geovin Du,涂聚文
    /// 原型模式(Prototype Patterns)
    /// </summary>
    public partial class PrototypePatternsForm : Form
    {
        private SwimData swdata;
        /// <summary>
        /// 
        /// </summary>
        private void init()
        {
            swdata = new AgeSwimData("swimmers.txt");
            reload();
        }
        /// <summary>
        /// 
        /// </summary>
        private void reload()
        {
            lsKids.Items.Clear();
            swdata.moveFirst();
            while (swdata.hasMoreElements())
            {
                Swimmer sw = swdata.getSwimmer();
                lsKids.Items.Add(sw.getName());
            }

        }
        /// <summary>
        /// 
        /// </summary>
        public PrototypePatternsForm()
        {
            InitializeComponent();
            init();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrototypePatternsForm_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btClone_Click(object sender, EventArgs e)
        {
            AgeSwimData newSd = new AgeSwimData();
            newSd.cloneMe(swdata);
            newSd.sort();
            lsNewKids.Items.Clear();
            while (newSd.hasMoreElements())
            {
                Swimmer sw = (Swimmer)newSd.getSwimmer();
                lsNewKids.Items.Add(sw.getName());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btBack_Click(object sender, EventArgs e)
        {
            reload();
        }


    }

  

输出:

 

标签:return,string,Swimmer,private,Patterns,Csharp,swdata,Prototype,public
From: https://www.cnblogs.com/geovindu/p/16704761.html

相关文章

  • dependency-injection-principles-practices-and-patterns 阅读记录
    最近断断续续看了一下dependency-injection-principles-practices-and-patterns这本书,随便记录了一下思维导图,结合目前的经验还是感觉到收获不少。......
  • Csharp: Builder Patterns
     ///<summary>///SummarydescriptionforEquities.///geovindu,GeovinDu,涂聚文///生成器(建造)模式(BuilderPatterns)///</summary>......
  • js判断变量数据类型typeof、instanceof、Object.prototype.toString.call()、 constru
    JavaScript有4种方法判断变量的类型,分别是typeof、instanceof、Object.prototype.toString.call()(对象原型链判断方法)、constructor(用于引用数据类型) typeof:常用于......
  • 终止 Array.prototype.forEach 方法运行的方式
    通常情况下,Array.prototype上的遍历方法forEach、map、filter、...被调用以后会完整的遍历每一个数组项,并执行内部代码指令,无法被中途终止。但是可以通过 throw语句......
  • 用 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......
  • 如果给Array.prototype.fill()方法传入1个引用类型的填充对象
    先来看下fill方法的介绍Array.prototype.fill()fill()方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素(不包括终止索引)。 再来看下该方法的入参细节......
  • Vue.prototype、Vue.component和Vue.use的区别
    1、Vue.prototype在很多组件里用到数据/实用工具,但是不想污染全局作用域。这种情况下,可以通过在原型上定义它们使其在每个Vue的实例中可用。$是在Vue所有实例中都可......
  • [Javascript] Prototype Pattern
    Source:https://javascriptpatterns.vercel.app/patterns/design-patterns/prototype-pattern Ifyouusefactorypatterntocreateobject:constcreateDog=(nam......