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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:02:41+00:00 2026-06-11T19:02:41+00:00

Let’s assume there is an ArrayList that contains many different elements of type Element.

  • 0

Let’s assume there is an ArrayList that contains many different elements of type “Element”. All of them will be drawn to the screen in an endless loop.

These “Element”s can implement 0..n interfaces like “Movable”, “Selectable” and so on.

What I tried so far is iterating through all Elements and check for an interface like:

ArrayList<Element> allElements;
...

for (Element element : allElements) {
  if (element instanceof Movable) {
    ((Movable)element).move();
  }
  if (element instanceof Selectable) {
    ...
  }

  element.draw();
}

I’m not happy with that approach, though, because it violates the open/closed principle (and probably thousands of other principles too). Of course, I could re-design it so that each element responds itself according to the interfaces it implements:

for (Element element : allElements) {
  element.move(); // element checks itself if it can move, and if true moves
  ...
}

The drawback is that the Element class must provide signatures for each possible behavior, i.e. it must provide the methods of every interface and maybe override them in child classes. That’s also not what I want because it bloats the Element class.

I also tried selecting Elements by interface like this:

getElementsByInterface(Movable, allElements) { ... }

(Movable is the interface, allElements the ArrayList)

But it doesn’t compile. It seems Java doesn’t know interfaces at run-time anymore.

Long story short (sorry for my possibly overly verbose post):
What is the best design solution to let array elements react according to their abilities(/interfaces)?

  • 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-11T19:02:42+00:00Added an answer on June 11, 2026 at 7:02 pm

    I see nothing inherently wrong with this approach.

    There are basically two approaches to this scenario:

    1. Use polymorphism and the Visitor pattern. Due to (the interface aspect of) the LSP, this introduces the “drawback” described of each handler having to implement all actions.

    2. Using local type-based switching (this is more common place in languages like Scala with ADTs where this approach is acceptable/common; the Java syntax is just a bit unwieldy in comparssion).

    There is no violation of (polymorphic) open/closed here because interfaces are being used and the “action” is still left in the implementation (which still supports polymorphism and thus “open”); it is only the selection of action that is not.

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

Sidebar

Related Questions

Let's assume that we are building a high traffic site that will be used
Let's assume that a user votes for some movies in a scale of 1
Let assume we have two activities. A - main activity, that is home launcher
Let's suppose there are many divs inside a div: <div id=#container> div div div
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say there is a graph and some set of functions like: create-node ::
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say after I had login I will be prompt to enter the Name
Let's say, if I need to assign an id to element with first name,

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.