Move duplicated code into a factory

Replace subclasses of WeightBalancingMemoryHeuristics which contain
much duplicate code with a single factory class.

[#56447064]