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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:31:06+00:00 2026-05-10T16:31:06+00:00

I have created my own Tree implementation for various reasons and have come up

  • 0

I have created my own Tree implementation for various reasons and have come up with two classes, a ‘base’ class that is a generic tree node that is chock full of logic and another class that extends that one which is more specialised.

In my base class certain methods involve instantiating new tree nodes (e.g. adding children). These instantations are inside logic (in a nested loop, say) which makes the logic hard to separate from the instantation.

So, if I don’t override these instantations in the specific class the wrong type of node will be created. However, I don’t want to override those methods because they also contained shared logic that shouldn’t be duplicated!

The problem can be boiled down to this:

public class Foo {     public String value() { return 'foo'; }      public Foo doStuff() {         // Logic logic logic..         return new Foo();     } }  class Bar extends Foo {     public String value() { return 'bar'; }  }  new Bar().doStuff().value(); // returns 'foo', we want 'bar' 

The first thing that popped into my head would have a ‘create hook’ that extending classes could override:

public Foo createFooHook(/* required parameters */) {   return new Foo(); } 

Now. while it was a fine first thought, there is a stench coming off that code something awful. There is something very… wrong about it.

It’s like cooking while naked– it feels dangerous and unnecessary.

So, how would you deal with this 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. 2026-05-10T16:31:07+00:00Added an answer on May 10, 2026 at 4:31 pm

    So, after getting my copy of Design Patterns and opening it for what I’m fairly sure is the first time ever I discovered what I want.

    It’s called the Factory Method and it’s mostly a perfect fit. It’s still a bit ugly because my super class (Foo in the above example) is not abstract which means subclasses are not forced to implement the hook.

    That can be fixed with some refactoring though, and I’ll end up with something to the effect of:

    abstract class AbstractFoo {     public String value() { return 'Foo'; }      public AbstractFoo doStuff() {         // Logic logic logic         return hook();     }      protected abstract AbstractFoo hook(); }  class Foo extends AbstractFoo {     protected AbstractFoo hook() { return new Foo(); } }  class Bar extends AbstractFoo {     public String value() { return 'Bar'; }      protected AbstractFoo hook() { return new Bar(); } }  new Bar().doStuff().value(); // Returns 'Bar'! 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Declare the fileset out of the path and assign it… May 11, 2026 at 6:29 am
  • added an answer WPF doesn't include the handy System.Windows.Forms.Screen class, but you can… May 11, 2026 at 6:29 am
  • added an answer Have a look at this post. May 11, 2026 at 6:29 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.