設計模式之工廠方法(FACTORY METHOD))(三)
2024-07-21 02:23:36
供稿:網友
(接上頁)
straight seeding類
在這個小程序在實際應用當中,會發現大部分工作在straight seeding類中完成的。我們實例化straightseeding 類,復制、修改游泳運動員的集合和所屬泳道位置。
public class straightseeding
inherits seeding
public overrides sub seed()
dim lastheat as integer
dim lastlanes as integer
dim i, j, count, heats as integer
dim swmr as swimmer
try
sw = sort(sw)
laneorder = calclaneorder(numlanes)
count = sw.count
lastheat = count mod numlanes
if (lastheat < 3) and lastheat > 0 then
lastheat = 3 'last heat must have 3 or more
end if
count = sw.count
lastlanes = count - lastheat
numheats = lastlanes / numlanes
if (lastheat > 0) then
numheats = numheats + 1
end if
heats = numheats
'place heat and lane in each swimmer's object
j = 0
for i = 0 to lastlanes - 1
swmr = sw.swm(i)
swmr.setlane(ctype(laneorder(j), integer))
j = j + 1
swmr.setheat(heats)
if (j >= numlanes) then
heats = heats - 1
j = 0
end if
next i
'add in last partial heat
if (lastheat > 0) then
if j > 0 then
heats = heats - 1
end if
j = 0
for i = lastlanes to count - 1
swmr = ctype(sw(i), swimmer)
swmr.setlane(ctype(laneorder(j), integer))
j = j + 1
swmr.setheat(heats)
next i
end if
catch e as exception
console.writeline(i.tostring + j.tostring + e.tostring)
console.writeline(e.stacktrace)
end try
end sub
'-----
public sub new(byval swmrs as swimmers, byval lanes as integer)
mybase.new(swmrs, lanes)
end sub
end class
當調用getswimmers方法時,straightseeding 類將創建被選拔的游泳運動員數組。
circle seeding類
circleseeding 類是從straightseeding 類派生的。
public class circleseeding
inherits straightseeding
private circlesd as integer
'-----
public sub new(byval swmrs as swimmers, byval lanes as integer)
mybase.new(swmrs, lanes)
end sub
'-----
public overrides sub seed()
dim i, j, k, numheats as integer
laneorder = calclaneorder(numlanes)
sw = sort(sw) '排序
mybase.seed()
numheats = mybase.getheats
if (numheats >= 2) then
if (numheats >= 3) then
circlesd = 3
else
circlesd = 2
end if
i = 0
for j = 0 to numlanes - 1
for k = 1 to circlesd
sw.swm(i).setlane(ctype(laneorder(j), integer))
sw.swm(i).setheat(numheats - k + 1)
i = i + 1
next k
next j
end if
end sub
'-----
end class