When to Use Composite Design Pattern in Java
Composite Design Pattern is a design pattern that falls in the category of structural patterns. Such patterns are concerned with how classes and objects are composed to form larger structures. Composite Pattern describes one such way of building a class hierarchy made up of classes for two different kinds of objects (composite and primitive). One goal of software engineering is to maintain high cohesion between modules and the other to reduce coupling.
Many applications that have to deal with hierarchical data need to make sure that they do not have too much of dependency among their classes because such applications constantly need to update their components. A file system is a classic example of hierarchical structure that contains folders within folders and files within folders. A file can be a video, text document, audio file. So, there can be multiple audio files in one folder and multiple video files in other and both these can be inside of another folder that represents “media” which can itself be in another folder called “lectures”. The Lecture folder can also contain another folder that represent topic, and this can go on for ever. The point we are trying to make is if you are trying to design such an application that deals with hierarchical components, you may end up creating class structures that are highly coupled and in order to add more components a lot of redundancy might occur. In order to solve this common problem composite design pattern provides an elegant solution. First of all, let us see what is composite design pattern? (Java Development Company)
What is composite design pattern?
Composite design pattern provides a design hierarchy, in which nodes with children differ from nodes without children. You can think of the tree data structure in this case. And compared to our example, a node can be a folder, and children might be files or folders consisting of files. A group of objects are treated the same way as a single instance of the same type of that object, so no matter what, we will think of the node and its children as the folder component, this makes it easier for us to design our hierarchical structure of components for our application. We will refer to components (objects) within of components (objects) as compositions. The composite design pattern lets the client treat individual objects and compositions uniformly. The core thing to keep in mind is the tree structure, while dealing with this design pattern. In order to understand what is composite design pattern and its applicability better, let's make use of a code example in java which is similar to the file system example we gave.
Read: "All you need to know about Template Method Design Pattern"
Composite design pattern example in Java
The example we are going to use is going to be of a course that consist of topics and lectures. If you are taking a course on any subject, there will be a topic and that topic can consist of many topics and lectures. So, how would that be implemented? We will first show you the common approach.
1. Let us create a Lecture Class
Fig: composite design pattern example in java – Lecture Class
The Lecture has a name and a display method to display the hierarchy of it.
2. The Topic Class
Fig: composite design pattern example in java – Topic Class
In our example, the topic class has the features of adding a topic, adding a lecture, and displaying the lectures and topics in a hierarchy. Topic binds other topics and lectures within itself. So, we can have subtopics and lectures on each.
3. Let’s see how we combine these in the main method
Read the full article: https://www.decipherzone.com/blog-detail/when-use-composite-design-pattern-javaArticles from Mahipal Nehra
View blogMicroservices vs Application Programming Interfaces (APIs). Both have a distinct role in software de ...
We are living in a world that is on the verge of Industry 4.0 or the fourth industrial revolution. A ...
The software development industry progresses through continuous evolution where people try to solve ...
You may be interested in these jobs
-
Technical Lead
Found in: Talent500 IN C2 - 3 hours ago
Talent500. Raipur, IndiaTechnical Lead Peach Payments Our vision is to deliver delightful experiences for merchants and users and be the payments partner of choice on the African continent. · In everything we do, we strive to make it as easy as possible for merchants to focus on their business, while we ...
-
Assistant Manager
Found in: Talent IN C2 - 5 days ago
Royal Enfield Chennai, IndiaJob Requirements · Position title · Assistant Manager - Quality Management System · Reports to · Deputy Manager – Warranty Management · Job grade · Assistant Manager · Location · Chennai · Job Purpose · The Middle Management-Quality-Field will work to resolve field quality ...
-
Senior Mechanical Engineer
Found in: Talent IN C2 - 5 days ago
Barry-Wehmiller Chennai, India Full timeDescription · : The roll will involve · Organize, assign, and delegate necessary engineering and design tasks to produce construction documents in conjunction with a team of engineers and designers. · Manage scope, budget and schedule to meet project goals. · Coordinate and co ...
Comments