Design Pattern In Java Part X Composite Design Pattern
Posted By : Aftab Alam | 31-May-2018
1. Purpose of Design Pattern:- The purpose of design pattern, as per Gang of Four(GoF), to solve recurring problems to design flexible and reusable object-oriented Softwares.
2. Structural Design Pattern:- In Software Engineering, the structural design pattern is those design pattern which makes design easy by identifying a way to realize the relationship between entities. In other words, structural design pattern deals with structures and relationship among them.
3. Composite Design Pattern:- Composite design pattern falls in the category of the structural design pattern.
4. Composite Design Pattern Implementation:- Composite design pattern implementation involves interfaces and classes.
4.1. An abstract class that plays an important role to achieve composite design pattern.
public abstract class SongComponent{
void addComponent(SongComponent songComponent){
}
void removeComponent(SongComponent songComponent){
}
abstract void displaySongInfo();
}
The SomeComponent abstract class is defined which consists of three methods in first two defined with empty body means they won't be overridden by every class that implements this interface and another method is declared as an abstract that would be overridden by every class implementing this class.
4.1.1. The addComponent method is used to add a component whether it is a composite object or non-composite object.
4.1.2. The removeComponent method is used to remove a component whether it is a composite object or non-composite object.
4.1.3. The displaySongInfo method is used to display detailed information of an object whether it is a composite object and noncomposite object.
4.2. A Concrete class which extends SongComponent abstract class.
import java.util.*;
public class SongGroup extends SongComponent{
String name;
String description;
ArrayList<SongComponent> songComponents=new ArrayList<SongComponent>();
public SongGroup(String name, String description){
this.name=name;
this.description=description;
}
public String getName(){
return this.name;
}
public String getDescription(){
return this.description;
}
public void addComponent(SongComponent songComponent){
songComponents.add(songComponent);
}
void removeComponent(SongComponent songComponent){
songComponents.remove(songComponent);
}
public void displaySongInfo(){
System.out.println("=====================================================");
System.out.println("Group Name: "+name+" Group Description: "+description);
Iterator songGroupIterator=songComponents.iterator();
while(songGroupIterator.hasNext()){
SongComponent songComponent=(SongComponent)songGroupIterator.next();
songComponent.displaySongInfo();
}
}
}
4.2.1. The SongGroup class extends SongComponent class.
4.2.2. It consists of three data members in which two are of type string and one is of type array list object.
4.2.3. It has getter method corresponding to its data members.
4.2.4. It overrides three methods from SongComponent abstract class in which two are used to add and remove song component object from array list and one is displaySongInfo() which most important method that would be called in case of when the object type is a composite type.
4.3. This is a class for a song that also extends SomeComponent abstract class.
public class Song extends SongComponent{
String name;
String band;
int releasedYear;
public Song(String name, String band, int releasedYear){
this.name=name;
this.band=band;
this.releasedYear=releasedYear;
}
public String getName(){
return this.name;
}
public String getBand(){
return this.band;
}
public int getReleasedYear(){
return this.releasedYear;
}
public void displaySongInfo(){
System.out.println("Song Name: "+getName()+" Song Band: "+ getBand()+" Song Released Year: "+getReleasedYear());
}
}
4.3.1. It has three data members and getter methods corresponding to them.
4.3.2. It also overrides a displaySongInfo method that would be calling in case of object type non-composite.
4.4. This is the class which is showing the working of a composite design pattern.
public class CompositeDesignPattern{
public static void main(String a[]){
SongComponent songGroup1=new SongGroup("Song Group1", "It is named as Song Group1");
SongComponent songGroup2=new SongGroup("Song Group2", "It is named as Song Group2");
SongComponent songGroup3=new SongGroup("Song Group3", "It is named as Song Group3");
SongComponent songGroup4=new SongGroup("Song Group4", "It is named as Song Group4");
SongComponent song1=new Song("Song1", "Band1", 1);
SongComponent song2=new Song("Song2", "Band2", 2);
SongComponent song3=new Song("Song3", "Band3", 3);
SongComponent song4=new Song("Song4", "Band4", 4);
SongComponent song5=new Song("Song5", "Band5", 5);
songGroup1.addComponent(song1);
songGroup1.addComponent(songGroup2)
songGroup2.addComponent(songGroup3);
songGroup3.addComponent(song2);
songGroup3.addComponent(song3);
songGroup2.addComponent(songGroup4);
songGroup4.addComponent(song4);
songGroup4.addComponent(song5);
songGroup1.displaySongInfo();
}
}
4.4.1. It creates four objects of SongGroup class and it also creates five objects of Song type then it creates a hierarchy of objects.
5. Use(s): It has many uses,
5.1.1. It can be used to create a tree structure.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aftab Alam
Aftab has worked on multiple technologies in front-end as well as in back-end.