Csharp:Factory Method Pattern
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 | /// <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); } } |
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 | /// <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; } } |
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 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(); } |
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 | /// <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; } } |
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 117 | /// <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; } } |
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 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 ; } } |
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 | /// <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); } } } } } |
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,涂聚文 /// 工厂方法模式(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 ; } } } |
調體調用:
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> /// 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()); } } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2012-09-17 SQL 生成公曆和農曆對照數據,公曆查找農曆和農曆查找公曆函數