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

來源:互聯網
上載者:User
設計 Factory 方法

THE FACTORY METHOD

我們已經學會了簡單原廠模式(Simple Factory Pattern),工廠(factory)思想貫穿於整個物件導向編程(OOP)以及其他一些設計模式的始終。如:產生器(Builder)模式。其間,一個單一的類扮演類似交通警察的角色,決定哪一個單一層次上的子類將被執行個體化。

Factory 方法模式(Factory Method pattern)是對工廠(factory)思想進行了巧妙的延伸,它使得將超類的執行個體化延遲到它的每一個子類。這個模式沒有具體的指出延遲到哪一個子類,而是定義一個抽象類別建立對象,讓其子類決定建立哪一個對象。

下面是一個簡單的例子,在一個遊泳比賽中如何確定遊泳運動員的泳道。按照運動員的成績排列決賽事的分組,速度越快所分的小組的決賽的次序越靠後,反之,速度越慢就越先比賽,而且在每一個小組中成績越好、速度越快的選手也就越靠近中間的泳道。這被稱為straight seeding。

當遊泳運動員在錦標賽比賽過程中,他們通常要遊兩次。 通過在預賽中相互競爭,前12名或者16名遊泳運動員將繼續在決賽中繼續彼此競爭。 為了預賽工作更公平, 採用circle seeded,這樣使得速度最快的3名選手分別處於最迅速3個小組的中心泳道。在剩下的選手中再選出速度最好的3名選手,等等。

我們要實現這個選拔模式並且使用Factory 方法。

首先,設計抽象事件類別:

Public Class Events

Protected numLanes As Integer

Protected swmmers As Swimmers

'-----

Public Sub New(ByVal Filename As String, ByVal lanes As Integer)

MyBase.New()

Dim s As String

Dim sw As Swimmer

Dim fl As vbFile

fl = New vbFile(Filename) '開啟一個文字檔

fl.OpenForRead()

numLanes = lanes '儲存泳道數量資訊

swmmers = New Swimmers

'讀取遊泳選手資訊

s = fl.readLine

While Not fl.fEOF

sw = New Swimmer(s) '建立對象

swmmers.Add(sw) 'add to list

s = fl.readLine

End While

fl.closeFile()

End Sub

'-----

Public Function getSwimmers() As ArrayList

getSwimmers = swmmers

End Function

'-----

Public Overridable Function isPrelim() As Boolean

End Function

'-----

Public Overridable Function isFinal() As Boolean

End Function

'-----

Public Overridable Function isTimedFinal() As Boolean

End Function

'-----

Public Overridable Function getSeeding() As Seeding

End Function



End Class



因為所有的衍生類別都要從文字檔讀取資料,所以,我們把Events類作為基類。其中所定義的方法均為虛方法,可以通過繼承Events類來實現具體的類(PrelimEvent類、TimedFinalEvent類),這兩個類之間唯一的不同就是返回的選拔的類別不同。我們也定義了一個包含以下方法的抽象選拔類:



Public MustInherit Class Seeding

Protected numLanes As Integer

Protected laneOrder As ArrayList

Protected numHeats As Integer

Private asw() As Swimmer

Protected sw As Swimmers

'-----

Public Function getSeeding() As Swimmers

getSeeding = sw

End Function

'-----

Public Function getHeat() As Integer

End Function

'-----

Public Function getCount() As Integer

getCount = sw.Count

End Function

'-----

Public MustOverride Sub seed()

'-----

Public Function getSwimmers() As ArrayList

getSwimmers = sw

End Function

'-----

Public Function getHeats() As Integer

Return numHeats

End Function

'-----

Public Function odd(ByVal n As Integer) As Boolean

odd = (n \ 2) * 2 <> n

End Function

'-----

Public Function calcLaneOrder(ByVal lns As Integer) As ArrayList

numLanes = lns

Dim lanes(numLanes) As Integer

Dim i As Integer

Dim mid, incr, ln As Integer

mid = (numLanes \ 2)

If (odd(numLanes)) Then

mid = mid + 1

End If

incr = 1

ln = mid

For i = 0 To numLanes - 1

lanes(i) = ln

ln = mid + incr

incr = -incr

If (incr > 0) Then

incr = incr + 1

End If

Next i



laneOrder = New ArrayList

For i = 0 To numLanes - 1

laneOrder.Add(lanes(i))

Next i

calcLaneOrder = laneOrder

End Function



Public Sub New(ByVal swmrs As Swimmers, ByVal lanes As Integer)

MyBase.New()

sw = swmrs

numLanes = lanes

End Sub

'-------------------

Public Function sort(ByVal sw As Swimmers) As Swimmers



Dim i, j, max As Integer

Dim tmp As Swimmer



Try

max = sw.Count

Dim asw(max) As Swimmer



For i = 0 To max - 1



asw(i) = CType(sw.Item(i), Swimmer)

Next i

For i = 0 To max - 1

For j = i To max - 1

If asw(i).getTime > asw(j).getTime Then

tmp = asw(i)

asw(j) = asw(i)

asw(i) = tmp

End If

Next j

Next i



sw = New Swimmers

For i = 0 To max - 1

sw.Add(asw(i))

Next i



sort = sw

Catch e As Exception

Console.WriteLine("Caught " + i.ToString + " " + j.ToString + " " + max.ToString + " " + e.ToString())

Console.WriteLine(e.StackTrace)

End Try

End Function

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 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。