Composite
This presentation is the property of its rightful owner.
Sponsored Links
1 / 12

Composite PowerPoint PPT Presentation


  • 95 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Composite

An Image/Link below is provided (as is) to download presentation

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

Composite

A robot made of smaller robots.


Composite

*


Composite

*

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

The basic idea

  • 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


Try it out

Try it out

  • 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?


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)


Where we are

Where we are

  • 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


Composite

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);

myComposite.add(leaf);


Should we have features like add remove in the abstract superclass

Should we have features like add() remove() in the abstract superclass?

  • 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;

composite.add(leaf)

} else {

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

// case

}


Where we are1

Where we are

  • 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


Composite

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.


  • Login