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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:26:25+00:00 2026-05-31T06:26:25+00:00

I tagged this question both Java and Scala because while I mainly develop on

  • 0

I tagged this question both Java and Scala because while I mainly develop on Java I’d like to see if the solution on Scala will be different.

I have a problem with designing classes for my application. I have a set of common objects with different behaviour. As I recently read some book about patterns I told: “Okay, I can use a Strategy pattern here”, define behaviour as some field object and delegate it all the logic. And here are my problem started 🙂

Let’s say I have a base class Duck which can fly and I delegate flying to some FlyBehaviour.

interface IFlyable { void fly(); }

interface IFlyBehaviour { void fly(); }

class Duck implements IFlyable {
    IFlyBehaviour flyBehaviour;

    void fly() {
        flyBehaviour.fly();
    }
}

But my ducks are a little different and I realize that I want the behaviour to depend on it. First I have a SpaceDuck which should fly in space and use a spaceShip field which defined only for SpaceDuck. Next I have a HelicopterDuck which I want to fly as low as possible and use some anti-air-defence flares which are defined only for HelicopterDuck. So in code it’s something like this

class SpaceDuck extends Duck {
    String spaceship;
}

class SpaceFlyBehaviour implements IFlyBehaviour {
    void fly() {
        System.out.println("Flying in space on spaceship: " + spaceduck.spaceship);
    }
}

class HelicopterDuck extends Duck {
    int flares;
}

class HelicopterFlyBehaviour implements IFlyBehaviour {
    void fly() {
        while(helicopterduck.flares > 0) {
            System.out.println("I'm going low and using flares");
            helicopterduck.flares--;
        }
    }
}

Here in my behaviour implementations I don’t actually have a reference to spaceduck or helicopterduck and this code won’t compile. I just provided a sample of how I imagined and would like it to be. I could modify IFlyBehaviour and pass duck as an argument to fly() method but then I have to downcast to get access to duck specific fields which is not a good idea I guess.

It looks like the obvious way is just dropping IFlyBehaviour away and move the logic to fly() method of each duck. But I expect a lot of different space fly behaviours and helicopter behaviours and fly() is not the only method. It will be squack(), run() and etc. and each with different set of behaviours. So my class hierarchy will become huge and unsupportable.

In my real application I will have some runnable and stoppable instances which can be run and stopped in different way. One instance would be started via SSH script, another via MBean (or SSH, it depends on how user configured it), third using some 3rd party and etc. So I hope the Duck sample reflexes my problem quite well.

Any thoughts to push me in right direction would be very helpful. Thanks in advance!

  • 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-31T06:26:26+00:00Added an answer on May 31, 2026 at 6:26 am

    In Scala, traits were included just for this kind of stuff.

    In Java, it is more difficult. I would try getting rid of the duck subclasses rather than the behaviours, moving the type-specific properties into the behaviour classes. If you only use the ducks via their common interface, these properties are unavailable anyway – they should only be seen at the point of instantiation.

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

Sidebar

Related Questions

Late Edit I tagged this as a C# question as well as C++ because
I tagged this question as javascript because even though I currently wrote this in
Forgive the duplicate question. I'd like to see this solved in Emacs Lisp too,
While this question is tagged EventMachine, generic BSD-socket solutions in any language are much
Note first of all that this question is not tagged winforms or wpf or
Tagged with [android] so that someone will actually read this, but applies equally to
(I have tagged this question as Python as well since I understand Python code
this is my first question on StackOverflow, but I think that we'll both come
First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow
EDIT: I've tagged this C in a hope to get more response. It's more

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.