hey i have a difficult question.
class DatabaseHelper { Database db; String defaultShema; public DatabaseHelper(Database db, String defaultSheme) { this.db = db; this.defaultShema = defaultShema; } public void databaseOperation1(String selectData, String FromData, String whereData) { // uses db and defaultShema } public void databaseOperation2(Database db, String shema, String selectData, String FromData, String whereData) { // uses parameter db and parameter shema } }
if i use operation 1 then i use the field variables db und defaultShema
But what should i do if i have a special case; a case where i dont want to use the defaultShema or the default db.
for that case i must create a new DatabaseHelper.
And if i want to call my operation method 1000 times with different db/defaultShema variables each, then i have to instance 1000 temporary DatabaseHelper objects.
like this pattern
new object(...).doAction(); //after that delete
So i have operation2, but this one uses lesser till nothing state from the sourounding class. Here i dont have to create thousands of senseless instances of DatabaseHelper. But maybe this method could also exists as a static method, if BOTH db and defaultSchema is used from the method-parameter-set. But maybe i want only use one of them from the parameter-set the other one from the field-set of the class.
So here my question, when should i make variables to fields and when to method-parameters? Maybe in most of the cases the selectData is identical. So i would make that parameter to a field.
But there i have the problem that other methods, for example insert-methods dont need that field -> unncecessary internal class dependancy.
i used here a example with the Database. But i guess that problem is very common with other classes like this.
If i use always operation 2, then i dont need classes anymore -> i only have static methods.
But if i use opertion 1 like methods, then i have to decide which variables i have to make to fields. But which?
And that one i make to fields, in some cases, i dont want to use them in default mode; so i want to change them
But i guess the following is nonsense:
String tmp=db.getDefaultSchema(); db.changeStateOfDefaultSchema("XYZ"); db.databaseOperation1(...); db.changeStateOfDefaultSchema(tmp); //OR new DatabaseHelper(..., "XYZ").databaseOperation1(...); //GC -> delete of that tmp DatabaseHelper
So how should i design my classes? Which variables should i make to fields and which one to method parameter What should i do if i want to have the flexibilty to change later each of that variables; if i dont know yet, which variables the client have to change often and which one not so often.