Download presentation
Presentation is loading. Please wait.
1
D ESIGN P ATTERNS Vasanth Raja Chittampally http://www.vasanthexperiments.wordpress.com
2
W HAT IS A D ESIGN P ATTERN ? Christopher Alexander says, “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice”. Briefly: Pattern is a solution to a problem in Context.
3
C OMPOSITE Compose objects into tree structures to represent “part-whole” hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly The Composite pattern describes how to use recursive composition so that clients don't have to make this distinction. In UML, composition is depicted as a filled diamond and a solid lineUML
4
A GGREGATION VS COMPOSITION Aggregation doesn’t impose the restriction that the “ Part ” object to be destroyed after the “ Whole ” is destroyed which is the case with composition The more general form, aggregation, is depicted as an unfilled diamond and a solid lineaggregation Composition Aggregation
5
E XAMPLE
6
E XAMPLE (2)
7
A PPLICABILITY Use the Composite pattern when (i) you want to represent part-whole hierarchies of objects. (ii) you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.
8
J AVA E XAMPLE Use Composition or aggregation when you can find “has-a” between objects To use composition in Java, you use instance variables of one object to hold references to other objects The has-a relationship doesn't mean that the containing object must have a constituent object at all times, but that the containing object may have a constituent object at some time
9
C OMPOSITION EXAMPLE public class A { private B b = new B(); public A() { } } class B { public B() { }
10
A GGREGATION EXAMPLE public class A { private B b; public A( B b ) { this.b = b; } } public class C { private B b = new B(); public C() { A a = new A( this.b ); } }
11
R ECURSIVE COMPOSITION Objects can be composited recursively with the use of recursive types or references. Consider a tree. Each node in a tree may be a branch or leaf; in other words, each node is a tree at the same time when it belongs to another tree.recursive typesreferences One implementation for the recursive composition is to let each object have references to others of the same type. In C, for example, a binary tree can be defined like: struct bintree { struct bintree *left, *right; // some data };
12
Q UERIES ???
13
Thank You Vasanth Raja Chittampally http://www.vasanthexperiments.wordpress.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.