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?
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?
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;}
}
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.