抽象工厂模式
程序代码:
'抽象工厂模式 Module Module1 Sub Main() Dim man As New Man Dim yangsheng As New YangSheng man = yangsheng.GetPerson("Man") man.Eat() man.Talk() Dim bull As New Bull bull = yangsheng.GetAnimal("Bull") bull.Eat() bull.Sleep() Dim woman As New Woman Dim yinsheng As New YinSheng woman = yinsheng.GetPerson("Woman") woman.Eat() woman.Talk() Dim cow As New Cow cow = yinsheng.GetAnimal("Cow") cow.Eat() cow.Sleep() System.Console.ReadLine() End Sub End Module Public Interface IPerson '人类工厂 Sub Eat() Sub Talk() End Interface Public Interface IAnimal '动物工厂 Sub Eat() Sub Sleep() End Interface Public Class Man : Implements IPerson '男人 Public Sub Eat() Implements IPerson.Eat System.Console.WriteLine("男人在吃东西...") End Sub Public Sub Talk() Implements IPerson.Talk System.Console.WriteLine("男人在说话...") End Sub End Class Public Class Woman : Implements IPerson '女人 Public Sub Eat() Implements IPerson.Eat System.Console.WriteLine("女人在吃东西...") End Sub Public Sub Talk() Implements IPerson.Talk System.Console.WriteLine("女人在说话...") End Sub End Class Public Class Bull : Implements IAnimal '公牛 Public Sub Eat() Implements IAnimal.Eat System.Console.WriteLine("公牛在吃东西...") End Sub Public Sub Sleep() Implements IAnimal.Sleep System.Console.WriteLine("公牛睡觉了...") End Sub End Class Public Class Cow : Implements IAnimal '母牛 Public Sub Eat() Implements IAnimal.Eat System.Console.WriteLine("母牛在吃东西...") End Sub Public Sub Sleep() Implements IAnimal.Sleep System.Console.WriteLine("母牛睡觉了...") End Sub End Class Public Interface INWFactory '女娲工厂 Function GetPerson(ByVal PersonType As String) As IPerson Function GetAnimal(ByVal AnimalType As String) As IAnimal End Interface Public Class YangSheng : Implements INWFactory 'YangSheng-->阳绳 造雄性动物 Public Function GetAnimal(ByVal AnimalType As String) As IAnimal Implements INWFactory.GetAnimal If AnimalType = "Bull" Then Return New Bull Return System.DBNull.Value End Function Public Function GetPerson(ByVal PersonType As String) As IPerson Implements INWFactory.GetPerson If PersonType = "Man" Then Return New Man Return System.DBNull.Value End Function End Class Public Class YinSheng : Implements INWFactory 'YinShen-->阴绳 造雌性动物 Public Function GetAnimal(ByVal AnimalType As String) As IAnimal Implements INWFactory.GetAnimal If AnimalType = "Cow" Then Return New Cow Return System.DBNull.Value End Function Public Function GetPerson(ByVal PersonType As String) As IPerson Implements INWFactory.GetPerson If PersonType = "Woman" Then Return New Woman Return System.DBNull.Value End Function End Class