Flyweight Pattern

November 02, 2014

Reading time ~1 minute

Flyweight Pattern is one of the Structural Design Patterns. Its goal is to ease the memory consumption when using many objects by extracting the common properties in some kind of pool in the memory, represented by a data structure such as Dictionary, HashMap or another.

Real World Example

Lets say, for example that you are creating a Car Registration System. Everything is fine until you have to dealt with the problem of registering millions of cars and your system begins to take large portion of memory.

One way to deal with this problem is to use Flyweight Pattern.

Every car has properties like model, year, make, owner, tag, renew date and etc.. Some of these properties are common among the cars and you could extract them so the memory for them is allocated only once and shared between the cars. This is one classical example of Flyweight Pattern usage and it should decrease your system memory consumption.

Example Implementation

So, what we are going to need to make our Car System work efficiently.

We will need a CarFactory, CarManager and two objects to hold the intrinsic (common) and extrinsic (variable) car properties.

Normally we would use Interfaces for the factory and the manager, but for sake of simplicity we will not, in this post.

I use the two objects to avoid confusion to which properties are intrinsic and which are extrinsic.

using System;
namespace CarRegistration
{
public class ExtrinsicCar
{
public object Owner { get; set; }
public string Tag { get; set; }
public DateTime RenewDate { get; set; }
}
}
view raw ExtrensicCar hosted with ❤ by GitHub
namespace CarRegistration
{
public class IntrinsicCar
{
public string Model { get; set; }
public int Year { get; set; }
public string Make { get; set; }
}
}
view raw IntrinsicCar hosted with ❤ by GitHub

Now lets take a look at CarFactory. It holds a Dictionary that holds the common intrinsic car properties, to avoid the instantiation of multiple common objects.

using System.Collections.Generic;
namespace CarRegistration
{
public class CarFactory
{
private readonly Dictionary<string, IntrinsicCar> _carPool;
public CarFactory()
{
_carPool = new Dictionary<string, IntrinsicCar>();
}
public IntrinsicCar CreateCar(IntrinsicCar car)
{
string carKey = string.Format("{0}-{1}-{2}", car.Model, car.Year, car.Make);
if (_carPool.ContainsKey(carKey))
{
return _carPool[carKey];
}
var newCar = new IntrinsicCar()
{
Make = car.Make,
Model = car.Model,
Year = car.Year
};
_carPool.Add(carKey, newCar);
return newCar;
}
}
}
view raw CarFactory hosted with ❤ by GitHub

And finally here is our CarManager, where the actual registration logic is.

namespace CarRegistration
{
public class CarManager
{
private readonly CarFactory _carFactory;
public CarManager()
{
_carFactory = new CarFactory();
}
public void RegisterCar(IntrinsicCar intrinsicCar, ExtrinsicCar extrinsicCar)
{
IntrinsicCar car = _carFactory.CreateCar(intrinsicCar);
// some logic to save intrinsic and extrinsic car properties to database, for example.
}
}
}
view raw CarManager hosted with ❤ by GitHub

Release early, release often (REPO) strategies

In this post I will try to review some of the things that every company should do in order to have better product releases. Continue reading

New things in C# 6

Published on December 22, 2014

ECMAScript 6 - The new JavaScript Part 2

Published on December 07, 2014