Use the Composite Template to provide a common interface for all of your objects.
, , , . "" . ( Leaf- .) Composite , , . Composite , Leaf.
ComponentObject. ComponentObjects CategoryObject, .
ComponentObject
public abstract class ComponentObject
{
private CategoryObject myParent;
protected ComponentObject(CategoryObject parent)
{
myParent = parent;
}
public CategoryObject getParent()
{
return myParent;
}
public abstract int getNumChildren();
}
: NonCategoryObject, , , CategoryObject, , , CategoryObject, NonCategoryObject s.
NonCategoryObject
public class NonCategoryObject extends ComponentObject
{
public NonCategoryObject(CategoryObject parent)
{
super(parent);
}
@Override
public int getNumChildren()
{
return 0;
}
}
CategoryObject
public class CategoryObject extends ComponentObject
{
private ArrayList<ComponentObject> myChildren;
public CategoryObject(CategoryObject parent)
{
super(parent);
myChildren = new ArrayList<ComponentObject>();
}
public void addChild(ComponentObject child)
{
myChildren.add(child);
}
public ComponentObject getChild(int index)
{
return myChildren.get(index);
}
public int getNumDirectChildren()
{
return myChildren.size();
}
@Override
public int getNumChildren()
{
int numChildren = 0;
for (ComponentObject child : myChildren)
{
numChildren += 1 + child.getNumChildren();
}
return numChildren;
}
}
getNumChildren .
ArrayList , , , :
public static void main(String[] args)
{
CategoryObject root = new CategoryObject(null);
CategoryObject categoryOne = new CategoryObject(root);
CategoryObject categoryTwo = new CategoryObject(root);
CategoryObject categoryThree = new CategoryObject(root);
CategoryObject categoryOneOne = new CategoryObject(categoryOne);
CategoryObject categoryOneTwo = new CategoryObject(categoryOne);
CategoryObject categoryOneThree = new CategoryObject(categoryOne);
NonCategoryObject leafOneFour = new NonCategoryObject(categoryOne);
NonCategoryObject leafOneOneOne = new NonCategoryObject(categoryOneOne);
NonCategoryObject leafOneOneTwo = new NonCategoryObject(categoryOneTwo);
NonCategoryObject leafOneTwoOne = new NonCategoryObject(categoryOneTwo);
NonCategoryObject leafOneThreeOne = new NonCategoryObject(categoryOneThree);
NonCategoryObject leafTwoOne = new NonCategoryObject(categoryTwo);
NonCategoryObject leafThreeOne = new NonCategoryObject(categoryThree);
ArrayList<ComponentObject> components = new ArrayList<ComponentObject>();
components.addAll(Arrays.asList(leafOneFour, leafOneOneTwo, leafOneThreeOne,
categoryOne, categoryOneOne, categoryOneThree, root, leafThreeOne,
leafOneOneOne, categoryThree, leafOneTwoOne, leafTwoOne,
categoryTwo, categoryOneTwo));
, . ( ).
ComponentObject foundRoot = null;
for (ComponentObject c : components)
{
CategoryObject parent = c.getParent();
if (parent == null)
{
foundRoot = c;
}
else
{
parent.addChild(c);
}
}
, . 2 , :
compositeMethod(foundRoot);
recursiveMethod(foundRoot);
}
. -, "" , . root getNumChildren().
private static void compositeMethod(ComponentObject root)
{
int numChildren = root.getNumChildren();
System.out.println("Composite method: " + numChildren);
}
:
Composite method: 13
, , , , , . .
, , :
private static void recursiveMethod(ComponentObject root)
{
int numChildren = findNumChildren(root);
System.out.println("Recursive method: " + numChildren);
}
private static int findNumChildren(ComponentObject root)
{
if (root instanceof CategoryObject)
{
CategoryObject parent = (CategoryObject) root;
int numChildren = 0;
for (int i = 0; i < parent.getNumDirectChildren(); i++)
{
ComponentObject child = parent.getChild(i);
numChildren += 1 + findNumChildren(child);
}
return numChildren;
}
return 0;
}
:
Recursive method: 13