jeudi 12 novembre 2015

State Design Pattern "this"

So I'm working with a State Design Pattern in Java. I was hoping someone could explain the use of this in a Constructor. I understand that this is used for constructor chaining, but how does it work?

Example:

public class DegreeConstroller 
{
   private DegreeState firstState, secondState, thirdState, fourthState, curState;
   private ArrayList<myObj> objs;
   private String number, variable;

   //Assume four other states and all other instance vars. have been initialized
   public DegreeController(String number, String variable)
   {
       thirdState = new thirdState(this);  <----------------

       curState = firstState;
   }

}

  1. What is this referencing?
  2. How is this referencing that? (see #1)
  3. Why is this needed instead of something else?

All referring to the State Design Pattern

Aucun commentaire:

Enregistrer un commentaire