Skip to content

Commit

Permalink
Adding Flywheight Pattern in scala
Browse files Browse the repository at this point in the history
  • Loading branch information
alanfgn committed Oct 30, 2022
1 parent fe88beb commit 1d32f70
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Structural/Flyweight/scala/Flywheight.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import scala.collection.mutable.ListBuffer

case class Flyweight(sharedState: String)

case class Context(uniqueState: Int, flyweight: Flyweight)

class FlyweightFactory {

var flyweights = ListBuffer.empty[Flyweight]

def getFlyweight(sharedState: String): Flyweight = {
val selectedFlyweights = flyweights.filter(flyweight => flyweight.sharedState.equals(sharedState))

if (selectedFlyweights.size > 0) {
selectedFlyweights(0)
} else {
val newFlyweight = Flyweight(sharedState)
flyweights.addOne(newFlyweight)
newFlyweight
}
}

def total = flyweights.size
}


class Client(flyweightFactory: FlyweightFactory) {

val contexts = ListBuffer.empty[Context]

def getContext(uniqueState: Int, sharedState: String): Context = {
val flyweight = flyweightFactory.getFlyweight(sharedState)
val context = Context(uniqueState, flyweight)
contexts.addOne(context)

context
}

}

object FlyweightPattern {

def main(args: Array[String]): Unit = {
val flyweightFactory = new FlyweightFactory()
val client = new Client(flyweightFactory)


val uniqueStates = Range(0, 10).toSeq
val sharedStates = Seq("Alpha", "Beta", "Gamma")

val contexts = uniqueStates.flatMap(uniqueState => {
sharedStates.map(sharedState => client.getContext(uniqueState, sharedState))
})

println(f"Contexts: ${contexts.size}")
println(f"Flyweights: ${flyweightFactory.total}")
}
}

0 comments on commit 1d32f70

Please sign in to comment.