Factory method
The factory method
We have learned Simple Factory Pattern, factory (Factory) thoughts throughout the entire object-oriented programming (OOP) and other design modes. Such as: Builder mode. Between, a single class plays a role similar to traffic police, deciding which subclasses at a single level will be instantiated.
Factory Method Pattern is a clever extension of Factory's idea, which makes an excellent instantiation to each subclass of it. This mode does not specifically indicate which subclass delayed, but defines an abstract class to create an object, so that the subclass decides which object created.
Here is a simple example in a swimming game in a swimming lane. According to the grouse of the athlete's grades, the more the speed of the finals of the group, the more the finals of the team finals, the slower, the more the speed is, the better, the better in each group, the faster the speed of the player It is closer to the middle of the middle. This is called stright seeding.
When the swimmer is in the tournament game, they usually swim twice. By competing in each other in the preliminaries, the top 12 or 16 swimmers will continue to compete with each other in the final. For the preliminaries, Circle Seeded is used, so the fastest 3 players are in the most rapid three-group central lanes. The best speed of 3 players in the remaining players, and so on. We have to implement this selection mode and use factory methods. First, design an abstract event class:
Public Class Events
Protected Numlanes as in 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) 'Opens a text file
Fl.openforread ()
Numlanes = lanes' Save Lane Quantity Information
SWMMERS = New Swimmers
'Read swimming player information
s = fl.readline
While not fl.feof
SW = new swimmer (s) 'builds an object
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 SEEDINGEND FUNCTION
END CLASS
Because all derived classes are read from the text file, we use the Events class as the base class. The methods defined are unusual, and the specific classes can be implemented by inheriting the Events class (the preloadted class, the TimedFinalEvent class), and the only difference between the two classes is different. We also defined an abstract selection class containing the following methods:
Public Mustinherit Class SEEDING
Protected Numlanes as in Integer
Protected laneorder as arraylist
Protected Numheats as in
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 kil
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
These methods are built to call these methods for this selection basis (SEEDING). We now build these two specific selection subclasses: StraightSeeding Class CircleaseEding class. The PrelimEvent class will return an instance of the Circleseeding class, and the TimedFinaLent class returns the StraightSeeding class instance.
The structure is as follows:
Through the entire event level, you can see the PrelimEvent class, the TimedFinalevent class contains the getSeeding method, the PrelimEvent class will return an instance of the Circleseeding class, and the TimedFinaLent class returns the StraightSeeding class instance. In this simple example, we did not point out the real factory (Factory), however, which Event class instantizes determines which SEEDING class will be instantiated.
Although it looks like a pair of communication between two classes. But this is not required. There are many different types of Events classes but they only use a few seeding classes.
SWIMMER class
The SWIMMER class contains some of the specific information of the swimmer and a group of groups and lanes that modify the athletes after selection. EVENT class reads athlete information in the database (in this example). Passing this athlete's collection to the SEEDING class when the GetSeeding method of the EVENT class is used.
Events class
We have defined an abstract base class. In actual applications, we just use it to read the data information of the swimmer (in this example for text files).
PrelimEvent class Returns a Circleseeding instance
Public Class Prelimevent
Inherits Events
DIM SD As SEEDING
Public overrides function getseeding () as dressing
Return New CircleaseEDing (SWMMERS, NUMLANES)
END FUNCTION
Public overrides function isfinal () as booleanisfinal = false
END FUNCTION
Public overrides function isprelim () AS Boolean
Isprelim = TRUE
END FUNCTION
Public overrides function istimedFinal () as boolean
iStimedFinal = FALSE
END FUNCTION
Public Sub New (Byval FileName As String, ByVal Lanes As Integer)
Mybase.new (filename, lanes)
Numlanes = Lanes
End Sub
END CLASS
TIMEDFINALEVENT class Returns StraightSeeding instance
Public class timedfinalEvent
Inherits Events
Public Sub New (Byval FileName As String, ByVal Lanes As Integer)
Mybase.new (filename, lanes)
End Sub
'--------
Public overrides function getseeding () as dressing
DIM SD As SEEDING
'CREATE SEEDING AND EXECUTE IT
SD = new straightseeding (swmmers, numlanes)
sd.seed ()
GetSeeding = SD
END FUNCTION
'--------
Public overrides function isfinal () as boolean
IsFinal = false
END FUNCTION
'--------
Public overrides function isprelim () AS Boolean
Isprelim = FALSE
END FUNCTION
'--------
Public overrides function istimedFinal () as boolean
iStimedFinal = TRUE
END FUNCTION
END CLASS
Straight SEEDING class
In this applet in practical applications, most of the work is done in the Straight SEEDING class. We instantiate the StraightSeeding class, copy, modify the collection of swimmers and the locked position.
Public class straightseeding
Inherits seeding
Public overrides sub seed ()
DIM Lastheat As Integer
Dim Lastlanes as in
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) Thennumheats = 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 = la castlanes 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
When the GetSwimmers method is called, the StraightSeeding class will create an array of selected swimmers.
Circle Seeding Class
The CircleaseEding class is derived from the StraightSeeding class.
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) 'sort
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
Fork = 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
run
In this example, we use the 500M freestyle and 100m freestaist list to create the TimedFinaLent class and the PrelimEvent class.
BLOG: http://blog.9cbs.net/guoyan19811021/