1 / 27

Software Design

Software Design. Lecture : 36. Composite Design Pattern. Motivation for Composite Design Pattern. Every component or object can be classified into one of the two categories — Individual Components Composite Components — which are composed of

mforrest
Download Presentation

Software Design

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. Software Design Lecture : 36

  2. Composite Design Pattern

  3. Motivation for Composite Design Pattern • Every component or object can be classified into one of the two categories — • Individual Components • Composite Components — which are composed of individual components or other composite components

  4. Intend of Composite Design Pattern • To have a common interface for both individual and composite components so that client programs can view both the individual components and groups of components uniformly. • Usually a Tree structure having nodes and leave nodes represents composite and individual components respectively.

  5. Discussion on Whole-Part relation

  6. Aggregation or “ has –a “ relationship • Specialized form of association i-e Weak Association. • Assembling a class from other classes. • Aggregation is a special type of relationship used to model a "whole to its parts" relationship • Part class is independent from the whole class's lifecycle.

  7. Aggregation • In an aggregation relationship, the child class instance can outlive its parent class. • Child class can be a part of multiple parent classes. • To represent an aggregation relationship, you draw a solid line from the parent class to the part class, and draw an unfilled diamond shape on the parent class's association end

  8. Example

  9. Aggregation Example

  10. Tree Structure Revisited

  11. Discussion on Vector

  12. Composite Pattern Defined • Composite Design pattern allow us to compose objects into tree structures to represent whole-part hierarchy . It let the client handle the composite and individual components in a uniform manner

  13. Class Diagram of Composite Design Pattern

  14. Example of Composite Pattern • Let us create an application to simulate the Windows/UNIX file system. The file system consists mainly of two types of components — directories and files. Directories can be made up of other directories or files, whereas files cannot contain any other file system component. In this aspect, directories act as nonterminal nodes and files act as terminal nodes or leaf node of a tree structure.

  15. Design Approach • Let us define a common interface for both directories and files in the form of a Java interface FileSystemComponent The FileSystemComponent interface declares methods that are common for both file components and directory components. Let us further define two classes — FileComponent and DirComponent — as implementers of the common FileSystemComponent interface as shown in next slide

  16. Class Diagram of Proposed Approach

  17. Description of Classes • FileComponent The FileComponent class represents a file in the file system and offers implementation for the following methods. • getComponentSize() • This method returns the size (in kilobytes) of the file represented by the File- Component object.

  18. Class Descriptions • DirComponent This class represents a directory in the file system. Since directories are composite entities, the DirComponent provides methods to deal with the components it contains. These methods are in addition to the common getComponentSize method declared in the FileSystemComponent interface.

  19. Class Description • addComponent(FileSystemComponent) This method is used by client applications to add different DirComponent and FileComponent objects to a DirComponent object. • getComponent(int) The DirComponent stores the other FileSystemComponent objects inside a vector. This method is used to retrieve one such object stored at the specified location.

  20. Class Description • getComponentSize() This method returns the size (in kilobytes) of the directory represented by the DirComponent object. As part of the implementation, the DirComponent object iterates over the collection of FileSystemComponent objects it contains, in a recursive manner, and sums up the sizes of all individual FileComponents. The final sum is returned as the size of the directory it represents.

  21. Processing of Class Diagram A typical client would first create a set of FileSystemComponent objects (both DirComponent and FileComponent instances). It can use the addComponent method of the DirComponent to add different FileSystemComponents to a DirComponent, creating a hierarchy of file system (FileSystemComponent) objects.

  22. Calculating the Size of Object When the client wants to query any of these objects for its size, it can simply invoke the getComponentSize method. The client does not have to be aware of the calculations involved or the manner in which the calculations are carried out in determining the component size. In this aspect, the client treats both the FileComponent and the DirComponent object in the same manner. No separate code is required to query FileComponent objects and DirComponent objects for their size.

  23. Problem in Current Design • Client to keep track when calling a non-common method like addcomponent and getcomponent as these methods exists only in DirComponent

  24. Design Approach -II Provide the same advantage of allowing the client application to treat both the composite DirComponent and the individual FileComponent objects in a uniform manner while invoking the getComponentSize method

  25. Code in Word File

More Related