mvc and state pattern

View: New views
7 Messages — Rating Filter:   Alert me  

mvc and state pattern

by Stefan Vandermeulen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi list,

long time since I've been here.

I'm struggling with something here and i know somebody on this list has an answer :)

How can the mvc pattern and the state pattern be combined in a useful way?
should the model act as a state machine?
should the state machine sit in between a controller and the model?

maybe a better question: should mvc and state pattern be combined? :)

grtz
s

_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org

Re: mvc and state pattern

by Martin Wood-Mitrovski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Stefan Vandermeulen wrote:

> Hi list,
>
> long time since I've been here.
>
> I'm struggling with something here and i know somebody on this list has
> an answer :)
>
> How can the mvc pattern and the state pattern be combined in a useful way?
> should the model act as a state machine?
> should the state machine sit in between a controller and the model?
>
> maybe a better question: should mvc and state pattern be combined? :)

for me the real question is always : what problem are you trying to solve?

Its quite hard (for me at least) to suggest how to create something without
knowing what the purpose is. Really, any part of the MVC triad could use an
implementation of the state pattern if needed..


_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org

Re: mvc and state pattern

by Stefan Vandermeulen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi Martin,

i'm just trying to find a clean way to build in application flow.
now i'm building a video player and using mvc

the players flow:

do some checks on video ( countdown, geolock, etc.. )
display image
start preroll ( flv or swf )
display image or continue
start video
start postroll ( flv or swf )

each state has it's own behavior ( play/pause/stop etc... witch buttons to enable/disable )
of course i can use a switch statement somewhere in my playbackmodel but i would like a cleaner more elegant solution
a solution that can be used in other projects too ( as an architecture )

grt
s

Martin Wood-Mitrovski schreef:
Stefan Vandermeulen wrote:
  
Hi list,

long time since I've been here.

I'm struggling with something here and i know somebody on this list has 
an answer :)

How can the mvc pattern and the state pattern be combined in a useful way?
should the model act as a state machine?
should the state machine sit in between a controller and the model?

maybe a better question: should mvc and state pattern be combined? :)
    

for me the real question is always : what problem are you trying to solve?

Its quite hard (for me at least) to suggest how to create something without 
knowing what the purpose is. Really, any part of the MVC triad could use an 
implementation of the state pattern if needed..


_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org

__________ NOD32 3178 (20080611) Informatie __________

Dit bericht is gecontroleerd door het NOD32 Antivirus Systeem.
http://www.nod32.nl



  


_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org

Re: mvc and state pattern

by Martin Wood-Mitrovski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

to me that sounds like the state part is the 'VideoController'

i.e. the part that handles user interaction from the view.

I guess one solution is to have a single 'VideoController' class which delegates
input to its current state.

e.g.

VideoController :

// This holds the controller which handles the current state
// of the 'transport' i.e. play / stop etc..
private var transportState:ITransportControllerState;

private function playClicked(event:BasicEvent)
{
        transportState.playClicked();
}

----

Then you can choose how to do the state switching, either through explicit methods :

public function videoReady()
{
// create the new state and pass in 'this' as its context, or even
// pass in the 'TransportView', whichever way gets you access to the
// things you need :)
        transportState = new VideoReadyTransportState(this);
}

or handle it one method :

public function setVideoState(vs:String)
{
// you could hold a map of states, e.g. "READY" -> VideoReadyTransportState
// where you have already created the state instances
        transportState = stateMap[vs];
}

---

does that make any sense?


Stefan Vandermeulen wrote:

> hi Martin,
>
> i'm just trying to find a clean way to build in application flow.
> now i'm building a video player and using mvc
>
> the players flow:
>
> do some checks on video ( countdown, geolock, etc.. )
> display image
> start preroll ( flv or swf )
> display image or continue
> start video
> start postroll ( flv or swf )
>
> each state has it's own behavior ( play/pause/stop etc... witch buttons
> to enable/disable )
> of course i can use a switch statement somewhere in my playbackmodel but
> i would like a cleaner more elegant solution
> a solution that can be used in other projects too ( as an architecture )
>
> grt
> s

_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org

Re: mvc and state pattern

by Edgars Simsons :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey,

 I would have a sollution as follows:

 have a _state property in model
have get and set functions
set function also dispaches notifyChanged STATE_CHANGE
in EventList whave public static var STATE_CHANGE : EventType = new EventType( "onStateChange )
all the Views would be added as listeners to the model
add any other necessary listeners for this particular event in model
this event would be listened by "onStateChange" functions
in the listeners you can have a switch statement for necessary changes
and for the functions who need to change implementation depending on state, also have a switch or if

 Hope this is useful.

Edgars

2008/6/12 Martin Wood-Mitrovski <flashdev@...>:
to me that sounds like the state part is the 'VideoController'

i.e. the part that handles user interaction from the view.

I guess one solution is to have a single 'VideoController' class which delegates
input to its current state.

e.g.

VideoController :

// This holds the controller which handles the current state
// of the 'transport' i.e. play / stop etc..
private var transportState:ITransportControllerState;

private function playClicked(event:BasicEvent)
{
       transportState.playClicked();
}

----

Then you can choose how to do the state switching, either through explicit methods :

public function videoReady()
{
// create the new state and pass in 'this' as its context, or even
// pass in the 'TransportView', whichever way gets you access to the
// things you need :)
       transportState = new VideoReadyTransportState(this);
}

or handle it one method :

public function setVideoState(vs:String)
{
// you could hold a map of states, e.g. "READY" -> VideoReadyTransportState
// where you have already created the state instances
       transportState = stateMap[vs];
}

---

does that make any sense?


Stefan Vandermeulen wrote:
> hi Martin,
>
> i'm just trying to find a clean way to build in application flow.
> now i'm building a video player and using mvc
>
> the players flow:
>
> do some checks on video ( countdown, geolock, etc.. )
> display image
> start preroll ( flv or swf )
> display image or continue
> start video
> start postroll ( flv or swf )
>
> each state has it's own behavior ( play/pause/stop etc... witch buttons
> to enable/disable )
> of course i can use a switch statement somewhere in my playbackmodel but
> i would like a cleaner more elegant solution
> a solution that can be used in other projects too ( as an architecture )
>
> grt
> s

_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org


_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org

Re: mvc and state pattern

by Stefan Vandermeulen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Edgars,

your solution works and in this case ( not to many states ) it's not a problem, but your code could get hard to maintain if you would add more and more states.
( for instance a video player can also have a normalviewState, fullscreenviewState,... witch can influence behavioour of all other states )
it's for this reason i want a clean solution that keeps code clean and elegant, but also very flexible.

the solution that Martin described seems ok. placing states between controller and model. if i think about it. it's a bit command pattern like, no?

stefan

Edgars Simsons schreef:
Hey,

 I would have a sollution as follows:

 have a _state property in model
have get and set functions
set function also dispaches notifyChanged STATE_CHANGE
in EventList whave public static var STATE_CHANGE : EventType = new EventType( "onStateChange )
all the Views would be added as listeners to the model
add any other necessary listeners for this particular event in model
this event would be listened by "onStateChange" functions
in the listeners you can have a switch statement for necessary changes
and for the functions who need to change implementation depending on state, also have a switch or if

 Hope this is useful.

Edgars

2008/6/12 Martin Wood-Mitrovski <flashdev@...>:
to me that sounds like the state part is the 'VideoController'

i.e. the part that handles user interaction from the view.

I guess one solution is to have a single 'VideoController' class which delegates
input to its current state.

e.g.

VideoController :

// This holds the controller which handles the current state
// of the 'transport' i.e. play / stop etc..
private var transportState:ITransportControllerState;

private function playClicked(event:BasicEvent)
{
       transportState.playClicked();
}

----

Then you can choose how to do the state switching, either through explicit methods :

public function videoReady()
{
// create the new state and pass in 'this' as its context, or even
// pass in the 'TransportView', whichever way gets you access to the
// things you need :)
       transportState = new VideoReadyTransportState(this);
}

or handle it one method :

public function setVideoState(vs:String)
{
// you could hold a map of states, e.g. "READY" -> VideoReadyTransportState
// where you have already created the state instances
       transportState = stateMap[vs];
}

---

does that make any sense?


Stefan Vandermeulen wrote:
> hi Martin,
>
> i'm just trying to find a clean way to build in application flow.
> now i'm building a video player and using mvc
>
> the players flow:
>
> do some checks on video ( countdown, geolock, etc.. )
> display image
> start preroll ( flv or swf )
> display image or continue
> start video
> start postroll ( flv or swf )
>
> each state has it's own behavior ( play/pause/stop etc... witch buttons
> to enable/disable )
> of course i can use a switch statement somewhere in my playbackmodel but
> i would like a cleaner more elegant solution
> a solution that can be used in other projects too ( as an architecture )
>
> grt
> s

_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org


_______________________________________________ Pixlib mailing list Pixlib@... http://osflash.org/mailman/listinfo/pixlib_osflash.org __________ NOD32 3179 (20080611) Informatie __________ Dit bericht is gecontroleerd door het NOD32 Antivirus Systeem. http://www.nod32.nl


_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org

Re: mvc and state pattern

by Martin Wood-Mitrovski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Stefan Vandermeulen wrote:

> Hi Edgars,
>
> your solution works and in this case ( not to many states ) it's not a
> problem, but your code could get hard to maintain if you would add more
> and more states.
> ( for instance a video player can also have a normalviewState,
> fullscreenviewState,... witch can influence behavioour of all other states )
> it's for this reason i want a clean solution that keeps code clean and
> elegant, but also very flexible.
>
> the solution that Martin described seems ok. placing states between
> controller and model. if i think about it. it's a bit command pattern
> like, no?

well, after thinking about it I came to the conclusion that I kind of missed the
point, mainly because I often dont use an explicit model for UI components, I
generally just model the 'business domain' and not the application.

so...I think Edgars is right that if you have an explicit model for this UI
component then that would be the place to perform the state management, BUT,
I think that in this case it would probably be overkill as although you can look
at it as a change of state, it appears to me to be more of a change of
configuration, either some buttons are enabled or they are not, rather than the
  buttons performing different actions depending on the state. (Im talking about
this part in context of only the 'transport' section, play / stop etc..)

Probably what is a good idea in this case is to reduce the whole 'Video Player'
component into sub-components and deal with them seperately.

i.e.

Transport Control (the play / stop buttons etc..)
Video Info (current position, length, amount buffered)
Display Controls (toggle fullscreen)
Video View (displays the actual video)

then the Video Player component combines all this pieces together. That way you
dont always have to use all the pieces, you might want a simple player with just
a video view that plays when its ready.


_______________________________________________
Pixlib mailing list
Pixlib@...
http://osflash.org/mailman/listinfo/pixlib_osflash.org