QTP设计模式-工厂设计模式

'工厂设计模式举例:

Public oOutputs

Set oOutputs = New OutFactory

 

Class OutFactory

 Public Channels '定义变量

 Private Sub Class_Initialize

  Set Me.Channels = CreateObject("Scripting.Dictionary")

 End Sub

 Private Sub Class_Terminate

  Set Me.Channels = Nothing

 End Sub

 Public Sub Construct(sChannelName, sChannelType)

  Dim oNewChannel

  Select Case sChannelType

   Case "excel"

    '创建一个Excel的SINGLETON类

    Set oNewChannel = New ExcelWrapper

   Case "fso"

    '创建一个FSO的SINGLETON类

    Set oNewChannel = New fsoWrapper

   Case "DB"

    '创建一个ADO的SINGLETON类

    Set oNewChannel = New DBWrapper

  End Select

  '把类都存储起来

  Me.Channels.Add sChannelName,oNewChannel

 End Sub

End Class

'*********构造方法*********

oOutputs.Construct "excel_instance","excel"

oOutputs.Construct "fso_instance","fso"

oOutputs.Construct "DB_instance","DB"

'********直接取我们需要的东西,并进行操作**********

oOutputs.Channels("excel_instance").whatever

'此处的Whatever是Excel的SINGLETON类中的自定义方法

 

posted @ 2012-09-11 09:24  dushuai  阅读(214)  评论(0编辑  收藏  举报