Java / Substation Design Structure

I am coding in Java and I am using a state sample template, as in this example: http://onjavahell.blogspot.fr/2009/05/simple-example-of-state-design-pattern.html

However, the UML I got is this:

+---------+ +---------+ | Context |--------| State | +---------+ +---------+ | +----------------+ | | +---------+ +---------+ | State 1 | | State 2 | +---------+ +---------+ | +----------------+ | | +----------+ +----------+ | State 1A | | State 1B | +----------+ +----------+ 

I have subordinates. How to do it? Should I make an abstract class State 1 or is there another way? Search I did not find such an example.

+4
source share
2 answers

What is the meaning of "sub-state"? There is nothing in the state template that could be called a “sub-state”. "State" represents the state of an object. Every state is a state. If you do not give a reasonable explanation of what you are trying to achieve, I believe that you are thinking about something wrong.

As long as all states have the same base class / interface, it will work. You may have an inheritance relationship between states, they will still work and have nothing to do with the state template. And even you have inheritance relationships, they are still “states” and not “sub-states”

+3
source

As always, it's hard to say without additional information about the domain you are trying to simulate. However, you can find the main cases:

  • So that your states do not overlap and respond to the IS-A principle ( here you can find more detailed information and discussions about this). In this case, State1A and State1B are ok as subclasses of State1 .
  • That your states can be combined, so it makes sense to be in State1 variants A and B and in State2 , variants A and B. Although this is not an ordinary case, this sometimes happens, and this means that your object actually has two types states. How to deal with this, a lot depends on the interaction between the states; if they are independent, then you can model them as separate hierarchies. If they depend on their behavior, then the State class, in turn, can have an internal state of A and B

As I said, the second case is rare, but in some projects this happens.

NTN

0
source

Source: https://habr.com/ru/post/1445411/


All Articles