Lets suppose i am having a package "pack" and in this package i can have multiple number of classes.
But i dont know the names of classes. Now at runtime i want to check if a particular attribute "attr1" is present in any class.
If it is present i have to create object of this class. In other case i have to ignore this class.
I have attached my project in which i have a package "Data" in which there are five classes having some attribute.
I want to run this and at runtime i want to check if "range " attribute is present in any class. And i want to store that data in any file.
I am using Rhapsody 7.6.
Could anyone please tell me possible ways to do so.?
Hoping to hear from you soon.
Thanks in Advance
Pinned topic Dynamic Creating Of Objects
Answered question This question has been answered.
Unanswered question This question has not been answered yet.
Updated on 2013-04-04T13:29:07Z at 2013-04-04T13:29:07Z by Jeff_Cohen
Jeff_Cohen 270001TEU371 Posts
Re: Dynamic Creating Of Objects2013-04-04T13:29:07ZThis is the accepted answer. This is the accepted answer.When I get questions like this, I always turn them around and ask how the person would do it if they are working with cutting the code by hand.
In your case, I'm a little confused because I think you left out some information. A class has no data associated with it, only instantiations of that class do. Therefore, you know at design time if a class has an attribute.
I'm going to make an assumption that you are trying to create a class factory that is reading configuration data from a file. If a certain attribute has a value within a certain range, then you will create an object. In other words, attr1 is always present, but sometimes it has a valid value, sometimes it doesn't.
If my assumption is true, take a look at the class factory pattern (note, Rhapsody uses a class factory already, but I don't think I would use that for this job. Rather, create your own). All of your classes will need to be derived from a base class (e.g., ClassRoot) that has an operation CreateInstance()(these can be locally overridden). Create a class that reads the file. While reading the file, parse it for attr1. If it is present (or has a valid range), call the CreateInstance() method for the specific child class. (note: it would be more efficient if attr1 were NOT an attribute of the class because it is only used when creating the class)
Hope this helps.