You are not logged in.

#1 2005-10-06 20:17:42

mmccaskill
Member
From: NC
Registered: 2005-02-21
Posts: 165

Design Pattern for Building Trees (Java)

I'm wondering if there is a design pattern suited to build trees from a given domain object.

public interface DomainObject {
    public long getId();
}

public abstract class Displayable {
    private DomainObject domainObject;
    private Displayable parent;
    private Vector children;
    
    public Displayable(DomainObject domainObject, Displayable parent) {
        this.domainObject = domainObject;
        this.parent = parent;
        this.children = new Vector();
    }
    
    public Displayable(DomainObject domainObject) {
        this(domainObject, null);
    }

    public Displayable getParent() {
        return parent;
    }

    public DomainObject getDomainObject() {
        return domainObject;
    }

    public Displayable[] getChildren() {
      return (Displayable[])children.toArray(new Displayable[children.size()]);
    }

    public void addChild(Displayable d) {
        children.add(d);
    }
}

Example:
DomainObject O
Displayable D (contains O)
D's parent is C
C's parent is B
B's parent is A
A does not have a parent

A --> B --> C --> D(O)

Method signature

public Displayable buildDisplayable(DomainObject domainObject);

So basically pass in a DomainObject (O) and spit out the Displayable (D), with the tree built up to the root (A).

Offline

#2 2005-10-07 01:32:32

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Design Pattern for Building Trees (Java)

sounds like a homework problem.

Offline

#3 2005-10-07 05:10:52

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Design Pattern for Building Trees (Java)

No kidding. Did they ask for it in a balanced red-black tree, or a b-tree?
wink


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#4 2005-10-07 11:43:13

mmccaskill
Member
From: NC
Registered: 2005-02-21
Posts: 165

Re: Design Pattern for Building Trees (Java)

No I graduated last year from college. I'm trying to improve our SWT/JFace GUI framework. That method signature is just something I thought about. I'm thinking using Factory Pattern will have to do considering I know ahead of time which subclasses of Displayable have parents and children.

Offline

Board footer

Powered by FluxBB