What is the difference between a state (referring to the State Machine) and a class?

1

I would like to know how to differentiate a state, an interface of a class, what are its main differences? How can I distinguish them?

    
asked by anonymous 05.04.2017 / 18:46

1 answer

3

State (of a State Machine):

A State can simply be an Instance of a Class that implements an Interface that we can call 'State'. You can have multiple classes implementing the 'State' interface, such as the 'StateX' class, the 'StateY' class, and so on. See:

public interface EstadoDosBotoes { //Esta é a Interface
    boolean isExibirBotaoAbrirArquivo();
    boolean isExibirBotaoSalvarArquivo();
    boolean isExibirBotaoNovoArquivo();
    boolean isExibirBotaoFecharArquivo();
}
public class EstadoSemArquivoAberto implements EstadoDosBotoes { //Classe que implementa a interface
   public boolean isExibirBotaoAbrirArquivo() {return true;}
   public boolean isExibirBotaoSalvarArquivo() {return false;}
   public boolean isExibirBotaoNovoArquivo() {return true;}
   public boolean isExibirBotaoFecharArquivo() {return false;}
}
public class EstadoComArquivoAberto implements EstadoDosBotoes { //Outra Classe que implementa a interface
   public boolean isExibirBotaoAbrirArquivo() {return false;}
   public boolean isExibirBotaoSalvarArquivo() {return true;}
   public boolean isExibirBotaoNovoArquivo() {return false;}
   public boolean isExibirBotaoFecharArquivo() {return true;}
}

State machine:

A 'State Machine' can simply be an Instance of a Class that has a 'State', like this:

public class ControladorDosBotoes {//Esta é nossa "Máquina de Estados"
    private EstadoDosBotoes estadoAtualDosBotoes = new EstadoSemArquivoAberto();
    public void setEstado(EstadoDosBotoes novoEstado) {estadoAtualDosBotoes = novoEstado;}
}

We create "State Machines" when we have some Object that needs to switch between certain specific states during execution, for each possible State you must create a Class that implements the interface that the "State Machine" accepts (in our case, the "StatusBoxes" interface), as we did above.

This ensures that the "ControllerBoxes" will only toggle between the "OpenFileState" and "OpenFileState" States, and any other State that we create later; Thus, you define in each state the Buttons that will be displayed (and those that will not be) while the "ControllerBoxes" is in this State.

The "State Machine" needs to use the "State" it currently has, behaving differently for each "State" you place on it; this can require that each "State" has one or more methods (overlays of the "State" Interface) that the "State Machine" will call for them to do something. This already looks like the Standard Strategy because you can change it - even during execution! - The code that your Strategy Client (your State Machine) will execute simply by changing the Strategy (the "State") that is within it.

Differences between the State (of a State Machine), Interface and Class:

  • A State is an Instance of a Class that implements an Interface;
  • An interface declares methods (without a body, that is, without a code inside) that a Class that implements this interface should implement (should add the code to the method declared in the interface). That is, the Interface defines the return and input parameters of each method, as well as their names, and Classes that implement this interface should define the code for each of these methods.
  • A Class is a File where you will place the code itself. To create "States" you must have an Interface that declares all the methods that the "States Machine" will need to call in its internal State, and, it must create a Class for each State making it implement ( "implements" / em>) this Interface. To create a "State Machine", you must create a Class that you receive and use an Instance (a State) that implements the Interface in question, where this State Machine should change its behavior according to the State that you have .
05.04.2017 / 21:26