設計模式之Factory 方法(FACTORY METHOD))(三)

來源:互聯網
上載者:User
設計 (接上頁)

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





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。