Adding a Flyweight factory
In the Scripts folder, create a new C# script named FlyweightFactory
and update its contents to match the following code block, which stores a Dictionary
of IFlyweight
objects (indexed by the TerrainType
name
) and returns either a newly created or reused tile flyweight when queried by the client:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 1
public class FlyweightFactory
{
// 2
private static Dictionary<string, IFlyweight> _cache = new Dictionary<string, IFlyweight>();
// 3
public static IFlyweight GetFlyweight(TerrainType type)
{
// 4
var key = type.ToString();
// 5
if(!_cache.ContainsKey(key))
{
IFlyweight newTile = null;
switch(type)
{
case TerrainType.Ground:
newTile = new Tile(type, Color.white, 0.75f);
break;
case TerrainType.Water...