0

Good day everybody! Here's my question:

I need to make a Tutorial for my app. For doing this, I've created a class called TutorialClass which contains some methods that I need to call from several other classes. The working flow is quite like this:

Class 1:

//...
if(Tutorial.tutorialStep==Tutorial.TUTORIAL_STEP1){
    Tutorial.TutorialStep1();
    Tutorial.tutorialStep=Tutorial.TUTORIAL_STEP2;
}

Class 2:

//...
if(Tutorial.tutorialStep==Tutorial.TUTORIAL_STEP2){
    Tutorial.TutorialStep2();
    Tutorial.tutorialStep=Tutorial.TUTORIAL_STEP3;
}

And so on... All the classes I use, have not to extend Activity necessarily

You can find this piece of code in several class I use. So, first of all I need to create an instance of TutorialClass

TutorialClass Tutorial = null;

So here is the question: how can I use this instance from all the classes in which I have to show my tutorial? As you can see, the value of tutorialStep has to be visible from all classes, and all classes have to see that value or change it, in order to let the tutorial go on.

Here is the code of my tutorial class:

public class TutorialClass{

    Context context;
    public static int tutorialStep;

    final int TUTORIAL_STEP1=1;
    final int TUTORIAL_STEP2=2;
    final int TUTORIAL_STEP3=3;
    //...

    TutorialClass(Context context){
        this.context = context;
    }

    public void Tutorial1() { ... }
    public void Tutorial2() { ... }
    public void Tutorial3() { ... }
    //...

}

I've read that exist a Singleton class that allows to reach my objective, but I've noticed that it's not the best solution. Do you have any solution? Thanks to all!

Apokalos
  • 124
  • 1
  • 2
  • 13
  • 2
    Possible duplicate of [What is an efficient way to implement a singleton pattern in Java?](http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java) – GAlexMES Mar 10 '17 at 14:06
  • https://en.wikipedia.org/wiki/Software_design_pattern#Classification_and_list . Choose the one you want. You'll probably end up with `Singleton`. – R. Zagórski Mar 10 '17 at 14:09
  • What are your arguments against using Singleton pattern? Why is it "not the best solution"? – Dmitry Mina Mar 10 '17 at 14:10
  • I've never used Singleton classe because I've never needed it. So I was just looking for a solution to this problem and I end up with this: http://stackoverflow.com/questions/1300655/whats-alternative-to-singleton – Apokalos Mar 10 '17 at 15:12

1 Answers1

1

you need to create another class that return instance of the TutorialClass

public class TutorielInstance {
    private static TutorialClass instance;
    private static Context context;

    public static synchronized TutorialClass getInstance(){
        if(instance==null){
            instance=new TutorialClass(context);
        }
        return instance;
    }


    public  static  void setContext(Context c){
        context=c;
    }
}

and then in the activity you can use

TutorielInstance.setContext(this);
TutorialClass tutorialClass=TutorielInstance.getInstance();