Csharp:Factory Method Pattern

  /// <summary>
	/// Summary description for Seeding.
    /// 工厂方法模式(Factory Method Patterns)
	/// </summary>
	public abstract class Seeding 	{
		protected int       numLanes;                      
		protected int[]     lanes;
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
		public abstract IEnumerator getSwimmers();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
		public abstract int getCount();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
		public abstract int getHeats();
        /// <summary>
        /// 
        /// </summary>
		protected abstract void seed();
		/// <summary>
		/// 
		/// </summary>
		protected void calcLaneOrder() {
			lanes = new int[numLanes];
			int mid = numLanes / 2;
			if (odd(numLanes))
				mid = mid + 1;       //start in middle lane
			int incr = 1;
			int ln = mid;
			//create array of lanes from
			//center to outside
			for (int i=0; i< numLanes; i++) {
				lanes[i] = ln;
				ln = mid + incr;
				incr = - incr;
				if (incr > 0)
					incr=incr+1;
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="x"></param>
		/// <returns></returns>
		private bool odd(int x) {
			return(((x / 2)*2) != x);
		}
	}

  

 /// <summary>
    /// Summary description for Swimmer.
    /// Summary description for PrelimEvent.
    /// geovindu,Geovin Du,涂聚文
    /// 工厂方法模式(Factory Method Patterns)
    /// </summary>
    public class Swimmer
    {
        private string firstName, lastName;
        private int age;
        private string club;
        private float time;

        private int heat, lane;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataline"></param>
        public Swimmer(String dataline)
        {
            StringTokenizer st = new StringTokenizer(dataline, " ");
            string lineNumber = st.nextToken();    //ignore and discard
            firstName = st.nextToken();
            lastName = st.nextToken();
            age = Convert.ToInt32(st.nextToken().Trim());
            club = st.nextToken().Trim();

            string stime = st.nextToken().Trim();
            int i = stime.IndexOf(":");
            if (i > 0)
            {
                stime = stime.Substring(0, i) + stime.Substring(i + 1);
            }
            time = Convert.ToSingle(stime);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ln"></param>
        public void setLane(int ln)
        {
            lane = ln;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int getLane()
        {
            return lane;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ht"></param>
        public void setHeat(int ht)
        {
            heat = ht;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int getHeat()
        {
            return heat;
        }
        /// <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 firstName + " " + lastName;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string getClub()
        {
            return club;
        }

    }

  

  /// <summary>
    /// Summary description for Event.
    /// geovindu,Geovin Du,涂聚文
    /// 工厂方法模式(Factory Method Patterns)
    /// </summary>
    public abstract class Event
    {
        protected int numLanes;
        protected ArrayList swimmers;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="lanes"></param>
        public Event(string filename, int lanes)
        {
            numLanes = lanes;
            swimmers = new ArrayList();
            //read in swimmers from file
            csFile f = new csFile(filename);
            f.OpenForRead();
            string s = f.readLine();
            while (s != null)
            {
                Swimmer sw = new Swimmer(s);
                swimmers.Add(sw);
                s = f.readLine();
            }
            f.close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public abstract Seeding getSeeding();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public abstract bool isPrelim();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public abstract bool isFinal();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public abstract bool isTimedFinal();



    }

  

/// <summary>
    /// 
    /// geovindu,Geovin Du,涂聚文
    /// 工厂方法模式(Factory Method 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 StraightSeeding.
    /// Summary description for PrelimEvent.
    /// geovindu,Geovin Du,涂聚文
    /// 工厂方法模式(Factory Method Patterns)
    /// </summary>
    public class StraightSeeding : Seeding
    {
        protected ArrayList swimmers;
        protected Swimmer[] swmrs;
        protected int count;
        protected int numHeats;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sw"></param>
        /// <param name="lanes"></param>
        public StraightSeeding(ArrayList sw, int lanes)
        {
            swimmers = sw;
            numLanes = lanes;
            count = sw.Count;
            calcLaneOrder();
            seed();
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void seed()
        {
            //loads the swmrs array and sorts it
            sortUpwards();

            int lastHeat = count % numLanes;
            if (lastHeat < 3)
                lastHeat = 3;   //last heat must have 3 or more
            int lastLanes = count - lastHeat;
            numHeats = count / numLanes;
            if (lastLanes > 0)
                numHeats++;
            int heats = numHeats;

            //place heat and lane in each swimmer's object
            int j = 0;
            for (int i = 0; i < lastLanes; i++)
            {
                Swimmer sw = swmrs[i];
                sw.setLane(lanes[j++]);
                sw.setHeat(heats);
                if (j >= numLanes)
                {
                    heats--;
                    j = 0;
                }
            }
            //Add in last partial heat
            if (j < numLanes)
                heats--;
            j = 0;
            for (int i = lastLanes - 1; i < count; i++)
            {
                Swimmer sw = swmrs[i];
                sw.setLane(lanes[j++]);
                sw.setHeat(heats);
            }
            //copy from array back into ArrayList
            swimmers = new ArrayList();
            for (int i = 0; i < count; i++)
                swimmers.Add(swmrs[i]);
        }
        /// <summary>
        /// 
        /// </summary>
        protected void sortUpwards()
        {
            swmrs = new Swimmer[count];
            for (int i = 0; i < count; i++)
                swmrs[i] = (Swimmer)swimmers[i];
            for (int i = 0; i < count; i++)
            {
                for (int j = i; j < count; j++)
                {
                    if (swmrs[i].getTime() > swmrs[j].getTime())
                    {
                        Swimmer swtemp = swmrs[i];
                        swmrs[i] = swmrs[j];
                        swmrs[j] = swtemp;
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int getCount()
        {
            return swimmers.Count;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override IEnumerator getSwimmers()
        {
            return swimmers.GetEnumerator();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int getHeats()
        {
            return numHeats;
        }

    }

  

 /// <summary>
    /// Summary description for PrelimEvent.
    /// geovindu,Geovin Du,涂聚文
    /// 工厂方法模式(Factory Method Patterns)
    /// </summary>
    public class PrelimEvent : Event
    {


        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="lanes"></param>
        public PrelimEvent(string filename, int lanes)
            : base(filename, lanes)
        {
        }
        /// <summary>
        /// return circle seeding
        /// </summary>
        /// <returns></returns>
        public override Seeding getSeeding()
        {
            return new CircleSeeding(swimmers, numLanes);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override bool isPrelim()
        {
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override bool isFinal()
        {
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override bool isTimedFinal()
        {
            return false;
        }
    }

  

 /// <summary>
    /// Summary description for CircleSeeding.
    /// geovindu,Geovin Du,涂聚文
    /// 工厂方法模式(Factory Method Patterns)
    /// </summary>
    public class CircleSeeding : StraightSeeding
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sw"></param>
        /// <param name="lanes"></param>
        public CircleSeeding(ArrayList sw, int lanes)
            : base(sw, lanes)
        {
            seed();
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void seed()
        {
            int circle;

            base.seed();        //do straight seed as default
            if (numHeats >= 2)
            {
                if (numHeats >= 3)
                    circle = 3;
                else
                    circle = 2;
                int i = 0;
                for (int j = 0; j < numLanes; j++)
                {
                    for (int k = 0; k < circle; k++)
                    {
                        swmrs[i].setLane(lanes[j]);
                        swmrs[i++].setHeat(numHeats - k);
                    }
                }
            }
        }

    }

  

 /// <summary>
    /// A simple file handlng class
    /// geovindu,Geovin Du,涂聚文
    /// 工厂方法模式(Factory Method 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)
            {
                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 e)
            {
                return false;
            }
        }
    }

  

調體調用:

 /// <summary>
    /// 2022-09-17
    /// geovindu,Geovin Du,涂聚文
    /// 工厂方法模式(Factory Method Patterns)
    /// </summary>
    public partial class FactoryMethodForm : Form
    {


        private ArrayList events;
        /// <summary>
        /// 
        /// </summary>
        private void init()
        {
            //create array of events
            events = new ArrayList();
            lsEvents.Items.Add("500 Free");
            lsEvents.Items.Add("100 Free");
            //and read in their data
            events.Add(new TimedFinalEvent("500free.txt", 6));
            events.Add(new PrelimEvent("100free.txt", 6));
        }
        /// <summary>
        /// 
        /// </summary>
        public FactoryMethodForm()
        {
            InitializeComponent();
            init();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FactoryMethodForm_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lsEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = lsEvents.SelectedIndex;
            Event ev = (Event)events[index];
            Seeding sd = ev.getSeeding();
            IEnumerator en = sd.getSwimmers();
            lsSwimmers.Items.Clear();
            while (en.MoveNext())
            {
                Swimmer sw = (Swimmer)en.Current;
                lsSwimmers.Items.Add(sw.getHeat() + " " + sw.getLane() + " " + sw.getName() + " " + sw.getTime());
            }
        }
    }

  

 

posted @ 2022-09-17 23:10  ®Geovin Du Dream Park™  阅读(19)  评论(0编辑  收藏  举报