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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:08:38+00:00 2026-06-09T21:08:38+00:00

class Entity { // various fields and methods } class MobileEntity extends Entity {

  • 0
class Entity {
    // various fields and methods
}

class MobileEntity extends Entity {
    // various fields and methods
}

interface Consumer {
    public void consume(final Consumable c);
}

interface Consumable {
    public void onConsume(final Consumer c);
}

class Player extends MobileEntity implements Consumer {
    // Override methods, etc
    // Can consume
}

class Npc extends MobileEntity {
    // Cannot consume anything
}

class Food implements Consumable {
    public void onConsume(final Consumer c) {
        if (c instanceof Player) {
            healPlayer();
            removeFood(1);
        }
    }
}

Sorry for putting all of the code first, but I figured it’d be easier for me to explain, and for you to understand if you saw it. This is for a game I’m developing. The problem I’m having is how to deal with a Consumable (be it Food or Drink) without using the instanceof operator. I would like to avoid setting the concrete class Player in the Food class (because there could end up being hundreds of different types of Food), but there are exceptions to the contract in each case (which indicates a problem).

Not all Entity implementations will have a health field. They might, but if so, I would need to mark certain ones as “attackable”, and then have a test

void applyDamage(int damage) {
    if (attackable) {
        health -= damage;
    }
}

Which introduces new problems, due to hard-coding behaviour (an indicator to introduce a new class)

I’ve heard of possibly using the Visitor pattern in similar thread, but I don’t think it applies in the current situation.

  • 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-09T21:08:39+00:00Added an answer on June 9, 2026 at 9:08 pm

    Since Player implements Consumer, the onConsume method in Food should just call the consume() method on the the Consumer. In this case, the Player’s version of consume would do the right job. instanceof is not only bad but would fail when the objects are decorated.

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

Sidebar

Related Questions

I have an abstract entity: public abstract class Entity extends JPanel implements FocusListener And
mI've this entity class: @Entity public class User implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private
public abstract class Entity : IEntity { [Key] public virtual int Id { get;
I have an entity similar to the below: public class Entity { public List<DateItem>
I have the following classes: public class Entity<T> where T : Entity<T> { public
I have the following classes in my Model: public abstract class Entity : IEntity
These are my domain and view classes: public abstract class Entity : IEntity {
class TheModel extends Backbone.Model foo: #bar Entity = new TheModel(#pass in attributes) Can I
I have the following JPA (2.0.2) entities: Employee @Entity @Table(name = T_EMPLOYEE) public class
I have the following entity class : @Entity @Table(name = THE_TREE, catalog = ,

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.