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

Asked

Viewed 442 times

1

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

  • Humnn !?!?!?!?! Give more context, maybe it would be good to show where you are getting confused. Or at least make it clearer what you’re talking about, what are you getting at? What do you expect us to answer. These things don’t seem to be connected. State is something used in various contexts. Class is class, what doubt? Timeout? Input?!?!?!

  • @bigown I rephrased my question

  • http://answall.com/q/136404/101. I don’t understand what state machine has to do with classes and interfaces. You can do that if you put it in context.

  • @bigown interfaces and states are distinct things or are equivalent?

  • 1

    These are things so different that I don’t even know what they’re asking in the same question.

  • Did my answer clear your doubts? if yes, please mark it as "Accept", otherwise re-edit your question to make it clearer what you need to know, ok?

Show 2 more comments

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 several classes implementing the 'State' interface, such as the 'State' Class, the 'State' Class, etc. 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', thus:

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 interface "Estadodosbotoes"), as we did above.

This ensures that the "Controllers" will only switch between the "Statewide open" and "Statewide open" States, and any other State we create later; thus, you define in each State the Buttons that will be displayed (and those that will not be) while the "Controllers" is in this state (possess this state).

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

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

  • A State is an Instance of a Class that implements an Interface;
  • An Interface declares methods (no body, i.e., no code inside) that a Class that implements this interface must implement (it must 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, the Classes that implement this interface should define the code for each of these methods.
  • A Class is a File where you put the code itself. To create "States" you must have an Interface that declares all methods that the "State Machine" will need to call in its Internal State, and, you must create a Class for each State making it implement ("Implements") this interface. To create a "State Machine", you must create a Class that receives and uses an Instance (a State) that implements the Interface in question, where this "State Machine" shall change its behavior according to the State it owns.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.