The Composite Pattern - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

The Composite Pattern

Description:

... widget container objects which can hold other widgets ... If we want to add new kind of widget Modify WindowUpdate() 9. Composite Pattern Example contd. ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 11
Provided by: sp1d
Category:

less

Transcript and Presenter's Notes

Title: The Composite Pattern


1
The Composite Pattern
  • -Shalini Pradhan

2
What is Composite Pattern?
  • First of all design Patterns
  • Composition of some patterns
  • Constitutes patterns that integrate with a
    synergy
  • Abstraction from a concrete recurring solution
    that solves a problem in a certain context
  • Allows to build complex objects by recursively
    composing similar objects in a tree-like manner

3
Definition
  • Compose objects into tree structures to represent
    whole-part hierarchies. Composite lets clients
    treat individual objects and compositions of
    objects uniformly
  • "Design Patterns Gamma et al.

4
Structure
5
Consequences
  • Benefits
  • It makes it easy to add new kinds of components
  • It makes clients simpler
  • Liabilities
  • It makes it harder to restrict the type of
    components of a composite

6
Implementation Issues
  • Where should be child management methods be
    declared
  • In Component Class
  • Gives transparency since all the components can
    be treated the same
  • Not safe, since clients can try to do meaningless
    things to the leaf components on run-time
  • In Composite Class
  • Gives Safety, since any attempt to perform a
    child operation will be caught at compile-tim
  • No transparency, since now leaf and composite
    components have different interfaces

7
Composite Pattern Example
  • Problem A GUI system has window objects which
    contain various GUI components (widgets) such as,
    buttons, menus and text areas. A window can
    contain widget container objects which can hold
    other widgets
  • Solution 1 (Not using Patterns) If we design all
    the widgets with different interfaces for
    updating the screen

8
Composite Pattern Examplecontd.
  • The WindowUpdate()
  • Public class Window
  • Button buttons
  • Menu menus
  • Textarea textareas
  • WidgetContainer containers
  • public void update()
  • if (buttons ! null)
  • for (int k 0 k ltbuttons.length k)
  • buttonsk.draw()
  • if (menus ! null)
  • for (int k 0 k ltmenus.length k)
  • menusk.refresh() //Similarly handle other
    widgets
  • if (containers ! null)
  • for (int k 0 k ltcontainers.length k)
  • containersk.updateWidgets()
  • If we want to add new kind of widget Modify
    WindowUpdate()

9
Composite Pattern Example contd.
  • Using Composite Pattern!
  • Components

Component
Buttons
Menu
Widget Container
10
Composite Pattern Examplecontd.
  • Now WindowUpdate()
  • Public class Window
  • Component components
  • public void update
  • if (components ! Null)
  • for (int k 0 klt components.length k)
  • componentsk.update
Write a Comment
User Comments (0)
About PowerShow.com