in Education by
Short version: How would i go about creating an object pool that can store classes of different types all derived from the same base class? See below for an example of expected usage. Long version: I have a class BaseComponent, with many derived classes e.g. Child1Component, Child2Component. I also have another object that represents a collection of these components, with their properties set to specific values. I call this an EntityTemplate, since an entity is defined by the set of components and their values. I want to create entities based on entity components. To do this currently i get the appropriate EntityTemplate, loop through it's different components and call a Clone method I've defined on each child class. I also have a Copy method defined there too, which might be useful. When an entity expires i'd like to add its components to an object pool, then when i next need to create an entity i'd get the entity template and for each component i'd get one of the same type out of the pool and set it's properties equal to the one in the EntityTemplate, something like below: // What i want to do var entityTemplate = GetTemplate("UniqueString"); var MyActualEntity = new Entity(); foreach(var componentTemplate in entityTemplate) { var actualComponent = MagicComponentPool .GetComponentSameTypeAsParam(componentTemplate); actualComponent.CopyFrom(componentTemplate); MyActualEntity.Components.Add(actualComponent); } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I would use a dictionary. Dictionary dictionary = new Dictionary(); put the original components in like this: dictionary.Add(component.GetType(), component); and retrieve them by type. BaseComponent component = dictionary[componentTemplate.GetType()]; Complexity of retrieving objects from dictionaries is constant no matter how many objects there’s in the dictionary and is equal to the cost of calculating the hash of the key. However, I’m not sure if that is applicable for your purpose, but as you are copying the objects anyway, why not just clone the components from the template or even clone the whole template. Here’s a generic Clone method for you: using System.IO; using System.Runtime.Serialization.Formatters.Binary; public static T Clone(T o) { byte[] bytes = SerializeBinary(o); return DeserializeBinary(bytes); } public static byte[] SerializeBinary(object o) { if (o == null) return null; BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream()) { bf.Serialize(ms, o); return ms.GetBuffer(); } } public static T DeserializeBinary(byte[] bytes) { if (bytes == null) return default(T); BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream(bytes)) { return (T) bf.Deserialize(ms); } }

Related questions

0 votes
    I have to retrieve Derived class objects stored in a Map given the respective class name as key. As ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I have to retrieve Derived class objects stored in a Map given the respective class name as key. As ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    A __________ is an R-object which can contain many different types of elements inside it. (a) Vector (b) ... Out of R Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which of these packages contain all the collection classes? (a) java.lang (b) java.util (c) java.net ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these packages contain classes and interfaces used for input & output operations of a program? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of these packages contain all the collection classes? (a) java.lang (b) java.util (c) java. ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What is object pool in .Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What does a History object contain? (a) URL (b) Parameters (c) Attribute values (d) Property This ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 22, 2021 in Education by JackTerrance
0 votes
    What does a Git Commit object contain?...
asked Nov 4, 2020 in General by JackTerrance
0 votes
    What does a commit object contain?...
asked Nov 2, 2020 in Technology by JackTerrance
0 votes
    Which of these classes encapsulate runtime state of an object? (a) Class (b) System (c) Runtime (d ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What is an IPv4 address? What are the different classes of IPv4?...
asked Aug 14, 2021 in Technology by JackTerrance
0 votes
    We are building sentiment analysis application and we converted our tweets dataframe to an array. We created another array ... .txt").getLines.toArray var happyCount=0 for (e...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    We are building sentiment analysis application and we converted our tweets dataframe to an array. We created another array ... .txt").getLines.toArray var happyCount=0 for (e...
asked Apr 24, 2022 in Education by JackTerrance
...