Click here to Skip to main content
15,899,679 members
Home / Discussions / C#
   

C#

 
GeneralRe: Get file that spawned the application. Pin
cmarcus6-Jun-06 5:58
cmarcus6-Jun-06 5:58 
AnswerRe: Get file that spawned the application. Pin
J4amieC6-Jun-06 4:11
J4amieC6-Jun-06 4:11 
AnswerRe: Get file that spawned the application - New Problem Pin
cmarcus6-Jun-06 8:44
cmarcus6-Jun-06 8:44 
QuestionShared Add-in for Excel 2003 and Garbage Collector Pin
Kadur6-Jun-06 3:55
Kadur6-Jun-06 3:55 
QuestionObject lifetime, GC and the State Pattern [modified] Pin
User 66586-Jun-06 3:54
User 66586-Jun-06 3:54 
AnswerRe: Object lifetime, GC and the State Pattern [modified] Pin
Stephan Samuel6-Jun-06 4:03
Stephan Samuel6-Jun-06 4:03 
GeneralRe: Object lifetime, GC and the State Pattern [modified] Pin
User 66586-Jun-06 4:07
User 66586-Jun-06 4:07 
GeneralRe: Object lifetime, GC and the State Pattern Pin
Stephan Samuel6-Jun-06 6:53
Stephan Samuel6-Jun-06 6:53 
You should keep your states small. Structs won't work because they don't implement inheritance, but try to keep the information inside your concrete state classes to a minimum. I would avoid the singleton pattern for states.

Currently, your states don't really switch. Each tunnels through to the next. There should probably be one top-level controller (it may be a singleton but may not need to be) that decides which state you're in and runs the states:

(Please pardon any typos or syntax errors; I'm making this up as I go.)

<br />
public class MyController<br />
{<br />
State currentState;<br />
<br />
public void RunMe()<br />
{<br />
while (this.currentState != null)<br />
  this.currentState = this.currentState.RunMe();<br />
}<br />
}<br />
<br />
public abstract class State<br />
{<br />
public abstract State RunMe();<br />
}<br />
<br />
public class ConcreteState1()<br />
{<br />
public RunMe()<br />
{<br />
// do things.<br />
return new ConcreteState2();<br />
}<br />
}<br />
<br />
public class ConcreteState2()<br />
{<br />
public RunMe()<br />
{<br />
// do things.<br />
return null;<br />
}<br />
}<br />


Likely, you'll want to pass around some objects, and your states will pass out different "next state" objects as RunMe() returns based on what those objects look like. Each state lives only as long as it's executing, so there's no memory overhead. If you're worried about setup/tear-down performance of your state objects (e.g. -- if they have a complicated DB open process and you need them to run really quickly with persistent connections), consider some level of factory for the states: either have a connection factory, give each state its own factory and let it decide when to create a new one of itself, or create a factory class (or implement one in MyController) that decides the lifetime of your state objects.

I'd also implement IDisposable and the C# disposal pattern for each of your state objects to clear up unmanaged resources. Managed resources should get cleaned up automatically.

The other way to implement this is to have MyController decide which state is next based on the state of some object. My personal belief is that method isn't as good. It's less OO: you might as well just code all the possibilities within the controller and forget the states.
GeneralRe: Object lifetime, GC and the State Pattern Pin
User 66586-Jun-06 7:51
User 66586-Jun-06 7:51 
AnswerRe: Object lifetime, GC and the State Pattern [modified] Pin
Guffa6-Jun-06 4:23
Guffa6-Jun-06 4:23 
GeneralRe: Object lifetime, GC and the State Pattern [modified] Pin
User 66586-Jun-06 4:28
User 66586-Jun-06 4:28 
AnswerRe: Object lifetime, GC and the State Pattern [modified] Pin
Guffa6-Jun-06 8:50
Guffa6-Jun-06 8:50 
QuestionString manipulation performance issue Pin
sjembek6-Jun-06 3:37
sjembek6-Jun-06 3:37 
AnswerRe: String manipulation performance issue [modified] Pin
sathish s6-Jun-06 3:48
sathish s6-Jun-06 3:48 
GeneralRe: String manipulation performance issue [modified] Pin
sjembek6-Jun-06 4:04
sjembek6-Jun-06 4:04 
AnswerRe: String manipulation performance issue Pin
Stefan Troschuetz6-Jun-06 3:50
Stefan Troschuetz6-Jun-06 3:50 
AnswerRe: String manipulation performance issue [modified] Pin
Guffa6-Jun-06 5:20
Guffa6-Jun-06 5:20 
QuestionControlled Focus event - textBox Pin
conrado76-Jun-06 3:26
conrado76-Jun-06 3:26 
AnswerRe: Controlled Focus event - textBox Pin
NaNg152416-Jun-06 3:34
NaNg152416-Jun-06 3:34 
GeneralRe: Controlled Focus event - textBox Pin
conrado76-Jun-06 3:45
conrado76-Jun-06 3:45 
QuestionException Pin
sanmech6-Jun-06 3:25
sanmech6-Jun-06 3:25 
QuestionDrag&Drop listview subitem? Pin
Dominik Reichl6-Jun-06 3:16
Dominik Reichl6-Jun-06 3:16 
Questionconnected to the internet or not ?? Pin
Tamimi - Code6-Jun-06 3:10
Tamimi - Code6-Jun-06 3:10 
AnswerRe: connected to the internet or not ?? Pin
stancrm6-Jun-06 3:31
stancrm6-Jun-06 3:31 
GeneralRe: connected to the internet or not ?? Pin
Tamimi - Code6-Jun-06 3:47
Tamimi - Code6-Jun-06 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.