Question: I got nearly 10 functions in class having similar pattern like following function

```
    SQLiteDatabase database = this.getWritableDatabase();

    try {
          //Some different code , all other code(try,catch,finally) is same in all functions
    } catch (SQLiteException e) {
        Log.e(this.getClass().getName(), e.getMessage());
        return false;
    } finally {
        database.close();
    }

}

```

I want to remove that common code from all functions (try ,catch , finally) and move it to a single place
How can I achieve this?

Answer: 
There are a number of frameworks out there that drastically simplify database interaction that you can use, but if you want to do things on your own, and are interested in the Java way to do things like this, here's the idea:

Make your "executor" like so:

```
public class Executor {
  public static void runOperation(Operation operation) {
    SQLiteDatabase database = this.getWritableDatabase();
    try {
      operation.run(database);
    } catch (SQLiteException e) {
      Log.e(operation.getClass().getName(), e.getMessage());
      return false;
    } finally {
      database.close();
  }
}

```

Now each of the 10 things you want to do will be operations:

```
public interface Operation {
  void run(SQLiteDatabase database) throws SQLiteException;
}

```

Here is what a particular operation would look like:

```
Operation increaseSalary = new Operation() {
  public void run(SQLiteDatabase database) throws SQLiteException {
    // .... write the new increased salary to the database
  }
};

```

And you run it with:

```
.
.
Executor.runOperation(increaseSalary);
.
.

```

You can also make the implementation of the interface an anonymous inner class, but that may make it a little less readable.

```
.
.
Executor.runOperation(new Operation() {
  public void run(SQLiteDatabase database) throws SQLiteException {
    // put the increase salary database-code in here
  }
});
.
.

```

You can look through a list of classic Design Patterns to find out which one this is.
**To expand further on [Ray Toal's original answer](https://stackoverflow.com/a/20931708/1134080),** it is worth noting that using anonymous inner class will help avoid creating a separate class file for each operation. So the original class with 10 or so functions can remain the same way, except being refactored to use the `Executor` pattern.

Also, when using the `Executor` pattern, you have to take care of the usage of `this` in the original code. Assume the original functions are as follows:

```
public boolean operation1() {
    SQLiteDatabase database = this.getWritableDatabase();

    try {
        //Code for Operation 1
        return true;
    } catch (SQLiteException e) {
        Log.e(this.getClass().getName(), e.getMessage());
        return false;
    } finally {
        database.close();
    }

}

public boolean operation2() {
    SQLiteDatabase database = this.getWritableDatabase();

    try {
        //Code for Operation 2
        return true;
    } catch (SQLiteException e) {
        Log.e(this.getClass().getName(), e.getMessage());
        return false;
    } finally {
        database.close();
    }

}

```

With the `Executor` class defined as follows:

```
public class Executor {
  public static boolean runOperation(SQLiteOpenHelper helper, Operation operation) {
    SQLiteDatabase database = helper.getWritableDatabase();
    try {
      operation.run(database);
      return true;
    } catch (SQLiteException e) {
      Log.e(helper.getClass().getName(), e.getMessage());
      return false;
    } finally {
      database.close();
    }
  }
}

```

And the `Operation` interface defined as follows:

```
public interface Operation {
    public void run(SQLiteDatabase database) throws SQLiteException;
}

```

The original functions can now be rewritten as follows:

```
public boolean operation1() {
    return Executor.runOperation(this, new Operation() {
      public void run(SQLiteDatabase database) throws SQLiteException {
        //Code for Operation 1
      }
    });
}

public boolean operation2() {
    return Executor.runOperation(this, new Operation() {
      public void run(SQLiteDatabase database) throws SQLiteException {
        //Code for Operation 2
      }
    });
}

```

*This expansion also corrects mistakes Ray has overlooked in his original answer.*