Builder in VB.NET
Separates object construction from its representation
ReturnSeparate the construction of a complex object from its representation so that the same construction process can create different representations. The construction process can create different object representations and provides a high level of control over the assembly of the objects.
Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern
1: ' Builder Design Pattern.
2: ' See description in //www.vb-net.com/ProgramTheory/Builder.htm
3: Public Class MainApp
4: ' Entry point into console application.
5: Public Shared Sub Main()
6: ' Create director and builders
7: Dim director As New Director()
8: Dim b1 As Builder = New ConcreteBuilder1()
9: Dim b2 As Builder = New ConcreteBuilder2()
10: ' Construct two products
11: director.Construct(b1)
12: Dim p1 As Product = b1.GetResult()
13: p1.Show()
14: director.Construct(b2)
15: Dim p2 As Product = b2.GetResult()
16: p2.Show()
17: ' Wait for user
18: Console.ReadKey()
19: End Sub
20: End Class
21:
22: ' The 'Director' class
23: Class Director
24: ' Builder uses a complex series of steps
25: Public Sub Construct(builder As Builder)
26: builder.BuildPartA()
27: builder.BuildPartB()
28: End Sub
29: End Class
30:
31: ' The 'Builder' abstract class
32: MustInherit Class Builder
33: Public MustOverride Sub BuildPartA()
34: Public MustOverride Sub BuildPartB()
35: Public MustOverride Function GetResult() As Product
36: End Class
37:
38: ' The 'ConcreteBuilder1' class
39: Class ConcreteBuilder1
40: Inherits Builder
41: Private _product As New Product()
42: Public Overrides Sub BuildPartA()
43: _product.Add("PartA")
44: End Sub
45: Public Overrides Sub BuildPartB()
46: _product.Add("PartB")
47: End Sub
48: Public Overrides Function GetResult() As Product
49: Return _product
50: End Function
51: End Class
52:
53: ' The 'ConcreteBuilder2' class
54: Class ConcreteBuilder2
55: Inherits Builder
56: Private _product As New Product()
57: Public Overrides Sub BuildPartA()
58: _product.Add("PartX")
59: End Sub
60: Public Overrides Sub BuildPartB()
61: _product.Add("PartY")
62: End Sub
63: Public Overrides Function GetResult() As Product
64: Return _product
65: End Function
66: End Class
67:
68: ' The 'Product' class
69: Class Product
70: Private _parts As New List(Of String)()
71: Public Sub Add(part As String)
72: _parts.Add(part)
73: End Sub
74: Public Sub Show()
75: Console.WriteLine(vbLf & "Product Parts -------")
76: For Each part As String In _parts
77: Console.WriteLine(part)
78: Next
79: End Sub
80: 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/Builder.htm
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |