1 / 12

# Composite - PowerPoint PPT Presentation

Composite. A robot made of smaller robots. *. *. Line with open diamond at the end means “aggregates”. Basically, it’s saying that the Component objects within the Composite class “belong” to it, in a weak sense. Main thing to remember: diamond points to the owner. The basic idea.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Composite' - yaakov

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Composite

A robot made of smaller robots.

Line with open diamond at the end means “aggregates”. Basically, it’s saying that the Component objects within the Composite class “belong” to it, in a weak sense. Main thing to remember: diamond points to the owner.

• A common abstract superclass that has operations that apply to both singular (leaf) objects and composite objects.

• Composite objects delegate method calls to their children

• The Composite objects contain a mix of leaves and other Composites

• Go to the classwork for today in the quiz section in Sakai and answer the challenge 5.2 and 5.3 from the handout.

• Once you finish that, consider this: what part of the Picassa code could be considered a composite?

What part of the Picassa code from last week could be considered a composite?

• Picassa Model, Parser, and RGBColor

• RGBColor and NumberExpression

• Expression, ParenExpression, and NumberExpression

• Operator and the specific operators (e.g. PlusExpression, MinusExpression)

• I hope you understand the gist of composite

• I want to talk about 1 or 2 concrete details from the GoF book

• Then we’ll do a final, more challenging composite exercise

With parent references, it's essential to maintain the invariant that all children of a composite have as their parent the composite that in turn has them as children. The easiest way to ensure this is to change a component's parent only when it's being added or removed from a composite. If this can be implemented once in the Add and Remove operations of the Composite class, then it can be inherited by all the subclasses, and the invariant will be maintained automatically.

-GoF book

Fix this code so it’s not problematic.

//passing to constructor sets parent

//pointer in leaf

Leaf leaf = new Leaf(myComposite);

• What should they do if you call them on a leaf?

• This code could work:

Node myNode = //something

if(myNode.isComposite()) {

CompositeNode composite = (CompositeNode) myNode;

} else {

// throw an exception or do whatever you do in this

// case

}

• I hope you understand the gist of composite

• I want to talk about 1 or 2 concrete details from the GoF book

• How to maintain parent references (keep them in add and remove)

• Should add and remove be in abstract parent? (maybe or you can check and then cast)

• Next: Final, more challenging composite exercise

publicclassWordWebSearch {

private String searchWord;

publicWordWebSearch(String searchWord)

{

this.searchWord = searchWord;

}

public List<Webpage> getAllPagesMatching(List<Webpages> inputPages)

{

// returns a new list with only web pages containing

// the search word

}

}

We have a Websearch class:

We want to have new AndWebSearch and OrWebSearch classes that allow searches like “I want all webpages that contain the words pirate or (robot and ninja)”. Draw the UML of the classes that would be involved with that and how they relate.