分类

  • 软件天地

  • 设计模式之工厂方法 FACTORY METHOD(一)‖
    整个面向对象编程(OOP)
    ,一个单一的类扮演类似交
    厂模式(Simple Factory Patter
    以及其他一些设计模式的始终。
    通警察的角色,决定哪一个单一
    n),工厂(factory)思想贯穿于
    如:生成器(Builder)模式。其间
    层次上的子类将被实例化。

      工厂方法模式(Factor
    ,它使得将超类的实例化延
    ,而是定义一个抽象类创建
    y Method pattern)是对工厂(
    迟到它的每一个子类。这个模式
    对象,让其子类决定创建哪一个
    factory)思想进行了巧妙的延伸
    没有具体的指出延迟到哪一个子类
    对蟆?br>

      下面是一个简单的例子
    绩排列决赛事的分组,速度
    ,而且在每一个小组中成绩
    straight seeding。
    ,在一个游泳比赛中如何确定游
    越快所分的小组的决赛的次序越
    越好、速度越快的选手也就越靠

    泳运动员的泳道。按照运动员的成
    靠后,反之,速度越慢就越先比赛
    近中间的泳道。这被称为


      当游泳运动员在锦标赛比赛过程中,
    名或者16名游泳运动员将继续在决赛中继
    seeded,这样使得速度最快的3名选手分
    再选出速度最好的3名选手,等等。
    他们通常要游两次。 通过在预赛中相互竞争,前12
    续彼此竞争。 为了预赛工作更公平, 采用circle
    别处于最迅速3个小组的中心泳道。在剩下的选手中


      我们要实现这个选拔模式并且使用工厂方法。                                      

      首先,设计抽象事件类:                                                        

      Public Class Events                                        

        Protected numLanes As Integer                

        Protected swmmers As Swimmers                

        '-----                                                              

        Public Sub New(ByVal Filenam
    e As String, ByVal lanes As Integer)

          MyBase.New()                                              

          Dim s As String                                        

          Dim sw As Swimmer                                    

          Dim fl As vbFile                                      

          fl = New vbF
    ile(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                                                  

        '-----                                                              

       ublic Overridab
    le Function isPrelim() As Bo
    olean

        End Function                                                  

        '-----                                                              

        Public Overridab
    le Function isFinal() As Boo
    lean

        End Function                                                  

        '-----                                                              

        Public Overridab
    le Function isTimedFinal() A
    s Boolean

        End Function                                                  

        '-----                                                              

        Public Overridable Function
    getSeeding() As Seeding

        End Function                                                  

      End Class                                                            

      因为所有的派生类都要从文本文件读
    义的方法均为虚方法,可以通过继承Even
    TimedFinalEvent类),这两个类之间唯
    一个包含以下方法的抽象选拔类:
    取数据,所以,我们把Events类作为基类。其中所定
    ts类来实现具体的类(PrelimEvent类、
    一的不同就是返回的选拔的类别不同。我们也定义了


      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 I
    nteger) 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(B
    yVal swmrs As Swimmers, ByVa
    l lanes As Integer)

          MyBase.New()                                              

          sw = swmrs                                                  

          numLanes = lanes                                      

        End Sub                                                            

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

        Public Function sort(ByVal s
    w 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), Swimm
    er)

            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("C
    + max.ToString + " " + e.ToString())
    aught " + i.ToString + " " + j.ToString + " "


            Console.WriteLine(e.StackTrace)    

          End Try                                                        

        End Function                                                  

      End Class                                                            

    上一页 下一页




    map