Post

Log inSign up

Post

Log inSign up

Nemanja Rogic on X: "Composite design pattern lets you compose objects into tree structures and then work with these structures as if they were individual objects. It lets clients treat individual objects and compositions of objects uniformly Brief introduction + demo code ↓ #designpatterns #dotnet"

@rogic_nemanja
Nemanja Rogic
@rogic_nemanja
Composite design pattern lets you compose objects into tree structures and then work with these structures as if they were individual objects. It lets clients treat individual objects and compositions of objects uniformly Brief introduction + demo code ↓ #designpatterns #dotnet
11:30 AM · Feb 28, 2021·
1

Log in or sign up for X

See what’s happening and join the conversation

Continue with phone
or
Log in with username or email

Relevant people

Avatar
Nemanja Rogic@rogic_nemanjaFollow
Senior Software Engineer @Microsoft • Passionate about software craftsmanship, shipping reliable products, and tackling challenging problems

Trending now

Terms·Privacy·Cookies·Accessibility·US TIDA·Ads Info·© 2026 X Corp.
  • @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Composite design pattern lets you compose objects into tree structures and then work with these structures as if they were individual objects. It lets clients treat individual objects and compositions of objects uniformly Brief introduction + demo code ↓ #designpatterns #dotnet
    11:30 AM · Feb 28, 2021·
    1
  • @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    We can use a file system to demonstrate the Composite design pattern. In the simplest case, the file system consists of files and folders. We will use FileSystemEntry class to describe operations that are common to both simple (file) and complex (folder) elements of the tree.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    File is a really simple class used to represent an individual object. The file only knows about its own name and size. The file is a simple leaf component. Leaf components usually end up doing most of the real work, since they don’t have anyone to delegate the work to.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    Folder class is a little bit more complicated since it can contain files as well as additional folders (compositions of objects). The folder represents a complex composite component.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    It is important to understand that a composite component doesn’t know the concrete classes of its children. It works with all sub-elements only via the component interface.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    Upon receiving a request, a composite delegates the work to its sub-elements, processes intermediate results and then returns the final result to the client.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    The client code is pretty straight-forward. In order to determine the size of the root folder, we just need to ask the root folder for its size, and it will delegate work to appropriate components in order to give a final answer.
    2
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    Use the Composite pattern when you have to implement a tree-like object structure.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    The Composite pattern provides you with two basic element types that share a common interface: simple leaves and complex containers. A container can be composed of both leaves and other containers. This lets you construct a nested recursive object structure that resembles a tree
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, error-prone. The solution is an interface that allows treating complex and primitive objects uniformly.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    Use the pattern when you want the client code to treat both simple and complex elements uniformly. All elements defined by the Composite pattern share a common interface. Using this interface, the client doesn’t have to worry about the concrete class of the objects it works with
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    + You can work with complex tree structures more conveniently: use polymorphism and recursion to your advantage + You can introduce new element types into the app without breaking the existing code, which now works with the object tree (Open/Closed Principle)
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    - It might be difficult to provide a common interface for classes whose functionality differs too much. In certain scenarios, you’d need to overgeneralize the component interface, making it harder to comprehend
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 28, 2021
    Full implementation with a few additional usage examples can be found on
    GitHub - nemanjarogic/DesignPatternsLibrary: A comprehensive design patterns library implemented in...
    From github.com