-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} | ||
} |