1 / 12

Composite

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.

yaakov
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Composite A robot made of smaller robots.

  2. *

  3. * 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.

  4. 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

  5. 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?

  6. 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)

  7. 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

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

  9. 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 }

  10. 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

  11. 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.

More Related