Exercise: Game Entity Factory
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
Characterwith aNameproperty.Create a
Bossclass that inherits fromCharacter. Ensure no other class can inherit fromBoss.Create a static
EntityFactoryclass.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
Characterclass asabstractto prevent direct instantiation.Mark the
Bossclass assealedto prevent derivation.Apply type constraints to the generic
Spawn<T>()method. Usewhere T : Characterto ensure only characters are spawned, andnew()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
abstractkeyword on the base class so developers cannot spawn a generic “Character”.Use the
sealedkeyword on theBossclass 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 thenew()constraint guarantees this operation is safe.
Exercise: Game Entity Factory
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
Characterwith aNameproperty.Create a
Bossclass that inherits fromCharacter. Ensure no other class can inherit fromBoss.Create a static
EntityFactoryclass.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
Characterclass asabstractto prevent direct instantiation.Mark the
Bossclass assealedto prevent derivation.Apply type constraints to the generic
Spawn<T>()method. Usewhere T : Characterto ensure only characters are spawned, andnew()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
abstractkeyword on the base class so developers cannot spawn a generic “Character”.Use the
sealedkeyword on theBossclass 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 thenew()constraint guarantees this operation is safe.