Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8848335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:24:02+00:00 2026-06-14T12:24:02+00:00

I’m currently trying to find a definitive solution (meaning : finding a solution that

  • 0

I’m currently trying to find a “definitive” solution (meaning : finding a solution that seems efficient a complying with OOP precepts) to a recurring problem I’ve been experiencing for some time : the problem of shared data in different parts of my code.

Take note that I’m not using any MVC framework anywhere here. I’m just refering to my data class as a Model and to the display class as a View (because its the proper names and have nothing to do with the MVC pattern, people made views & models way before the MVC pattern was “created”).

Here’s my problem :
Whenever I make an application that uses some quite expanded data (for example a game), I try to separate logic (movements, collisions, etc…) and display in two classes. But then, I stumble upon the problem : how to “bind” the data stored in my logic class with the corresponding display objects in my view class, without duplicating data, references, or other things between the different classes ?

Lets take a basic example :

  • I have a MyLogicClass, holding a Vector of “EntityData” objects (each with position, sizes, various states, everything to handle the logic of my items)

  • And I have a MyViewClass, creating and displaying Sprites for each EntityData that are in the MyLogicClass, and make them move after them being updated in the game loop.

The first thing that would come to my mind would be to store inside each data element its corresponding view, thus allowing me to loop throught my Vector to update the items logic then update the views accordingly. But that forces me to hold a MyLogicClass reference inside the MyViewClass, to be sure that I can target the entities data, forcing me to couple the two classes (things that I would prefer not to do).

On the other hand, there’s the solution of each Entity having an ID, both in my data model (MyLogicClass’s EntityData objects having an ID parameter) and in my View class (Sprites holding a reference to its original entity data ID). But when I want to target a specific entity that forces me to loop for it in my data model, then loop for it again to find the related Sprite in my View. This solution allows me to have loose coupling between my data and my view, but looping through hundreds of elements twice every frame (can happen !) really sounds not performance optimized for me.

I may be giving the whole problem a lot more importance that it should deserve, but I’ve been stumbling upon that more than one time, and I’d love to have some other views than mine about that.

Do you guys have any advice / solution for such an issue ?

Are there some other data formats / hierarchy that I may not be aware of for such case ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:24:04+00:00Added an answer on June 14, 2026 at 12:24 pm

    I think maybe you have over thought the problem. I do this sometimes.

    Your view class has to have some type of link to the model obviously and an event is a great way to do it. Something bare bones here to give you an idea.

     // Model class
     package
     {
        class MyModel extends EventDispatcher
        {
            // you can make them public but that would 
            // be against some oop practices. so private it is
            private var m_position:Vector2D; 
    
            MyModel(){}
    
            // one way of doing getters/getters
            // example: theModel.SetPosition(something);
            public function GetPosition():Vector2D { return m_position; }
            public function SetPosition(value:Vector2D):void 
            { 
                m_position = value; 
                ModelChanged();
            }
    
            // the other way
            // sample: theModel.position = something;
            public function get position():Vector2D {return m_position; }
            public function set position(value:Vector2D):void 
            { 
                m_position = value; 
                ModelChanged();
            }
    
            private function ModelChanged():void
            {
                dispatchEvent(new Event(Event.CHANGE));
            }
        }
    }
    
    
    // now for our view.
    package
    {
        class MyView extends Sprite // or whatever
        {
            private var model:MyModel;
    
            MyView(model:MyModel)
            {
                this.model = model;
    
                model.addEventListener(Event.CHANGE, handleModelChanged);
    
                // fire off an event to set the initial position.
                handleModelChanged(NULL);
            }
    
            private function handleModelChanged(evt:Event):void
            {
                x = model.position.x;
                y = model.position.y;
                // etc etc etc.
            }
        }
    }
    

    Anyhow you don’t need the setters if your going to have the logic in the model file also obviously if nothing outside of the model needs to change it no reason for setters. But you do need the getters.

    This decouples the model from the view and you can write any view any way you want and all you have to provide is a handler for when the model has changed. Just expose whatever data your views will need with getters.

    You now only have to loop through the models and if one changes it will fire off an event and the views that are listening in will update.

    hope I didn’t miss anything and that explains what you were wanting.

    Edit: I forgot to add, you don’t have to have “ModelChanged()” all over the place if your using something like an update function. Just update and when your finished fire off the event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.