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 3935008
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:46:52+00:00 2026-05-19T23:46:52+00:00

Here’s a part of a class hierarchy that I use for a simulation model

  • 0

Here’s a part of a class “hierarchy” that I use for a simulation model (my code is in Python, but I think my question isn’t language dependent):

class World:
# highest-level class, which "knows" everything about the model
# most likely will have just one instance
# contains (e.g., in a dictionary) references to all the instances of class Agent

class Agent:
# each instance represents an agent
# an agent can, among other things, move around according to certain rules
# movement depends on the internal state of the agent,
# but also on the terrain and other information not stored in the Agent instance

Question: where should I put the move instance method?

I thought I’m supposed to limit the dependency of class Agent to classes lower in the hierarchy than itself (i.e., classes whose instances are contained in Agent instances). But that means move method cannot be in class Agent since it creates a dependency on (at least the interface of) classes that describe terrain, etc – so I might as well add to Agent a reference to (and hence a dependency on) World. Is this ok from software design perspective?

The alternative is to put method move in class World, where it won’t cause any additional dependencies. However, class World would then be doing almost all the work, and it seems to me that it would go against the main idea of OOP (which I understand as not to pile all the functionality into one place, but rather contain it in the relevant classes).

Performance considerations are only of minor concern (and I don’t think performance would differ between the two approaches anyway).

EDIT: I misused the words “class hierarchy” above. I wasn’t referring to inheritance hierarchy, just to a bunch of classes whose instances contain each other.

  • 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-05-19T23:46:52+00:00Added an answer on May 19, 2026 at 11:46 pm

    The thing that you need to take into account is the Single Responsibility Principle. Basically, each class should be responsible for one “thing”, and should completely encapsulate that one responsibility. And you should only inherit where the responsibility is extended. You should always be able to say that the extending class is 100% of the parent and more (more in a specific sense). You should never have a situation where the child is a subset of the parent and is “less”. So a person extending a world is not a good design since there are aspects of the world that do not relate to a person.

    So, if we look at an example, you would put the instance methods on the level that is dictated by the role of that particular class. So, let’s take a more defined look at an example:

    class Person:
        name: ""
        birthDate: ""
    
    class PoliceOfficer extends Person:
        badgeNumber: ""
    

    Obviously this is pseudocode, but it demonstrates what’s happening.

    Now, where would you add a move() method? We could add it to PoliceOfficer, but then we would break the encapsulation of Person since a person can also move.

    class Person:
        def move(world):
    

    But, where would we add an issueTicket() method? The generalized Person cannot issue a ticket, so if we added that to the Person class, we’d be breaking the responsibility of it. So instead, we’d add it to PoliceOfficer, since that’s where it makes sense.

    As far as creating dependency, you should always favor composition over inheritance. So in that sense, there can be as many dependencies as you’d like since they are all soft-dependencies (well, kind of). Since move() takes an instance of world (or an object with the world interface), the dependency is pushed out of the class and into the calling code. So that lets your class’s code remain pretty open and dependency-free while still being productive.

    It’s generally seen as bad practice to hard-code dependencies. But injecting them (through Dependency Injection or Composition) is typically seen as a good thing.

    In summary: Put instance methods where it makes logical sense to put them.

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

Sidebar

Related Questions

Here a simple question : What do you think of code which use try
Here is my code (Say we have a single button on the page that
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
Here is an example: I write html code inside of textarea, then I swap
Here's what I'm trying to accomplish with this program: a recursive method that checks

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.