Composite Design Pattern by example

This post explains Composite design pattern with an example.

When to use ? – Use Composite pattern when you have treat a collection of objects as they were once one thing. With Composite design pattern it is very easy to simplify operations over collection data structure. It reduces complexity of the code required if you were going to handle collection as special case.

How to Use? - The Composite pattern consist of Component, Leaf and Composite Classes as shown below class diagram

Component – Component declares the common interface that both the single and composite nodes will implements

Leaf – The leaf class represent the singular atomic data type implementing he component interface.

 

image

 Composite – Composite is an abstract entity which declares common functionality which are needed by concrete implementation for handling collection data.

An Example -

 

image

Item.java

public interface Item {
    public double getPrice();
}

 

ColdDrink.java

public class ColdDrink implements Item {
    private String itemName;
    private String iemDesc;
    private double price;
    public ColdDrink(String itemName, String desc, double price){
        this.itemName=itemName;
        this.iemDesc= desc;
        this.price = price;
    }
    public double getPrice() {
        return price;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemName() {
        return itemName;
    }

    public void setIemDesc(String iemDesc) {
        this.iemDesc = iemDesc;
    }

    public String getIemDesc() {
        return iemDesc;
    }
}

 

Composite.java

public abstract class Composite implements Item {
    /**
     * @associates <{demp.composite.Item}>
     */
    List<Item> items = new ArrayList<Item>();

    public void add(Item itm) {
        items.add(itm);
    }

    public void remove(Item itm) {
        items.remove(itm);
    }

    public void addAll(List lst) {
        items.addAll(lst);
    }

    public double getPrice() {
        double sum=0;
        for (Item i: items) {
            sum += i.getPrice();
        }
        return sum;
    }

}

 

ColdDrinkPartyPack.java

import java.util.List;

public class ColdDrinkPartyPack extends Composite {
    public ColdDrinkPartyPack(List items){
        super.addAll(items);       
    }
    public double getPrice(){
       return super.getPrice() - super.getPrice()*.25; // get 25% discount on family pack
    }
}

ColdDrinkFamilyPack .java

import java.util.List;

public class ColdDrinkFamilyPack extends Composite{
    public ColdDrinkFamilyPack(List items){
        super.addAll(items);       
    }
   public double getPrice(){
       return super.getPrice() - super.getPrice()*.10; // get 15% discount on family pack
   }
}

 

The application Class

public class Application {
    public Application() {
    }
    public static void main(String[] args){
        List items = new ArrayList();
        items.add(new ColdDrink("Pepsi","cold drink",10));
        items.add(new ColdDrink("Cock","cold drink",20));
        items.add(new ColdDrink("maza","cold drink",15));
        ColdDrinkFamilyPack familyPack = new ColdDrinkFamilyPack(items);
        System.out.println(familyPack.getPrice());
        List item2s = new ArrayList();
        item2s.add(new ColdDrink("Pepsi","cold drink",10));
        item2s.add(new ColdDrink("Cock","cold drink",20));
        item2s.add(new ColdDrink("maza","cold drink",15));
        item2s.add(new ColdDrink("Pepsi","cold drink",10));
        item2s.add(new ColdDrink("Cock","cold drink",20));
        item2s.add(new ColdDrink("maza","cold drink",15));
        ColdDrinkPartyPack partyPack = new ColdDrinkPartyPack(item2s);
        System.out.println(partyPack.getPrice());
    }
}

 

Output

40.5
67.5

Comments

  1. you spelled Coke Cock LOL

    ReplyDelete
  2. 367DCFletcherF4491April 14, 2024 at 5:25 AM

    05AF9
    ----
    matadorbet
    ----
    ----
    ----
    ----
    ----
    ----
    ----

    ReplyDelete

Post a Comment

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester