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

  • SEARCH
  • Home
  • 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 751065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:37:24+00:00 2026-05-14T14:37:24+00:00

At work, we are developing an PHP application that would be later re-programmed into

  • 0

At work, we are developing an PHP application that would be later re-programmed into Java. With some basic knowledge of Java, we are trying to design everything to be easily re-written, without any headaches. Interesting problem came out when we tried to implement composite pattern with huge number of methods in leafs.

What are we trying to achieve (not using interfaces, it’s just a quick example):

class Composite {
    ...
}


class LeafOne {
    public function Foo( );

    public function Moo( );
}


class LeafTwo {
    public function Bar( );

    public function Baz( );
}


$c = new Composite( Array( new LeafOne( ), new LeafTwo( ) ) );

// will call method Foo in all classes in composite that contain this method
$c->Foo( );

// same with Bar
$c->Bar( );

It seems like pretty much classic Composite pattern, but problem is that we will have quite many leaf classes and each of them might have ~5 methods (of which few might be different than others). One of our solutions, which seems to be the best one so far and might actually work, is using __call magic method to call methods in leafs.
Unfortunately, we don’t know if there is an equivalent of it in Java.

So the actual question is: Is there a better solution for this, using code that would be eventually easily re-coded into Java? Or do you recommend any other solution? Perhaps there’s some different, better pattern I could use here.

In case there’s something unclear, just ask and I’ll edit this post.

Edit:

Actual problem is that not every leaf class contains, for example, method Baz. If we used simple foreach to call Baz in every class, it’d give use bunch of errors, as there are certain classes that don’t contain this method. Classic solution would be to have every single method from every single leaf class implemented into Composite class, each with different implementation. But this would make our composite class huge and messy with amount of methods we use.

So usual solution would look like this (Composite class):

class Composite implements Fooable, Bazable {
    ...

    public function Foo( ) {
        foreach( $this->classes as $class ) {
            $class->Foo( );
        }
    }

    public function Baz( ) {
        ...
    }
}

To prevent our code to become real mess, we were thinking about something like:

class Composite {
    ...

    public function __call( ) {
        // implementation
    }
}

But we aren’t really sure if it’s a good solution and if there’s something similar also in Java (as asked already before edit).

  • 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-14T14:37:25+00:00Added an answer on May 14, 2026 at 2:37 pm

    Within Java you could consider using the visitor pattern whereby you pass a visitor object to each node in the tree and the node makes a callback to the visitor class to determine which behaviour should be performed.

    This avoids any casting or explicitly checking the type of each node.

    /**
     * Visitor capable of visiting each node within a document.
     * The visitor contains a callback method for each node type
     * within the document.
     */
    public interface DocumentNodeVisitor {
      void visitWord(Word word);
      void visitImage(Image img);
    }
    
    /**
     * Base interface for each node in a document.
     */
    public interface DocumentNode {
      void applyVisitor(DocumentVisitor v);
    }
    
    /**
     * Conrete node implementation representing a word.
     */    
    public class Word implements DocumentNode {
      private final String s;
    
      public Word(String s) { this.s = s; }
    
      public String getValue() { return this.s; }
    
      public void applyVisitor(DocumentVisitor v) {
        // Make appropriate callback to visitor.
        v.visitWord(this);
      }
    }
    
    /**
     * Conrete node implementation representing an image.
     */        
    public class Image implements DocumentNode {
      public void applyVisitor(DocumentVisitor v) {
        // Make appropriate callback to visitor.
        v.visitImage(this);
      }
    }
    
    public class Paragraph implements DocumentNode {
      private final List<DocumentNode> children;
    
      public Paragraph() {
        this.children = new LinkedList<DocumentNode>();
      }
    
      public void addChild(DocumentNode child) {
        // Technically a Paragraph should not contain other Paragraphs but
        // we allow it for this simple example.
        this.children.add(child);
      }
    
      // Unlike leaf nodes a Paragraph doesn't callback to
      // the visitor but rather passes the visitor to each
      // child node.
      public void applyVisitor(DocumentVisitor v) {
        for (DocumentNode child : children) {
          child.applyVisitor(v);
        }
      }
    }    
    
    /**
     * Concrete DocumentVisitor responsible for spell-checking.
     */
    public class SpellChecker implements DocumentVisitor
      public void visitImage(Image i) {
        // Do nothing, as obviously we can't spellcheck an image.
      }
    
      public void visitWord(Word word) {
        if (!dictionary.contains(word.getValue()) {
          // TODO: Raise warning.
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SeRQL doesn't support update operations. SPARQL does in its 1.1… May 15, 2026 at 1:46 pm
  • Editorial Team
    Editorial Team added an answer Yup. In your options (the hash passed to the qtip()… May 15, 2026 at 1:46 pm
  • Editorial Team
    Editorial Team added an answer If you use Forward, it is still the same request.… May 15, 2026 at 1:46 pm

Trending Tags

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

Top Members

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.