Design Patterns By Võ Văn Hải Faculty f Infrmatin Technlgies HUI Cllectinal Patterns Sessin bjectives Intrductin Cmpsite pattern Iteratr pattern 2 1
Intrductin Cllectinal patterns primarily: Deal with grups r cllectins f bjects Deal with the details f hw t cmpse classes and bjects t frm larger structures Cncentrate n the mst efficient way f designing a class s that its instances d nt carry any duplicate data Allw the definitin f peratins n cllectins f bjects 3 Cllectinal Patterns Cmpsite pattern 4 2
Cmpsite pattern Mtivatin Example Let us create an applicatin t simulate the Windws/UNIX file system. The file system cnsists mainly f tw types f cmpnents directries and files. Directries can be made up f ther directries r files, whereas files cannt cntain any ther file system cmpnent. In this aspect, directries act as nn- terminal ndes and files act as terminal ndes f a tree structure. 5 Cmpsite pattern Mtivatin Example First apprach 6 3
Cmpsite pattern Mtivatin Example First apprach prblem When the client wants t query any f these bjects fr its size, it can simply invke the getcmpnentsize methd. The client treats bth the FileCmpnent and the DirCmpnent bject in the same manner. N separate cde is required t query FileCmpnent bjects and DirCmpnent bjects fr their size. But when call getcmpnent r addcmpnent methd, client must check t make sure that the FileSystemCmpnent bject it is wrking with is in fact a DirCmpnent bject. 7 8 4
Cmpsite pattern Mtivatin Example Secnd apprach 9 Cmpsite pattern Mtivatin Example Secnd apprach bjectives Prvide the same advantage f allwing the client applicatin t treat bth the cmpsite DirCmpnent and the individual FileCmpnent bjects in a unifrm manner while invking the getcmpnentsize methd Free the client applicatin frm having t check t make sure that the FileSystemCmpnent it is dealing with is an instance f the DirCmpnent class while invking any f the cmpsitespecific methds such as addcmpnent r getcmpnent 10 5
11 Cmpsite pattern Intent Cmpse bjects int tree structures t represent part-whle hierarchies. Cmpsite lets clients treat individual bjects and cmpsitins f bjects unifrmly. This is called recursive cmpsitin Applicability Yu want t represent part-whle hierarchies f bjects Yu want clients t be able t ignre the difference between cmpsitins f bjects and individual bjects. Clients will treat all bjects in the cmpsite structure unifrmly. 12 6
Cmpsite Pattern UML 13 Cmpsite Pattern UML - Participants Cmpnent declares the interface fr bjects in the cmpsitin. implements default behavir fr the interface cmmn t all classes, as apprpriate. declares an interface fr accessing and managing its child cmpnents. Leaf (ptinal) defines an interface fr accessing a cmpnent's parent in the recursive structure, and implements it if that's apprpriate. represents leaf bjects in the cmpsitin. A leaf has n children. defines behavir fr primitive bjects in the cmpsitin. 14 7
Cmpsite Pattern UML - Participants Cmpsite defines behavir fr cmpnents having children. stres child cmpnents. implements child-related peratins in the Cmpnent interface. Client manipulates bjects in the cmpsitin thrugh the Cmpnent interface. 15 Cmpsite Pattern Real wrld example Java Use f Cmpsite - AWT Widgets 16 8
Cllectinal Patterns Iteratr Pattern 17 Iteratr Pattern Descriptin The Iteratr pattern allws a client bject t access the cntents f a cntainer in a sequential manner, withut having any knwledge abut the internal representatin f its cntents. The term cntainer can simply be defined as a cllectin f data r bjects. The Iteratr pattern enables a client bject t traverse thrugh this cllectin f bjects (r the cntainer) withut having the cntainer t reveal hw the data is stred internally. The Cntainer bject shuld be designed t prvide a public interface in the frm f an Iteratr bject fr different client bjects t access its cntents. 18 9
Iteratr Pattern UML 19 Iteratr Pattern UML - Participants Iteratr (AbstractIteratr) defines an interface fr accessing and traversing elements. CncreteIteratr (Iteratr) implements the Iteratr interface. keeps track f the current psitin in the traversal f the aggregate. Aggregate (AbstractCllectin) defines an interface fr creating an Iteratr bject CncreteAggregate (Cllectin) implements the Iteratr creatin interface t return an instance f the prper CncreteIteratr 20 10
Types f Iteratr Internal iteratrs The cllectin itself ffers methds t allw a client t visit different bjects within the cllectin. Fr example, the java.util.resultset class cntains the data and als ffers methds such as next() t navigate thrugh the item list There can be nly ne iteratr n a cllectin at any given time The cllectin has t maintain r save the state f iteratin. 21 Types f Iteratr External iteratrs The iteratin functinality is separated frm the cllectin and kept inside a different bject referred t as an iteratr. Usually, the cllectin itself returns an apprpriate iteratr bject t the client depending n the client input. There can be multiple iteratrs n a given cllectin at any given time. The verhead invlved in string the state f iteratin is nt assciated with the cllectin. It lies with the exclusive Iteratr bject. 22 11
Iteratr Pattern Internal Iteratr example 23 Iteratr Pattern External Iteratr example 24 12
Iteratr Pattern Real wrld Java Example DOM Traversal Example 25 Summary Pattern Name Cm psite Iteratr Fly weight Visitr Descriptin Allws bth individual bjects and cm psite bjects t be treated unifrmly. Allws a client t access the cntents f an aggregate bject (cllectin f bjects) in sme sequential manner, withut having any knwledge abut the internal representatin f its cntents. The intrinsic, invariant cmmn infrmatin and the variable parts f a class are separated int tw classes, leading t savings in terms f the memry usage and the amunt f time required fr the creatin f a large number f its instances. Allws an peratin t be defined acrss a cllectin f different bjects withut changing the classes f bjects n which it perates. 26 13
Questins and Answers 27 14