in design pattern java object oriented programming oop cleanc code programming refactor ~ read.

Composite Design Pattern in Java

In this post we are going to talk about another structural design pattern - Composite Design Pattern.

What the heck is Composite Design Pattern?

Here are some definition that I think is really helpful, at least for me, to understand what Composite Design Pattern is.

Highly recommend you guys to have a look at the links first before proceed.

In my definition, Composite Pattern is useful when you are trying to treat an individual and a collection of individual the same. Let's say you have a Page object and a PageList array consisted of multiple Page objects. And you want to treat them the same as it is so you don't have to create some if..else block to differentiate them when you are trying to use them.

Enough for talking, let's dive into the code now.

Demo

Let's say I have a class called Page, and another class called PageItem. And I want to treat PageItem(individual objects) and Page(compositions of objects) uniformly.

package com.designpattern.composite;

import java.util.ArrayList;  
import java.util.List;

public abstract class PageComponent {  
    String name;
    String url;
    List<PageComponent> pageComponent = new ArrayList<>();

    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }

    public abstract String toString();

    public String print(PageComponent pageComponent) {
        StringBuilder builder = new StringBuilder(name);
        builder.append(": ");
        builder.append(url);
        builder.append("\n");
        return builder.toString();
    }
}

(PageComponent)

First in order to treat them the same, I need to create an abstract class to bind the contract first.

Next I need to create Page class and PageItem class.

package com.designpattern.composite;

import java.util.Iterator;

public class Page extends PageComponent {  
    public Page(String name, String url) {
        this.name = name;
        this.url = url;
    }

    public PageComponent add(PageComponent pageComponent) {
        pageComponent.add(pageComponent);
        return pageComponent;
    }

    public PageComponent remove(PageComponent pageComponent) {
        pageComponent.remove(pageComponent);
        return pageComponent;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();

        builder.append(print(this));

        Iterator<PageComponent> itr = pageComponent.iterator();
        while(itr.hasNext()) {
            PageComponent pageComponent = itr.next();
            builder.append(pageComponent.toString());
        }

        return builder.toString();
    }
}

(Page)

package com.designpattern.composite;

public class PageItem extends PageComponent {  
    public PageItem(String name, String url) {
        this.name = name;
        this.url = url;
    }

    @Override
    public String toString() {
        return print(this);
    }
}

(PageItem)

As you can see that both Page class and PageItem class extend from PageComponent class. And only Page class has add() & remove() method, also its toString() method is a different from PageItem since Page might has a collection pages while PageItem only has one page.

End

This is a crash course for Composite Design Pattern. Hope you guys can learn something from there and as always if you have any opinions or ideas please leave it below. Thanks!.

comments powered by Disqus