Prototype in VB.NET
A fully initialized instance to be copied or cloned
ReturnSpecify 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
- Abstract Factory in VB.NET - Creates an instance of several families of classes.
- Builder in VB.NET - Separates object construction from its representation.
- Factory Method in VB.NET - Creates an instance of several derived classes.
- Prototype in VB.NET - A fully initialized instance to be copied or cloned.
- Singleton in VB.NET - A class of which only a single instance can exist.
- Adapter in VB.NET - Match interfaces of different classes
- Bridge in VB.NET - Separates an object???s interface from its implementation
- Composite in VB.NET - A tree structure of simple and composite objects
- Decorator in VB.NET - Add responsibilities to objects dynamically
- Facade in VB.NET - A single class that represents an entire subsystem
- Flyweight in VB.NET - A fine-grained instance used for efficient sharing
- Proxy in VB.NET - An object representing another object
- Chain of Resp. in VB.NET - A way of passing a request between a chain of objects
- Command in VB.NET - Encapsulate a command request as an object
- Interpreter in VB.NET - A way to include language elements in a program
- Iterator in VB.NET - Sequentially access the elements of a collection
- Mediator in VB.NET - Defines simplified communication between classes
- Memento in VB.NET - Capture and restore an object's internal state
- Observer in VB.NET - A way of notifying change to a number of classes
- State in VB.NET - Alter an object's behavior when its state changes
- Strategy in VB.NET - Encapsulates an algorithm inside a class
- Template Method in VB.NET - Defer the exact steps of an algorithm to a subclass
- Visitor in VB.NET - Defines a new operation to a class without change
Comments (
)
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> |