lundi 2 janvier 2023

Design pattern for creating a class with multiple methods which should be executed in an order

I am trying to create a template class for testing Room migrations. The steps which should be followed for testing a Room migration are as follows.

  1. Create a database in the previous version.
  2. user migration helper to migrate to the next version.
  3. insert some data.
  4. verify the integrity of that data.

Now I have created a class MigrationHelper which implements all this methods accordingly.

class MigrationHelper {

 fun createDatabase(version : Int) : MigrationHelper{
       initialDatabase = migrationTestHelper.createDatabase(TEST_DB_NAME , version)
       return this
 }

 fun insertData(db : SupportSQLiteDatabase){
   ... 
 }

}

Now for this class I am currently using the builder method so that the for writing tests developers can call methods in a clean and understandable way.

    @Test
    fun runMigration_78_79(){
        migrationHelper.setUp("test_db")
            .addTablesInvolved("packs")
            .createDatabaseWithData(78)
            .addMigrations(MIGRATION_77_78,MIGRATION_78_79)
            .runMigration(79)
            .cleanOut()

    }

But since builder pattern , any method can be called in any order . This is not good for my implementation , since some methods are neccessary to be called in an order . for eg : Can call the 'runMigration' method before the 'addMigration' method, Since for running the migration you need to add an array of all the migrations involved.

What would be the best design pattern for this kind of situation ? I understand I might be over complicating , I could just create class with a bunch of methods and call them individually . but I would like to have a clean approach. Thank you.

Aucun commentaire:

Enregistrer un commentaire