Some kind of mapping betwen VO and View

fbregist's Avatar

fbregist

15 May, 2013 10:18 AM

Ok i'm here again asking for your help!
I'm making a sort of AdventureGame where the user's choices alter the course of the story.
We have Nodes that rappresents points in the story, there could be a video, a game, a fork, ecc.., so each node VO is of different type innerithing from a NodeBase class that accomodate the common properties.

The problem now is: to each type of node (Classes: VideoNode, Game1Node, Game2Node) i need to associate a particular view that should be loaded and shown when i reach that type of node (Classes: VideoNodeView, Game1NodeView, Game2NodeView)

My approach: i made a ViewFactory Service, i pass the current node to it and it instantiate and return the correct view back.

ViewFactory use a simple switch on the type of node using getQualifiedClassName(node) so we have something like this:

private static const VIDEOSTEP : String = "VideoStep";
private static const FORKSTEP : String = "ForkStep";
private static const CUTGAMESTEP : String = "CutGameStep";
private static const SHOPGAMESTEP : String = "ShopGameStep";
public function createView(step:NodeBase):INodeView
        {
            var view : INodeView;
switch (getQualifiedClassName(step)) { case VIDEOSTEP: view = new VideoNodeView(); break; case FORKSTEP: view = new ForkNodeView(); break; case CUTGAMESTEP: ... break; case SHOPGAMESTEP: ... break;
}
return view;
}

I don't like so much this approach and i'm struggling myself to find a better way to do this, i'm also afraid that my problem could be simply a design flaw of the architecture.

Ideas?
Thank you.

  1. Support Staff 1 Posted by creynders on 16 May, 2013 07:56 AM

    creynders's Avatar

    The easiest solution that comes to mind is to map the VO classes to view classes:

    public class NodeViewFactory implements INodeViewFactory{
        private var _viewsByVOs:Dictionary = new Dictionary();
        public function map( voClass:Class, viewClass:Class):void{
            _viewsByVOs[voClass]=viewClass;
        }
    
        public function createView(vo:NodeBase):INodeView{
            var voClass : Class = vo.constructor as Class;
            return new (_viewsByVOs[voClass] as Class)(vo);
        }
    }
    
    //ConfigureNodeViewFactoryCommand
    
    [Inject]
    public var nodeViewFactory:INodeViewFactory
    
    public function execute():void{
        nodeViewFactory.map(VideoNode,VideoNodeView);
        nodeViewFactory.map(Game1Node,Game1NodeView);
    }
    

    Obviously you'd need to add a check to see whether the VO class is mapped and otherwise throw an error.

    I don't know whether architecturally this is the best approach, I'd need more context to be able to judge that.

  2. 2 Posted by Jerome Maurey-D... on 29 May, 2013 03:53 AM

    Jerome Maurey-Delaunay's Avatar

    It almost feels like the factory is a run around the Commands and mapping mechanism.

    When you reach a VideoNode, a NodeEvent.VideoNode fires and HandleVideoNodeCommand executes. That command knows its duties: setup a video player view to play a video and update the game UI accordingly (like pause gameplay and the update loops). Once that's completed, a state machine or some type of "next step" model makes sure the appropriate action is executed, like going to the next node in the tree or resume gameplay on a level etc...

    You can than use Command Mapping and View Mapping to handle temporary logic requirements. Like map all ForkStep to ForkStep3View after this point.

    Hope that makes sense!

    Cheers,

    J.

  3. Support Staff 3 Posted by Ondina D.F. on 25 Jun, 2013 09:33 AM

    Ondina D.F.'s Avatar

    Closing this for now. You can re-open the thread, if need be.

  4. Ondina D.F. closed this discussion on 25 Jun, 2013 09:33 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac