How to create a class when its attributes are dynamic and variable in Java, C ++ or any object-oriented language?

Well, in the Object-Oriented Language (OOL), when creating a class, we often know all its attributes in advance. Ex, the Item class must have fixed attributes (color, model, brand, price). So we just:

public Class Item{ private String color; private String model; //etc more attribute here //& set & get method for all attributes public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } } 

But what if all attributes are dynamic? For example, in one company their product attributes may be color, brand, but in another company they do not have color and brand attributes, but they have a width, height, size ...

How to create a class that accepts dynamic attributes in Java, C ++ or in any OOL?

+4
source share
3 answers

How to create a class that accepts dynamic attributes in Java, C ++ or in any OOL?

It depends on how you want to use it. In many cases, you can redesign your class to contain some kind of dynamically growing set, for example std::map in C ++ or Map (or Dictionary ) in Java.

This allows you to create and add arbitrary data for each instance using the key selected at run time.

+3
source

.NET

First I will start by analyzing your requirement, this is not something you will see often. Your classes may not be configured correctly.

The option can be saved in the dictionary

With the new version of .NET, they have a dynamic keyword that can help create dynamic properties such as ViewBag.

You can also use reflection with Reflection Emit or ICustomTypeDescriptor .

0
source

I would say that this is a prime example of when you should use inheritance. Take for example the following:

 Abstract Class Item { // This class will never be instantiated, but it can hold // things that are common to all Items, such as sale price. private int price; public int getPrice() { return price; } // etc. } Class VendorOneItem : Item { // This class gets 'price' and 'getPrice' by default, // since it inherits from Item private String color; private String model; // etc. } Class VendorTwoItem : Item { // This class also gets 'price' and 'getPrice' private Double width; private Double height; // etc. } 

I know that this does not go the way of dynamically reading data, but it is a clearer way in my mind. Even if you can somehow read all the data dynamically, how can this data be processed? As Reed noted, you might have a dictionary or map of some kind, which is reasonable. You will have data that might look like this:

 {"key": value} {"Color": "blue"} {"Price": 9.99} {"Model": 1234-567-890} 

or

 {"Price": 129.99} {"Height": 12.750} {"Width": 8.55} 

But then you just shift your problem by computing attributes at compile time to figure out the attributes at runtime. To handle these two, part of the code will know what to do with "Width" and "Height" , and he also needs to know what to do with "Model" and "Color" . Reflection is one of the possibilities, but what is described by these elements is fundamentally different, and it may be almost impossible to generalize the algorithm that concerns their details.

It is much easier for me to start with a base class and inherit classes that know the details. You can even create Item collections in your code along the way without knowing the exact type of each object.

In C ++:

 std::vector<*Item> items; items.push_back(new VendorOneItem()); items.push_back(new VendorTwoItem()); 

Now say you added this to the Item class:

 virtual public void ProcessItem()=0; //This will throw compilation errors // if all derived classes don't define ProcessItem 

So now that the items vector is defined, you can do this:

 items[0].ProcessItem(); items[1].ProcessItem(); 

And it does not matter that both objects are instances of different classes. Both of them must implement ProcessItem, so there is no problem figuring out which functions to call.

0
source

Source: https://habr.com/ru/post/1496861/


All Articles