The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.
- Learn about various design patterns
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
Hello, my dear friends. In this video, we will examine Composite Design Pattern. So, let's begin. Composite pattern is one of the structural design patterns. Composite Design Pattern is used when we have to represent a part-whole hierarchy. As described by GoF, the Composite Design Pattern composes objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. When we need to create a structure in a way that the objects in the structure have to be treated the same way, we can apply a Composite Design Pattern. Composite pattern is a partitioning design pattern and describes a group of objects that is treated the same way as a single instance of the same type of object. The intent of a composite is to compose objects into tree structures to represent part-whole hierarchies. It allows you to have a tree structure and ask each node in the tree structure to perform a task.
As you can see on the composite UML diagram, the Composite pattern has four participants. The Component is the base interface for all objects in the composition. It should be either an interface or an abstract class with the common methods to manage the child composites. Leaf implements the default behavior of the base component. It doesn't contain a reference to other objects. Composite has leaf elements. Composite stores child components and implements child-related operations in the component interface. Clients use the component class interface to interact with objects in the composition structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and after forwarding. Let's look at the advantages of Composite.
First, Composite defines class hierarchies consisting of primitive and complex objects. It makes it easier for you to add new kinds of components. It provides the flexibility of structure with a manageable interface. So, when should we select this pattern? In situations where you want to represent a full or partial hierarchy of objects, or where you want clients to ignore differences between objects that change in the hierarchy, or in structures that are dynamic or may have any level of complexity, it will be beneficial for your project to prefer the Composite Design Pattern. Okay, now I think it's time to apply the Composite pattern to a project. Let's create a new project and name it Composite. In the previous video, we created a project about shapes and colors using Bridge patterns. So, let's create the same structure but this time using, you guessed it,
the Composite pattern. Let's make the same structure with Composite. First, we need an interface for shapes. We'll create an object and name it Draw. Now, we need leaf objects. First, let's add Triangle implements Shape. Now, let's add Square. Done. Now, the other participant is Composite. This will be our Drawing class implements Shape.
First, we need a shape list. Now, override method. For List, we need add and remove methods. Let's also add a clear method to help clear all elements in List. Okay, done. Now, the last part is client codes. First, let's create a Triangle. Add a Square. Now, we will draw our shapes. Add to List. Now, give a color and draw. Okay, let's clear our list. Let's change color to white and draw again. Done. Now, we've finished, let's try. Great. We have four different shapes. Amazing. We now have finished the Composite pattern project. We'll look into the Decorator Pattern in the next video. So, I'll see you there.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.