Design Pattern: Composite Pattern

Tram Ho

Design Pattern: Composite Pattern

As you all know Design Pattern has great applications in the programming world. Perhaps many of you are not familiar with it so you can follow the next post to get an overview of Design Pattern and command pattern examples.

In today’s article, I will introduce the Composite Pattern

Still will like the article above I will follow 3 parts:

  • Composite pattern
  • How to use Composite Pattern
  • Application of Composite Pattern in IOS programming

Composite Pattern

Composite is a design belonging to the Structural Pattern group. Composite Pattern is a combination of components related to each other to create a larger component. It allows to perform interactions with all objects in the same sample. Below is a diagram depicting this pattern. Composite Pattern includes the following components:

  1. Component protocol : Ensures all components in the tree are handled in the same direction.
  2. Leaf : A component in the tree but has no children.
  3. Composite : A container containing leaf objects and other composites .

All composite and leaf nodes derive from the component protocol. We can organize several different leaf layers in the composite object.

How to use Composite Pattern

Composite Pattern is used when we need to handle a similar group of objects in a way that handles an object. The composite pattern arranges objects according to a tree structure to explain one part as well as the entire hierarchy. This pattern creates a class containing its own object group. This class provides ways to modify groups of the same object. This pattern allows clients to write the same code to interact with this composite object, regardless of whether it is an individual object or a collection of objects.

A simple example can be seen that is: Array is a composite with main component is Array. This composite is a container to contain leaf objects, with each leaf being an Int or String type or whatever you add to the array.

Above is how to use but certainly quite vague, right ?. Let’s go to implement it right away.

First we initialize a View class that acts as a Component Protocol with all the methods of initializing, adding and removing to perform additional operations and delete child views in it. In addition, View also has names and may have superView, childViews, so we declare these additional properties in View.

Initiate the initialization of Leaf Buttons and Label implementations of methods and properties of Component Protocol View.

We try to create a Dialog that acts as a Composite containing leaves and implement the method of Component Protocol View and try a little as follows:

As we can see it is quite simple, isn’t it.

However, when will we use the Composite Pattern?

  • Composite Pattern should only be applied when the object group must act as a single object (in the same way).
  • Composite Pattern can be used to create a tree-like structure.

Application of Composite Pattern in IOS programming

The basic View example we just made is the greatest demonstration of the application of the Composite pattern in ios programming.

Above is the Composite Pattern and its usage and application in IOS programming. Hopefully, this will be of some help to new students who are learning about design patterns. See you in the next post sharing.

Share the news now

Source : Viblo