开发者

Design pattern import files alters behaviour

开发者 https://www.devze.com 2023-04-09 03:23 出处:网络
I am developing an applica开发者_开发问答tion where the program can do a number of operations. It relies on a XML file being imported and DB connection established. However, some of the functions can

I am developing an applica开发者_开发问答tion where the program can do a number of operations. It relies on a XML file being imported and DB connection established. However, some of the functions can work without an xml file imported, and some can work only if the XML is imported or only if the DB is connected.

So, my question is what design pattern I should use in order to model this ? I read about the state pattern where an object's behaviour changes relative to the current state. Is this a good way of doing it ? For example, I can have several states : XML_FILE_IMPORTED_ONLY, DB_CONNECTED_ONLY, XML_IMPORTRED_AND_DB_CONNECTED, NOTHING_IMPORTED and based on the current state of the object relevant functions will be available ?

Any help will be much appreciated.

Regards,

Petar


You have two state machines, each controlling a portion of your overall state. Each state machine will perform transitions independent of the other.

XML Imported Statemachine

Initial state: Not Imported

  1. Not Imported. transitions: "import happens" -> Imported.
  2. Imported. transitions: "unload" -> Not Imported.

DB Connection Statemachine

Initial State: Not Connected

  1. Not Connected. transitions: "connect succeeded" -> Connected.
  2. Connected. transitions: "disconnect" -> Not Connected.

Edit: State machines are overkill for this problem. The state machines in question each have two states with one transition in each direction. A much better way to represent this situation would be to use a boolean variable.

boolean dbConnected;
boolean xmlImported;
0

精彩评论

暂无评论...
验证码 换一张
取 消