Question

In: Computer Science

Sometimes when I'm working with a class (usually one that represents a database model), I'll have...

Sometimes when I'm working with a class (usually one that represents a database model), I'll have about 15 different properties on it, usually of different types. These are properties that need to be accessed by the users, as well, so each property will typically need some function like

T getPropertyName();
setPropertyName(T value);

But, it's also extremely valuable for me to be able to iterate through each property in a particular order(although, I suppose that "value" may stem from laziness of not having to type each one individually). Is there some logical way to set up this class, so that I could iterate through it in this order? Or am I helpless for organization, and I should just go back to the basics of having 15 private variables, and 15 "getters" and "setters"?

As an aside, currently I'm working with Android and Java, so answers pertaining specifically to that would be the most helpful, but seeing how I encounter this somewhat frequently in other languages, too, any solution would be ideal.

Solutions

Expert Solution

If you don't mind a bit of repetition if it means avoiding "magic"... Use a nested class (non-static) that implements java.util.Map<String,Object>. This provides a uniform way for outside code to enumerate a set of properties you choose, while keeping the implementation details (ex. reflection versus a bunch of if/then clauses) hidden.

public class Product {
private String productName;
private int productPrice;
private PropMap props = new PropMap();

class PropMap implements Map<String,Object>{
/* Reflection, or if-then statements, etc.
Access outer items ex: this.Product.productName

Throw exceptions if someone tries to store the wrong
kind of item.

keySet() and `iterator()` will need to return implementations
that provide things in an appropriate order.
*/
}

public PropMap getPropMap(){
return props;
}
}

So external code could go:

Map<String,Object> m = myobj.getPropMap();
for(m.keySet(): String k):
Object v = m.get(k);
}
It's also possible to refactor PropMap into its own class, ProductPropMap, but then it wouldn't have access to private items.


Related Solutions

I'm using livesql for my database class to try and do these A medical clinic has...
I'm using livesql for my database class to try and do these A medical clinic has doctors, along with patients assigned to one of the doctors. Create 2 tables that include fields below with primary key chosen below. Use appropriate data types and simplified field names, eg: - Doctor’s Table: DKey ID           Last name First name Home phone Medical specialty Monthly Salary Patient’s Table: PKey ID           Last name First name Address (Use Address as column name)...
Hi guys, I'm working on an assignment for my Java II class and the narrative for...
Hi guys, I'm working on an assignment for my Java II class and the narrative for this week's program has me a bit lost. I've put the narrative of the assignment in the quotation marks below. " Included with this assignment is an encrypted text file. A simple Caesar cipher was used to encrypt the data. You are to create a program which will look at the frequency analysis of the letters (upper and lowercase) to help “crack” the code...
Write a Bottle class. The Bottle will have one private int that represents the countable value...
Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString() method will be given in class). All add, subtract, multiply...
I'm working on a lab that's the Ziegler Natta Catalytic Formation of Polyethylene and at one...
I'm working on a lab that's the Ziegler Natta Catalytic Formation of Polyethylene and at one point in the lab the lab manual says to: "Turn off the ethylene and turn on the nitrogen. Add 95% ethanol (100ml) to the addition funnel and add this dropwise to the reaction mixture. One of the questions is to "think of a reason why we flush with nitrogen before the addition of ethanol" and another is "why do we add ethanol? Why not...
Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
I'm looking for a excercise of these topic. If any one have one of these topic...
I'm looking for a excercise of these topic. If any one have one of these topic example please comment ... Chapter 6 1) Pump Specific Speed and Similarity 2) Cavitation in Water Pump 3) Selection of Pump 4) Pump in Parallel or in Series 5) Power of Reaction Turbine, relate to equation 6.36 to 6.39 6) Turbine Laws and Specific Speed 7) Cavitation in Turbine 8) Selection of Turbine ... Chapter 7 1) Surface Drainage 2) Highway Drainage 3) Subsurface...
Discussion Database Security and UML Often, systems have only one level of security when it comes...
Discussion Database Security and UML Often, systems have only one level of security when it comes to databases. Imagine that someone is trying to add a name (which should be composed only of letters) to a database. The user mistakenly hits a number, and the system rejects the entry. Such security can be implemented at the database management system (DBMS) or the application level. Where would you apply security? Justify your choice. What are some alternative practices or standards of...
I have a table to fill out for a Healthcare Finance class and I'm really struggling...
I have a table to fill out for a Healthcare Finance class and I'm really struggling with the TOTAL UNITS column of the first table and the other colums of the tables after that. Help? Activity Annual Costs Total Units1 Unit Alloc Rate Cost Driver Test A Test B Test C Test D Receive specimen 10,000 5000 Tests 2.00 No. Tests 2,000 1,500 1,000 500 Equipment setup 25,000 Minutes 8.33 Minutes per test 5 5 10 10 Run test 100,000...
Hello! I'm trying to work on a python lab in my class, and we basically have...
Hello! I'm trying to work on a python lab in my class, and we basically have to make this method play_interactive Now for the fun bit. The function play_interactive will take just one argument --- the length of patterns to use as keys in the dictionary --- and will start an interactive game session, reading either '1' or '2' from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session...
I'm working on a post against a recent creationism article about blue stragglers. From when I...
I'm working on a post against a recent creationism article about blue stragglers. From when I was in undergrad, the general explanation was that they were likely second generation stars within globular clusters. More recent work seems to indicate that it's likely due to binary interactions with a sun-like star siphoning material off a companion to become a recent and short-lived blue supergiant. Since this is way outside my field, I thought I'd try my luck here and get a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT