Possible solutions for an function in an interface that may needs different parameters based on each use

2 hours ago 1
ARTICLE AD BOX

I've been designing a pooling system in c# usable in unity which pools and returns classes directly to the caller so any objects will directly have reference to the class of the pooled object.

below is my general interface that is placed on any class that i may wish to pool

public interface IPoolable<T> where T : MonoBehaviour, IPoolable<T> { ///This is just a reference to a list so that any object can pool itself back public Pool<T> Pool { get; set; } /// <summary> /// determines if pooled object is new spawn to run any function needed /// to initialise the object. Manually set it to false after you run any custom initialisation function /// </summary> public bool IsNewSpawn { get; set; } public bool IsPooled { get; set; } /// <summary> /// Implement a function to add itself back into the pool. /// Use to replace destroy function in execution /// </summary> public void PoolSelf(); }

A use case.

This is the object that will be pooled

public class PooledObject : IPoolable<PooledObject> { public Pool<PooledObject> Pool { get; set; } public bool IsNewSpawn { get; set; } public bool IsPooled { get; set; } public void PoolSelf() { ///whatever you need to do when this object needs to be pooled ///you then run a function in the pool which pools this object } ///say these are values i need to initialize on object spawn but not when it gets merely cached to be reused later public int randomValue; public string randomText; public vector3 randomVector; // i now setup an initialization function with variable parameter input based on what this class need public void Initialize(int value1, string value2, vector3 value3) { //do field assignment etc or anything else i may need } }

this is the spawner of said object with a reference to the same pool as the object so it can spawn stuff etc.

public class Spawner { //reference to the pool, i handle the assignment of reference with a static pool manager elsewhere private Pool<PooledObject> ObjPool; //a potential spawn method public void SpawnObj() { //this gets me a pooled object etc. PooledObject newSpawn = ObjPool.GetPooledObj(); //now i need to manually check if the newSpawn is actually a new object before i run the custom initialize method in the pooledobject } }

my problem is i wish to add this initialization function into the interface instead so all poolable objects will be guaranteed to have it rather then me doing a check with a bool then writing a custom intialization function for each unique pool case because it may have different input parameters

Any design suggestions will be appreciated.

Read Entire Article