Flyweight in VB.NET
A fine-grained instance used for efficient sharing
ReturnUse sharing to support large numbers of fine-grained objects efficiently. Flyweight pattern with a relatively small number of objects is shared many times by different clients.
Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern
1: ' Flyweight Design Pattern.
2: ' See description in //www.vb-net.com/ProgramTheory/Flyweight.htm
3: Class MainApp
4: ' Entry point into console application.
5: Public Shared Sub Main()
6: ' Arbitrary extrinsic state
7: Dim extrinsicstate As Integer = 22
8: Dim factory As New FlyweightFactory()
9: ' Work with different flyweight instances
10: Dim fx As Flyweight = factory.GetFlyweight("X")
11: extrinsicstate = extrinsicstate - 1
12: fx.Operation(extrinsicstate)
13: Dim fy As Flyweight = factory.GetFlyweight("Y")
14: extrinsicstate = extrinsicstate - 1
15: fy.Operation(extrinsicstate)
16: Dim fz As Flyweight = factory.GetFlyweight("Z")
17: extrinsicstate = extrinsicstate - 1
18: fz.Operation(extrinsicstate)
19: Dim fu As New UnsharedConcreteFlyweight()
20: extrinsicstate = extrinsicstate - 1
21: fu.Operation(extrinsicstate)
22: ' Wait for user
23: Console.ReadKey()
24: End Sub
25: End Class
26:
27: ' The 'FlyweightFactory' class
28: Class FlyweightFactory
29: Private flyweights As New Hashtable()
30: ' Constructor
31: Public Sub New()
32: flyweights.Add("X", New ConcreteFlyweight())
33: flyweights.Add("Y", New ConcreteFlyweight())
34: flyweights.Add("Z", New ConcreteFlyweight())
35: End Sub
36: Public Function GetFlyweight(key As String) As Flyweight
37: Return DirectCast(flyweights(key), Flyweight)
38: End Function
39: End Class
40:
41: ' The 'Flyweight' abstract class
42: MustInherit Class Flyweight
43: Public MustOverride Sub Operation(extrinsicstate As Integer)
44: End Class
45:
46: ' The 'ConcreteFlyweight' class
47: Class ConcreteFlyweight
48: Inherits Flyweight
49: Public Overrides Sub Operation(extrinsicstate As Integer)
50: Console.WriteLine("ConcreteFlyweight: " & extrinsicstate.ToString)
51: End Sub
52: End Class
53:
54: ' The 'UnsharedConcreteFlyweight' class
55: Class UnsharedConcreteFlyweight
56: Inherits Flyweight
57: Public Overrides Sub Operation(extrinsicstate As Integer)
58: Console.WriteLine("UnsharedConcreteFlyweight: " & extrinsicstate.ToString)
59: End Sub
60: 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/Flyweight.htm
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |