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...
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...
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...
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...
a. Class, sometimes, companies are too large to break up. Many of these companies have exclusive...
a. Class, sometimes, companies are too large to break up. Many of these companies have exclusive access to resources that make the introduction of competition impossible. No more is this more apparent than in Canada’s oil sands. Class, please provide some recent statistics on this industry, such as revenues and profit levels. You can do this by researching news articles. Be sure to source where you got the information from. b. One of the issues surrounding the oil sands is...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields Item Name Item Serial No Item Unit Price Item Stock Level Item Reorder Level It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level. b) Extend the Retail Item class of part a) above...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields  Item Name  Item Serial No  Item Unit Price  Item Stock Level  Item Reorder Level  It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.    b) Extend...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT