Exercise: Game Entity Factory

Enforce safe object creation using generic type constraints and sealed classes.

Problem statement

In game development, an entity factory is responsible for spawning characters dynamically into the world. The objective is to build a generic character spawner that only accepts valid character types. Furthermore, game design rules dictate that specific high-level enemy types, like bosses, must have locked hierarchies and can never act as base classes for other entities.

Task requirements

  • Create an abstract base class named Character with a Name property.

  • Create a Boss class that inherits from Character. Ensure no other class can inherit from Boss.

  • Create a static EntityFactory class.

  • Implement a generic method Spawn<T>() inside the factory that instantiates the requested character type, sets a default name, and returns it.

Constraints

  • Mark the Character class as abstract to prevent direct instantiation.

  • Mark the Boss class as sealed to prevent derivation.

  • Apply type constraints to the generic Spawn<T>() method. Use where T : Character to ensure only characters are spawned, and new() to guarantee the type has a parameterless constructor for instantiation.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Use the abstract keyword on the base class so developers cannot spawn a generic “Character”.

  • Use the sealed keyword on the Boss class to lock its inheritance hierarchy.

  • A generic method signature with multiple constraints looks like this: public static T Spawn<T>() where T : Character, new().

  • Inside the generic method, you can simply write T newEntity = new T(); because the new() constraint guarantees this operation is safe.

Exercise: Game Entity Factory

Enforce safe object creation using generic type constraints and sealed classes.

Problem statement

In game development, an entity factory is responsible for spawning characters dynamically into the world. The objective is to build a generic character spawner that only accepts valid character types. Furthermore, game design rules dictate that specific high-level enemy types, like bosses, must have locked hierarchies and can never act as base classes for other entities.

Task requirements

  • Create an abstract base class named Character with a Name property.

  • Create a Boss class that inherits from Character. Ensure no other class can inherit from Boss.

  • Create a static EntityFactory class.

  • Implement a generic method Spawn<T>() inside the factory that instantiates the requested character type, sets a default name, and returns it.

Constraints

  • Mark the Character class as abstract to prevent direct instantiation.

  • Mark the Boss class as sealed to prevent derivation.

  • Apply type constraints to the generic Spawn<T>() method. Use where T : Character to ensure only characters are spawned, and new() to guarantee the type has a parameterless constructor for instantiation.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Use the abstract keyword on the base class so developers cannot spawn a generic “Character”.

  • Use the sealed keyword on the Boss class to lock its inheritance hierarchy.

  • A generic method signature with multiple constraints looks like this: public static T Spawn<T>() where T : Character, new().

  • Inside the generic method, you can simply write T newEntity = new T(); because the new() constraint guarantees this operation is safe.

C# 14.0
namespace GameDev;
// TODO: Define a public abstract class named Character with a string Name property
// TODO: Define a public sealed class named Boss that inherits from Character
// TODO: Define a public static class named EntityFactory
// TODO: Create a public static generic method Spawn<T>() returning T
// TODO: Apply the constraints: where T : Character, new()
// TODO: Inside Spawn<T>, instantiate T, assign a default name, and return the instance