开发者

How do I perform an operation on many instances of an object

Say I have an object for U.S. state, and I want to perform a batch update on some attribute of that object, say census data. I see two options:

  • have a separate object that performs batch operations and finds and loops through all instances of the states, updating each one

  • some kind of function like State.new.parse_census which would not have any state information but could go through and update the database.

Sorry this is such a newb question. I'm assuming the former is cleaner and correct, but I want to make sure I am not making a design mistake wit开发者_如何学编程h that assumption. Thanks..


Standard method is to make a method within the class USState like so

public void updateCensus(...)
{
   //do stuff to internal data
}

then whatever is housing all the instances of this class just loops through

public class Houser
{
    ArrayList<USState> list;

    public void foo()
    {
        for(USState state : list)
        { 
           state.updateCensus(...)
        }
    }
}

The idea here is that you design your class to manage its own internals. This way, it is more maintainable, readable, and outside classes do not need to be exposed to the underlying structure of your object to interact with it appropriately.


The probably "cleanest" way of doing this would be to have the state object own a container (e.g. std::vector<censusdata>, assuming C++) and use std::foreach() to iterate over the elements.

Depending on the size of the elements and on what container you choose, this will be reasonably efficient too.

If performance is crucial, on the other hand, it is better to break with OOP and store each field of censusdata in a separate contiguous container. That way, when iterating over it, caches will work much more efficiently. It's quite non-pretty, though.


It may indicate your class structure needs refactoring

If you have a load of objects that all have a particular aspect that change at once, it may be that this part should be refactored out into its own class which all your objects point to.

So you have a list of State classes and you need to update the tax rate paid for each state. Now (I dont know if this is true in the US) every state needs to pay the government the same rate of tax. So rather than looping through each state updating their tax rate, you should probably have a separate TaxRate class that all states point to.

Then to update, you just need to update the TaxRate object in one place and all classes get updated.


You wrote "database", so I assume you are talking about persistent objects stored in a (relational?) database, right? In this case, your options depend heavily on how your architecture looks like. If you are using an object-relational mapper, for example, it may provide already some kind of navigational tools for the purpose. And if you have to update a million of objects, it might be a good idea to bypass the ORM mapper and send a single update SQL to your database which does the job, if that's possible.


If you are working in C#, a LINQ query would be the way to go.

 var states = new List<USState>; 

// ...initialize "states" here

// Update census in each state
states.Select( state => state.UpdateCensus(...) );

The "Select" method will iterate through every state, and call the state's UpdateCensus method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜