Creating Object Factory Base with TDD in C#

In Tutorials, UECS by EforenLeave a Comment

General Overview

In this tutorial we work on an extendable class for creating an object using the factory design pattern for use in our Entity Component System (ECS) using C# using Test Driven Development (TDD).

This will allow us to try our best to avoid the Garbage Collector and in most cases also avoid creating objects instead just using objects we have laying around in cache.

There are several gotchas that we need to watch out for with this pattern one of the biggest is that we need to make sure we clean the objects. That is what we will be doing in this tutorial we will be setting up the code that will run the custom cleaners.

In the previous tutorial we setup the handling of handles which will be very important to this process as it will allow us to kill all references to the entity without going out to scrape all the references and nulling them out.

Lets look at what we need for this tutorial.

Tests & Scafolding

In our testing we will need to setup some example Factories so lets set those up. The first thing we need to do is setup out base class.
public abstract class ObjectFactoryBase
    /// <summary>
    /// Creates a new instance of the type specified.
    /// </summary>
    /// <returns>New instance of type</returns>
    public abstract object ObjCreateNew();
    /// <summary>
    /// Returns the same object passed in with all the instance specific stuff cleaned out.
    /// </summary>
    /// <param name="obj">the instance of the object that needs to be cleaned</param>
    /// <returns>The same object that was passed in for cleaning</returns>
    public abstract object ObjCleanForReuse(object obj);

Leave a Comment