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?
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?
3
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;}
}
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).
Browser other questions tagged java classes
You are not signed in. Login or sign up in order to post.
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?!?!?!
– Maniero
@bigown I rephrased my question
– jorge saraiva
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.
– Maniero
@bigown interfaces and states are distinct things or are equivalent?
– jorge saraiva
These are things so different that I don’t even know what they’re asking in the same question.
– Maniero
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?
– Douglas
Related: In which context the state machine pattern is indicated?
– Piovezan