image
Composite Pattern
Start course
Difficulty
Intermediate
Duration
4h 56m
Students
118
Ratings
4/5
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello, dear friends. In this video, we will examine Composite Design Pattern. 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 let's 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 has to be treated the same way, we can apply 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 the 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 the 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 recipient is a leaf, then request is handled directly. If recipient is a composite, then it usually forwards the request to it's 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. Now, I think it is 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. Let's create the same structure, but this time using the composite pattern. First, we need an interface for shapes. We'll create a method and name it "Draw" or if you want you can change name with "Create." Now, we need  the leaf objects. First, let's add triangle. Implement shape.

Now, let's add square. Done. Now the other participant is "composite." This will be our drawing class. Implement 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. 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. Let's clear our list. Let's change color to white and draw again. Done. Now we have finished. Let's try. Great. We have four different shapes. We now have finished the composite pattern project. We'll look into the decorator pattern in the next video. See you in the next video.

 

About the Author
Students
3925
Courses
64
Learning Paths
5

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.

Covered Topics