Mögliche Lösungen für eine Funktion in einer Schnittstelle, die je nach Verwendung möglicherweise unterschiedliche ParamC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Mögliche Lösungen für eine Funktion in einer Schnittstelle, die je nach Verwendung möglicherweise unterschiedliche Param

Post by Anonymous »

Ich habe ein in Unity verwendbares Pooling-System in c# entworfen, das Klassen bündelt und direkt an den Aufrufer zurückgibt, sodass alle Objekte direkt auf die Klasse des gepoolten Objekts verweisen.
unten ist meine allgemeine Schnittstelle, die für jede Klasse platziert wird, die ich möglicherweise bündeln möchte

Code: Select all

public interface IPoolable where T : MonoBehaviour, IPoolable
{
///This is just a reference to a list so that any object can pool itself back
public Pool Pool { get; set; }
/// 
/// 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
/// 
public bool IsNewSpawn { get; set; }
public bool IsPooled { get; set; }
/// 
/// Implement a function to add itself back into the pool.
/// Use to replace destroy function in execution
/// 
public void PoolSelf();
}
Ein Anwendungsfall.
Dies ist das Objekt, das gepoolt wird

Code: Select all

public class PooledObject : IPoolable
{
public Pool 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
}
}
Dies ist der Spawner des Objekts mit einem Verweis auf denselben Pool wie das Objekt, damit es Dinge usw. erzeugen kann.

Code: Select all

public class Spawner
{
//reference to the pool, i handle the assignment of reference with a static pool manager elsewhere
private Pool 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
}
}
Mein Problem besteht darin, dass ich diese Initialisierungsfunktion stattdessen in die Schnittstelle einfügen möchte, damit alle poolfähigen Objekte sie garantiert haben, anstatt dass ich eine Prüfung mit einem Bool durchführe und dann eine benutzerdefinierte Initialisierungsfunktion für jeden einzelnen Poolfall schreibe, da sie möglicherweise unterschiedliche Eingabeparameter hat.
Jeder Designvorschlag ist willkommen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post