| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1133 人关注过本帖
标题:工厂方法模式
只看楼主 加入收藏
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
结帖率:78.95%
收藏
 问题点数:0 回复次数:0 
工厂方法模式
程序代码:
'工厂方法模式
Module Module1

    Sub Main()
        '小汽车
        Dim MyCar As ICar = CarFactory.GetCarInterface("Benz")
        MyCar.CarRun()
        MyCar.CarStop()
        MyCar = CarFactory.GetCarInterface("Ford")
        MyCar.CarRun()
        MyCar.CarStop()


        '巴士
        Dim MyBus As ICar = BusFactory.GetCarInterface("BigBus")
        MyBus.CarRun()
        MyBus.CarStop()
        MyBus = BusFactory.GetCarInterface("MiniBus")
        MyBus.CarRun()
        MyBus.CarStop()

        System.Console.ReadLine()
    End Sub

End Module

Public Interface ICar    '抽象汽车
    Sub CarRun()
    Sub CarStop()
End Interface

Public Class Benz : Implements ICar  '奔驰汽车


    Public Sub CarRun() Implements ICar.CarRun
        System.Console.WriteLine("奔驰汽车启动了.......")
    End Sub

    Public Sub CarStop() Implements ICar.CarStop
        System.Console.WriteLine("奔驰汽车停车了.......")
    End Sub
End Class

Public Class Ford : Implements ICar  '福特汽车

    Public Sub CarRun() Implements ICar.CarRun
        System.Console.WriteLine("福特汽车启动了.......")
    End Sub

    Public Sub CarStop() Implements ICar.CarStop
        System.Console.WriteLine("福特汽车停车了.......")
    End Sub

End Class

Public Class BigBus : Implements ICar  '大巴

    Public Sub CarRun() Implements ICar.CarRun
        System.Console.WriteLine("大巴启动了.......")
    End Sub

    Public Sub CarStop() Implements ICar.CarStop
        System.Console.WriteLine("大巴停车了.......")
    End Sub

End Class

Public Class MiniBus : Implements ICar  '小巴

    Public Sub CarRun() Implements ICar.CarRun
        System.Console.WriteLine("小巴启动了.......")
    End Sub

    Public Sub CarStop() Implements ICar.CarStop
        System.Console.WriteLine("小巴停车了.......")
    End Sub

End Class
Public Interface AbstructCarFactory '抽象汽车工厂

End Interface

Public Class CarFactory : Implements AbstructCarFactory  '小汽车工厂车间
    Public Shared Function GetCarInterface(ByVal CarType As String) As ICar
        Select Case CarType
            Case "Benz"
                Return New Benz
            Case "Ford"
                Return New Ford
        End Select
        Return System.DBNull.Value
    End Function
End Class

Public Class BusFactory : Implements AbstructCarFactory  '巴士工厂车间
    Public Shared Function GetCarInterface(ByVal CarType As String) As ICar
        Select Case CarType
            Case "BigBus"
                Return New BigBus
            Case "MiniBus"
                Return New MiniBus
        End Select
        Return System.DBNull.Value
    End Function
End Class

为了简单起见,没有用到反射机制.
搜索更多相关主题的帖子: 工厂 模式 
2008-02-17 14:18
快速回复:工厂方法模式
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038150 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved