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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:45:04+00:00 2026-05-12T07:45:04+00:00

I’m recently working on a tree structure, multiple nodes, multiple and increasable levels, and

  • 0

I’m recently working on a tree structure, multiple nodes, multiple and increasable levels, and a print() method.
At first, i thought it should be a Composite, i then wrote down some possible design and codes:

alt text

$struc = new Node(‘name0’, ‘id0’, ‘desc0’);
$node1 = new Node(‘node1’, ‘id1’, ‘desc1’);
$node2 = new Node(‘node2’, ‘id2’, ‘desc2’);
$node3 = new Node(‘node3’, ‘id3’, ‘desc3’);
$leaf1 = new Leaf(‘leaf1’, ‘ld1’, ‘lesc1’);
$leaf2 = new Leaf(‘leaf2’, ‘ld2’, ‘lesc2’);
$leaf3 = new Leaf(‘leaf3’, ‘ld3’, ‘lesc3’);
$leaf4 = new Leaf(‘leaf4’, ‘ld4’, ‘lesc4’);

$struc.add($node1);
$struc.add($node3);

$node1.add($leaf1);
$node1.add($leaf2);
$node1.add($node2);

$node2.add($leaf3);    
$node3.add($leaf4);

Looks good, i think and begin coding, print() method may follow Iterator pattern later.
But during coding, i feel is it too complex for these simple nodes? And i have to instantiate a lot of concrete classes (more than 50+, and increasing). I then stopped and thinking a simple similar way by using array:

-- Structure Class --
//To be more readable and clear, array here could be
//divided to 3 arrays(root/nodes/leafs), then connect
//in a similar way Composite does.
$struc = array('name0', 'id0', 'desc0',

           'children'=>array(

               array('node1', 'id1', 'desc1',
                  'children' => array(
                     array('leaf1', 'ld1', 'lesc1'),
                     array('leaf2', 'ld2', 'lesc2'),
                     array('node2', 'id2', 'desc2',
                        'children'=>array(array('leaf3', 'ld3', 'lesc3'))
                     )
                  )
               ),

               array('node3', 'id3', 'desc3',
                  'children' => array(array('leaf4', 'ld4', 'lesc4'))
               )
           )
);

function print($node = $this->struct) {
    ...
    if(isset($node['children'])) $this->print($node['children']);
    ...
}

The two designs look very similar, now i’m a little confused, what’s the value of Composite pattern, am i missed something important of this pattern?

  • 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-12T07:45:05+00:00Added an answer on May 12, 2026 at 7:45 am

    The point of the composite pattern is to be able to treat a collection of objects as if it were a single object (e.g. for displaying it or writing it to a file). As you write yourself “print() method may follow Iterator pattern later” – well, the point of the composite pattern would be that you could call print() without having to worry about whether you’re printing a single Component or have to iterate through an entire Tree.

    But it looks as though you’re unclear about object-oriented programming in general, since you’re considering using nested arrays instead. The value of using objects instead of hashes (which PHP arrays are) for everything is type safety, which makes it much easier to debug and maintain programs.

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

Sidebar

Ask A Question

Stats

  • Questions 188k
  • Answers 188k
  • 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 Such information is provided for some types (e.g. for WebRequest),… May 12, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer Set the AutoSizeMode of the problematic column to AllCells. This… May 12, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer If you want to color individual days in the calendar,… May 12, 2026 at 5:37 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

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.