Composite Design Pattern

Composite Design Pattern

Composite design patterns falls into one of structural design patterns. Before diving into the composite design pattern, let me clarify a few points here. We can define the composite objects as objects that contain other objects. For example, the company has divisions, and each division contains departments which contain teams, and so on.

Software engineers need the composite pattern since we would like to treat or manipulate the composite objects in the same way we manipulate the primitive objects. For example, let us assume the department as primitive object that can be increased and decreased in number of employees in it. And we would like to do the same pattern operation on division which is a composite object, containing other departments. The main point is we want to do operations on both primitives and composites in exactly the same way. If we want to distinguish between those two objects, thinking there are two different objects, our code will be too complex, leading to maintenance problem later on.

In the following cases, the composite design pattern is used:

  • Composite design pattern allows you to have a tree structure and ask each node to perform a task.
  • Pattern creates a tree structure of a group of objects.
  • Composite pattern is used where we need to treat a group of objects in similar way as a single object. Composite pattern composes the objects in terms of tree structure to represent part-whole hierarchy.

In regards of a tree structure, the composite pattern treats each node in two ways, composite or leaf. Composite means it can have other objects below it. Leaf means it has no objects below it.

I have created a sample demo for composite pattern in my github here.

Thanks for reading!

Great work you are doing??

要查看或添加评论,请登录

Farruh Habibullaev的更多文章

  • Good API architecture principles

    Good API architecture principles

    Claimer: This article is purely written from my own experience in software development and design, however in some…

  • Practical Dijkstra's Algorithm

    Practical Dijkstra's Algorithm

    In computer science and discrete mathematics, we have encountered the concept of "single - source shortest path" many…

  • Android Core: Looper, Handler, and HandlerThread

    Android Core: Looper, Handler, and HandlerThread

    I have posted this article for those who don't have any idea how multi-threading architecture in android works, and…

  • OkHttp's Ins and Outs (Part 2)

    OkHttp's Ins and Outs (Part 2)

    This is the second part of OkHttp tutorial. You can find the first part of tutorial in the link here.

    1 条评论
  • OkHttp's Ins and Outs (Part 1)

    OkHttp's Ins and Outs (Part 1)

    This is the first part of OkHttp tutorial. 1.

    4 条评论
  • Creational Design Patterns

    Creational Design Patterns

    Creational design patterns are one of three categories of design patterns (creational, structural, and behavioral…

  • Decorator and Proxy Pattern

    Decorator and Proxy Pattern

    Decorator pattern Decorator pattern is one of the structural design patterns and used to modify the functionality of…

  • S.O.L.I.D Principles in OO programming

    S.O.L.I.D Principles in OO programming

    SOLID is the first five essential principles of object oriented programming for building a well designed, working…

  • Mediator and Command Pattern

    Mediator and Command Pattern

    Mediator Pattern Mediator pattern falls into category of behavioral patterns since it deals with behavior of the…

  • Iterator Design Pattern

    Iterator Design Pattern

    Iterator design pattern falls into the category of behavioral pattern. Iterator pattern is used to provide iteration…

社区洞察

其他会员也浏览了