Moe's Tech Blog

[Design Pattern] Composite Pattern 본문

Software Design Pattern/Notes

[Design Pattern] Composite Pattern

moe12825 2022. 8. 15. 07:28
  • Buildings are composed of rooms
    • Although buildings and rooms have slightly different functions, they are essentially treated as generic housing structures
      • An individual can be entered and exited from a building or a room
      • A building or a room can both be used to store furnitures
    • Despite being unique types of objects, they can be dealt with uniformly

Composite Design Pattern

  • Achieves two goals
    • 1. To compose nested structures of objects 
    • 2. To deal with the classes for these objects uniformly

How it Works

  • 1. A component interface serves as the supertype for a set of classes so they all can be dealt with uniformly
    • This is done by enforcing polymorphism
    • All implementing classes conform to the same interface
    • An abstract superclass can also be used in place of an interface as both are allowed in polymorphism

  • 2.Composite class is used to aggregate any class that implements the component interface
    • Composite class allows you to traverse through and potentially manipulate the component objects that the composite object contains
    • Composite class is a subtype of component interface

 

  • 3. leaf class represents a non-composite type. It is not composed of other components
    • Leaf class is a subtype of component interface

 

Recursive Composition

  • is where a composite object contains other composite object
  • Addresses two issues:
    • 1. How do we use individual types of objects to build a tree-like structure?
    • 2. How can we treat the individual types of objects uniformly without checking their types
  • The easiest way to think about this is by thinking a tree
    • At root level, there is the main composite object
      • This is made up of other component objects or leaf objects
    • At each level, more components can be added below each composite object

 

 

Example

  • 1. The following is called the component interface
    • Is used to describe a building, floor, or a room
      • Each of these areunique type of objects

  • 2. The following is called the composite class
    • The housing class is the composite class (recursive)
    • A housing is a type of structure which could also contain other structures
      • Example
        • A building could be represnted by a housing object
        • A building could contain floors
        • Each floor is represented by another housing object
        • This allows to traverse through the building from floor to floor because the building is composed of floors all of which are housing objects

'Software Design Pattern > Notes' 카테고리의 다른 글

Summary of Design Patterns  (0) 2022.08.22
[Design Pattern] Proxy Pattern  (0) 2022.08.19
[Design Pattern] Adapter Pattern  (0) 2022.08.15
[Design Pattern] Facade Pattern  (0) 2022.08.13
[Design Pattern] Factory Pattern  (0) 2022.07.25