电脑网首页 | 新闻动态 | 报价查询 | 产品库 | 电脑学院 | 游戏 | 二手市场 | 小游戏 | 电脑商城 | 下载中心 | 电脑论坛 | 天天杀毒 | 网址 | 搜索
电脑广告载入中
您现在的位置: 衢州电脑网 >> 学院 >> 网页设计 >> 代码学习 >> .NET教程 >> .NET概论及软件使用 >> 文章正文
 2006-4-24 22:40:11

设计模式之工厂方法(FACTORY METHOD))(一)

作者:佚名    文章来源:www.youw.cn    点击数:    更新时间:2006-4-24 去论坛 要投稿
GOOGLE loading...
工厂方法

THE FACTORY METHOD

我们已经学会了简单工厂模式(Simple Factory Pattern),工厂(factory)思想贯穿于整个面向对象编程(OOP)以及其他一些设计模式的始终。如:生成器(Builder)模式。其间,一个单一的类扮演类似交通警察的角色,决定哪一个单一层次上的子类将被实例化。

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

下面是一个简单的例子,在一个游泳比赛中如何确定游泳运动员的泳道。按照运动员的成绩排列决赛事的分组,速度越快所分的小组的决赛的次序越靠后,反之,速度越慢就越先比赛,而且在每一个小组中成绩越好、速度越快的选手也就越靠近中间的泳道。这被称为straight seeding。

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

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

首先,设计抽象事件类:

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




[1] [2]  下一页


 
去百度搜索   去GOOGLE搜索   去雅虎搜索
GOOGLE广告载入中
 相关新闻  热点新闻
重构模式1    xxcc(收藏)
设计模式之简单工厂模式(Simple
使用Publish/Subscribe 设计模式
设计模式之观察者模式(Observer
使用Publish/Subscribe 设计模式
设计模式之观察者模式(Observer
图片新闻载入中
评分: 1分 2分 3分 4分 5分
评论载入中......
 电脑配置单     07/11/4
  • CPU AMD Athlon64 X2 3800+ AM2(90纳米/65W盒 ¥460
  • 主板 华硕 M2A-VM ¥499
  • 内存 威刚VDATA 1GB DDR2 667(万紫千红) ¥165
  • 硬盘 希捷 160G 7200.10 8M(串口/散) ¥455
  • 显卡 影驰 8600GT魔灵 ¥799
  • 液晶显示器 优派 Q19wb ¥1499
  • 机箱 华硕 TT-67 ¥180
  • 电源 航嘉 多核DH6 ¥398
  • 键鼠套装 多彩 防水高手K8020P+M338BP ¥99
  • 总价: 4554 元
  • 如要购买请联系本站
  • 您有报价提供请点此处
关于我们 - 免责声明 - 用户注册 - 网站 建议 - 合作联系 - 招聘精英 - 网站地图 - 客户服务