Factory Method in VB.NET
Creates an instance of several derived classes
ReturnDefine an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. The Abstract class may provide a default object, but each subclass can instantiate an extended version of the object.
Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern
1: ' Factory Method Design Pattern.
2: ' See description in //www.vb-net.com/ProgramTheory/FactoryMethod.htm
3: Class MainApp
4: ' Entry point into console application.
5: Public Shared Sub Main()
6: ' An array of creators
7: Dim creators As Creator() = New Creator(1) {}
8: creators(0) = New ConcreteCreatorA()
9: creators(1) = New ConcreteCreatorB()
10: ' Iterate over creators and create products
11: For Each creator As Creator In creators
12: Dim product As Product1 = creator.FactoryMethod()
13: Console.WriteLine("Created {0}", product.[GetType]().Name)
14: Next
15: ' Wait for user
16: Console.ReadKey()
17: End Sub
18: End Class
19:
20: ' The 'Product' abstract class
21: MustInherit Class Product1
22: End Class
23:
24: ' A 'ConcreteProduct' class
25: Class ConcreteProductA
26: Inherits Product1
27: End Class
28:
29: ' A 'ConcreteProduct' class
30: Class ConcreteProductB
31: Inherits Product1
32: End Class
33:
34: ' The 'Creator' abstract class
35: MustInherit Class Creator
36: Public MustOverride Function FactoryMethod() As Product1
37: End Class
38:
39: ' A 'ConcreteCreator' class
40: Class ConcreteCreatorA
41: Inherits Creator
42: Public Overrides Function FactoryMethod() As Product1
43: Return New ConcreteProductA()
44: End Function
45: End Class
46:
47: ' A 'ConcreteCreator' class
48: Class ConcreteCreatorB
49: Inherits Creator
50: Public Overrides Function FactoryMethod() As Product1
51: Return New ConcreteProductB()
52: End Function
53: 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/FactoryMethod.htm
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |