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

  • SEARCH
  • Home
  • 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 893261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:11:29+00:00 2026-05-15T14:11:29+00:00

I have a question re object-oriented design – I have two classes, a Map

  • 0

I have a question re object-oriented design – I have two classes, a Map (in effect an x,y grid) and Items placed on the map. The Map holds all the items and their locations and controls the program flow. The Items have some intelligence and determine if the wish to move around on the map.

For instance the map code may look like:

class Map
{
    ...
    Items[,] map;

    public void DoItemUpdate()
    {
        ...
        for (x = 0 to MaxX)
        {
            for (y = 0 to MaxY)
            {
                    map[x,y].UpdateItem();
            }
        }
    }
}

And the Item code may look like:

class Items
{
    ...
    Vector2 location;

    public void UpdateItem()
    {
        // determine if we want to move the unit
        ....

        // if yes, move the unit
        this.Move(fromXY, toXY);
    }

    public void Move(Vector2 from, Vector2 to)
    {
        // what goes in here - raise event, use reference to Map class?
    }
}

Basically what I’m trying to work out is how the move code should be structured so that it asks the map if the move is legal, and then registers the move in the map.

I have thought of a couple of approaches

  1. Pass a reference to the parent Map class to the Item and call a function from there
  2. Use a delegate / event structure to “callback” the map

Any comments on these approaches – what is the best/cleanest/most appropriate for OO code?
Thanks!

  • 1 1 Answer
  • 3 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-05-15T14:11:29+00:00Added an answer on May 15, 2026 at 2:11 pm

    Neither. I’d add a method to your Items (should that be Item?) class:

    Vector2 GetMoveRequest();
    

    Define a constant value that the Item can return to indicate that it does not want to be moved. (0, 0) if the return value indicates a delta, or (-1, -1) if the return value indicates an absolute position. Or if you don’t want to define a magic value:

    bool TryGetMoveRequest(out Vector2 request);
    

    Advantages:

    You’ve now separated the move logic appropriately across the two classes. The Item knows if it wants to move, and if so where. The Map can evaluate the legality of the move, then perform or ignore the Item’s move request based on your logic. No reference handling, no awkward callback mechanism.

    In addition, the logic within your Map class will be able to grow in complexity. Consider a scenario in which one Item’s move may prevent, or be prevented by another Item’s move. This would be considerably difficult to implement with either of the approaches that you’ve listed.

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

Sidebar

Related Questions

Object-Oriented programmers seem to have all the fun. Not only are they treated to
I have some Result classes that represent flat results in an object oriented fashion.
Both OOD (Object-Oriented-Design) and MVC (Model-View-Controller) architectures have become staples of modern software design.
I've been thinking about this object oriented design question for a while now and
One question that I have long asked myself is in object oriented programming,how should
I have a question about Parallel Object Hierarchies such as DTO. Theses hierarchies has
I have a question regarding the proper way to modify a php DateTime object.
I have a question about how to limit the creation of object on heap
I have a question. I first created an object which extends NSObject, I provided
I have a question regarding events in c#. Lets say I have an object

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.