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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:43:58+00:00 2026-05-27T10:43:58+00:00

Preamble : Is it bad design for a base class to be aware of

  • 0

Preamble: Is it bad design for a base class to be aware of and make interactions using derived types? I’m assuming not, so what approach should I consider for the following?


(Language is PHP, but I think this question is more concerned with the broader subject of design patterns)

I’ve been having a dilemma trying to model a set of classes to act as nodes; I keep second guessing my design decisions, and it’s leading to perpetual frustration.

Given this set of parameters:

  • Nodes have parent references (one way traversal)
  • Any object type derived of Node may be a parent (or child) to any other object type derived of Node.

So I’ve got:

abstract class AbstractNode{

    protected $_parent;

    public function __construct(self $parent = null){
        $this->_parent = $parent;
    }

    public function get_parent(){
        return $this->_parent;
    }

}

class NodeOne extends AbstractNode{ }

class NodeTwo extends AbstractNode{ }

// more derivatives

Now here’s where my design dilemma comes in; during traversal NodeOne instances may need to be found, by themselves and any other instances of types derived of AbstractNode (note that this functionality isn’t exclusive to NodeOne instances, but this is just an example)

This will allow a type-specific traversal to, for instance, aggregate data from objects of a specific type up the tree. I figured I’d specialize a method to serve this purpose:

public function get_node_one_ancestor(){
    if($this->_parent instanceof NodeOne){
        return $this->_parent;
    }
    if(null !== $this->_parent){
        return $this->_parent->get_node_one_ancestor();
    }
    return null;
}

Since any derived type may need to traverse instances NodeOne, it would make sense to plop this method in the AbstractNode base class, however now my base class requires awareness of a derived type.

I think this smells bad, but I don’t know where else this method should go. I’m reading on structural design patterns for possible solutions.


An analogy that comes to mind is the DOM, doing ancestor traversal for certain types:

<root>
    <foo id="1">
        <bar id="2"></bar>
        <bar id="3">
            <foo id="4">
                <bar id="5">
                    <foo id="6">
                        <bar id="7"></bar>
                    </foo>
                </bar>
                <bar id="8"></bar>
            </foo>
        </bar>
    </foo>
</root>
  • From bar[@id='8'] aggregate all foo ancestor id values:
    Result 4 1

  • From bar[@id='7'] aggregate all foo ancestor id values:
    Result 6 4 1

  • 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-27T10:43:59+00:00Added an answer on May 27, 2026 at 10:43 am

    You should be able to generalize it:

    public function get_ancestor($type){
        if($this->_parent instanceof $type){
            return $this->_parent;
        }
        if(null !== $this->_parent){
            return $this->_parent->get_ancestor($type);
        }
        return null;
    }
    

    To me, it seems like this is something that could live in an external iterator object, but I cannot say that I gave much thought before making this post…

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

Sidebar

Related Questions

What sort of code should I put in preamble, such that every math fomula
Preamble: I know, disabling warnings is not a good idea. Anyway, I have a
The preamble: I have designed a strongly interfaced and fully mockable data layer class
I am using ntheorem to typeset a set of conditions. In my preamble I
Preamble: All data connection strings, connections, etc are created using DbProviderFactories. Code is mixed
Is there any single magic preamble that will make a Perl script run under
Preamble I'm using git as a version control system for a paper that my
Explanation/preamble In Java, if you declare an abstract class with abstract methods, e.g. public
Preamble Using VTK library with C++, quite often I have to write something like
Preamble: I am using ASP.NET 3.5 and AJAXControlToolkit. I am trying to use the

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.