PLEASE HELP ANSWER THIS QUESTION... I HAVE INTERVIEW THIS MORNING
Another object oriented application deals with roads. The application has a global list object containing Road objects. When the RoadNetwork class requires a particular road it searches through the global list until it finds the road name that matches. The City class also requires roads by name, and also searches through the list when it requires roads. So far these are the only accesses to the road list. A new requirement on the RoadNetwork component means that roads must be accessed much more frequently than before. The application's performance has also now become an issue. How would you address the performance issue and how would you consider improving the existing design?
[693 byte] By [
balogiza] at [2007-10-2 13:51:59]

You ought to make cache of Road objects.
For example you can to create RoadsCache class which contains a map with entries. Each entry has key and value like <String, Road>.
Be better if RoadCache and RoadList class implement the same interface, for example IRoadList.
IRoadList interface must be used in RoadNetwork and City classes instead of road list.
Thanks for taking time to reply this question. But what type of design pattern can we use to improve performance for this program considering that roads must be accessed frequently?
Maybe this is particular case of Proxy pattern :)
This is the second interview question of yours that I've read.
If you have to come here to answer these, I'd suggest that you might not be qualified for the job. What time is the interview?
Proxy pattern? I think the answer is MUCH simpler than that. A List will be O(n) to find a Road, but a HashMap will be able to do it in O(1) using the name as the key. Change data structures and lookup performance will improve, especially for large numbers of Roads.
%
Ok :)But the road list may be located remotely therefore performance of searching Road in the road list is slowly.Or do you propose load all road list to HashMap before searching ? :)
> Ok :)
>
> But the road list may be located remotely therefore
> performance of searching Road in the road list is
> slowly.
Network latency will affect both the List and Map implementations the same. You're correct - if the object that returns Roads is remote the search time will likely be a wash compared to latency.
I'm assuming that this is an in-memory operation. If it's not, your point is valid. If that's the case, I'd recommend a relational database over either data structure.
> Or do you propose load all road list to HashMap
> before searching ? :)
No, I plan to change the private implementation from a List to a Map in that RoadCache class. I'm assuming that the implementation change won't affect the API that clients see.
%
> Thanks for taking time to reply this question. > > But what type of design pattern can we use to improve> performance for this program considering that roads> must be accessed frequently?Who says it has to be a design pattern?%
> I'm assuming that this is an in-memory operation. If
> it's not, your point is valid. If that's the case,
> I'd recommend a relational database over either data
> structure.
This is worded badly. What I meant was that a remote repository would make me think of a relational database instead of a data structure.
Since Roads should persist somewhere after the application exits, I'd imagine there'd be relational database somewhere in this chain. I'd load the Map from a database on startup.
Unless Roads are very dynamic, this sounds like reference data to me. Read it in on startup and cache it at application level.
%
> I'd load the Map from a database on startup.> Read it in on startup and cache it at application level.Bu I think that preventive reading data is better than loading all map from a database.
But I think that preventive reading of data is better than loading all map from a database. (i am sorry for mistakes)
I'm not sure I'm following you here.
I'll agree that reading lots of data into memory isn't a good idea when it's so large that it swamps your app. (Bringing in millions of records isn't smart.) In that case, it needs to remain in a database.
But the original question explicitly said "in memory", so that's what I'm answering. I'll assume that it's of a appropriate size.
%