读书人

Java设计方式之状态模式

发布时间: 2012-09-14 23:00:49 作者: rapoo

Java设计模式之状态模式

STATE?(Object Behavioral)?
Purpose?
Ties object circumstances to its behavior, allowing the object?
to behave in different ways based upon its internal state.?
Use When?
??? 1 The behavior of an object should be influenced by its state.?
??? 2 Complex conditions tie object behavior to its state.?
??? 3 Transitions between states need to be explicit.?
Example?
An email object can have various states, all of which will?
change how the object handles different functions. If the state?
is “not sent” then the call to send() is going to send the message?
while a call to recallMessage() will either throw an error or do?
nothing. However, if the state is “sent” then the call to send()?
would either throw an error or do nothing while the call to?
recallMessage() would attempt to send a recall notification?
to recipients. To avoid conditional statements in most or all?
methods there would be multiple state objects that handle the?
implementation with respect to their particular state. The calls?
within the Email object would then be delegated down to the?
appropriate state object for handling.?
new?ConcreteStateA());??

  • ????????context.request();??
  • ????}??
  • ??????
  • } ?

  • 读书人网 >软件开发

    热点推荐