(NET) NET (2016)

Flyweight in VB.NET

A fine-grained instance used for efficient sharing

Return


Use 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 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/Flyweight.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>