# I want a nice design pattern for “All”, “many” or “one”

In my particular usage here, I'm using two functions to deal with one thing.

Lets say the user request comes in to select "ALL" the records, in which case I would pass on some code such as:

function getRecords() {
return some_sql("Select * from whatever WHERE id > 0;");
}


But then I want to run just one record, I'd do

function getRecords(SomeID) {
return some_sql("Select * from whatever WHERE id = SomeID;");
}


And if I wanted to get a specific list of records, I'd do something like

function getRecordsByID(someArray(1,2,3,10,11,99)) {
var someIDs = "";
for (i in someArray) {
someIDs = "(" + someArray.join(",")  + ")";
}
return some_sql("Select * from whatever WHERE id IN SomeIDs;");
}


It dawns on me that I could of course use getRecordByID everywhere I've used getRecord, but I can't do this for getRecords.

Is there a common or overlooked design pattern I'm missing here? My first guess would be something like checking the argument's contents for a valid array and running based on that, but I'd like to see a better option.

Why would you want a single method to do three logically different things? Each method you make should really only perform one task. What's the problem in just having the consumer choose the method of selection based upon their need, instead of having a one method fits all sort of thing? If having three methods really isn't what you want, perhaps you could pass a Command pattern object to a database object, to execute the desired functionality?

• Because logically they're all the same thing. Getting record(s) – Incognito Jun 3 '11 at 18:51
• @user2905 While they're all getting records, I wouldn't say they're all the same thing since they clearly imply returning different results. Since they all share the common purpose of "getting records [from a database]", I would keep all methods in the same class. – bitsoflogic Jun 3 '11 at 20:36
• @user2905 For example, would getRecordsForTeenagers() also be the same thing? – bitsoflogic Jun 3 '11 at 20:38
• Why couldn't I return an array of records? – Incognito Jun 3 '11 at 20:40
• I think you could make a cohesive class for 'getting records', but at method level, you'd distinguish between the pieces of functionality with the criterion you query with to get the specific records. Returning a collection (array, list, map, whatever) of records is a good idea if your method reads as getRecords, but if as the consumer of the code, you know you only want a single record, it could become misleading. – RobB Jun 4 '11 at 16:18

While what you have can be done by reusing some code, it is always better to call the database once as opposed to calling it multiple times.

What you could do is have a helper function

function getRecordsByCondition(condition) {
return some_sql("Select * from whatever WHERE "+condition+";");
}


then call it from the other functions.

Also getRecord can be implemented as a special case of getRecordsByID with an array of size 1.

Just a suggestion:

function getAllRecords();
function getRecords(int[] recordIds){...}