(NET) NET (2016)

Prototype in VB.NET

A fully initialized instance to be copied or cloned

Return


Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.







Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern



   1:  ' Prototype Design Pattern.
   2:  ' See description in //www.vb-net.com/ProgramTheory/Prototype.htm
   3:  Class MainApp
   4:      ' Entry point into console application.
   5:      Public Shared Sub Main()
   6:          ' Create two instances and clone each
   7:          Dim p1 As New ConcretePrototype1("I")
   8:          Dim c1 As ConcretePrototype1 = DirectCast(p1.Clone(), ConcretePrototype1)
   9:          Console.WriteLine("Cloned: {0}", c1.Id)
  10:          Dim p2 As New ConcretePrototype2("II")
  11:          Dim c2 As ConcretePrototype2 = DirectCast(p2.Clone(), ConcretePrototype2)
  12:          Console.WriteLine("Cloned: {0}", c2.Id)
  13:          ' Wait for user
  14:          Console.ReadKey()
  15:      End Sub
  16:  End Class
  17:   
  18:  ' The 'Prototype' abstract class
  19:  MustInherit Class Prototype
  20:      Private _id As String
  21:      ' Constructor
  22:      Public Sub New(id As String)
  23:          Me._id = id
  24:      End Sub
  25:      ' Gets id
  26:      Public ReadOnly Property Id() As String
  27:          Get
  28:              Return _id
  29:          End Get
  30:      End Property
  31:      Public MustOverride Function Clone() As Prototype
  32:  End Class
  33:   
  34:  ' A 'ConcretePrototype' class 
  35:  Class ConcretePrototype1
  36:      Inherits Prototype
  37:      ' Constructor
  38:      Public Sub New(id As String)
  39:          MyBase.New(id)
  40:      End Sub
  41:      ' Returns a shallow copy
  42:      Public Overrides Function Clone() As Prototype
  43:          Return DirectCast(Me.MemberwiseClone(), Prototype)
  44:      End Function
  45:  End Class
  46:   
  47:  ' A 'ConcretePrototype' class 
  48:  Class ConcretePrototype2
  49:      Inherits Prototype
  50:      ' Constructor
  51:      Public Sub New(id As String)
  52:          MyBase.New(id)
  53:      End Sub
  54:      ' Returns a shallow copy
  55:      Public Overrides Function Clone() As Prototype
  56:          Return DirectCast(Me.MemberwiseClone(), Prototype)
  57:      End Function
  58:  End Class




See also:
Creational Patterns Structural Patterns Behavioral Patterns

Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/ProgramTheory/Prototype.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>