Composite Pattern

박영재·2023년 12월 10일
0

Design Pattern

목록 보기
1/1

Composite Pattern

Intent

Hierarchical representation of an object structure
where each object works on the same interfcae -> uniformity

Without Pattern

  • Type check and type coonversion are probably required
if (object instanceof (Composite)){
	// do what a composite does
}
if (object instanceof (Leaft){
	// do what a Leaf does
}
  • violation of OCP -> what if another leaf or composite is added?

Trade-off on Placing Child Management

in Component (transparency)

pros: consistent interface
cons: possibile illegal method call at runtime

in Composite (safety)

pros: no illegal method call at runtime
cons: inconsistent interface
->type checking and type conversion may be required

Iterator

A client may want to access each Component
Composite's data structure vaires for composing component objects such as tree, graph or even hashmap

How to let a client access each Component, while hidding the internal implementation of data structure?
-> Iterator

Decorator

Both involve recursive composition for an open-ended number of objects.
But Decorator adds responsibilities,

while Composite provides a uniform object representation

they often work in collaboration

profile
People live above layers of abstraction beneath which engineers reside

0개의 댓글