Jump to content

ExchangeContext or pass Script instance


Recommended Posts

Posted (edited)

Suppose I have these three classes:

Main (exchangesContext with BankTask), BankTask , Data (Data class contains methods which require api access)

Let's say the BankTask needs to use some methods from the Data class. I realised that I need to call exchangeContext inside BankTask aswell but there's obviously no onStart method there.

Should I let the Data class accept MethodProvider in its constructor instead or is it possible or even preferable to use exchangeContext in such cases?

Edited by Jammer
Posted (edited)
56 minutes ago, Jammer said:

Suppose I have these three classes:

Main (exchangesContext with BankTask), BankTask , Data (Data class contains methods which require api access)

Let's say the BankTask needs to use some methods from the Data class. I realised that I need to call exchangeContext inside BankTask aswell but there's obviously no onStart method there.

Should I let the Data class accept MethodProvider in its constructor instead or is it possible or even preferable to use exchangeContext in such cases?

 

You could always override the exchangeContext method to also exchangeContext with instances within your class:

public class BankTask extends Task {

    SomeOtherClassThatNeedsApi someOtherClass = new SomeOtherClassThatNeedsApi();
  
    @Override
    public MethodProvider exchangeContext(Bot bot) {
        super.exchangeContext(bot);
        someOtherClass.exchangeContext(bot);
        return this;
    }
  
    // ... Blah blah whatever
}

 

Or yes just pass MethodProvider as a parameter to the constructor of your other class.

Or just add an onStart method to your base Task class and call it once before executing a Task. This may or may not be appropriate depending on what your "Task" pattern is.

Edited by Explv
  • Like 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...